summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-04-19 06:14:34 +0000
committerLuke Kanies <luke@madstop.com>2005-04-19 06:14:34 +0000
commit371235bc1d5d2f260106444d44dfbff14978b020 (patch)
tree5f53dbf0be398ed76c3592a0bb92a136a431369c
parent7deba97509ec91885d76e0d909287a7553c995d8 (diff)
downloadpuppet-371235bc1d5d2f260106444d44dfbff14978b020.tar.gz
puppet-371235bc1d5d2f260106444d44dfbff14978b020.tar.xz
puppet-371235bc1d5d2f260106444d44dfbff14978b020.zip
reorganizing the FileType/FileRecord into a base class (TypeGenerator) and basing them off it
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@192 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/blink/type.rb14
-rw-r--r--lib/blink/type/filetype.rb456
-rw-r--r--lib/blink/type/typegen.rb155
-rw-r--r--lib/blink/type/typegen/filerecord.rb144
-rw-r--r--lib/blink/type/typegen/filetype.rb231
-rw-r--r--test/types/tc_filetype.rb10
6 files changed, 480 insertions, 530 deletions
diff --git a/lib/blink/type.rb b/lib/blink/type.rb
index 26b69f669..344c21a40 100644
--- a/lib/blink/type.rb
+++ b/lib/blink/type.rb
@@ -63,6 +63,20 @@ module Blink
}
#---------------------------------------------------------------
+ # a test for whether this type is allowed to have instances
+ # on clients
+ # subclasses can just set '@abstract = true' to mark themselves
+ # as abstract
+ def Type.abstract
+ if defined? @abstract
+ return @abstract
+ else
+ return false
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
# this is meant to be run multiple times, e.g., when a new
# type is defined at run-time
def Type.buildtypehash
diff --git a/lib/blink/type/filetype.rb b/lib/blink/type/filetype.rb
deleted file mode 100644
index d3a715c7a..000000000
--- a/lib/blink/type/filetype.rb
+++ /dev/null
@@ -1,456 +0,0 @@
-#!/usr/local/bin/ruby -w
-
-# $Id$
-
-# parse and write configuration files using objects with minimal parsing abilities
-
-require 'etc'
-require 'blink/type'
-
-module Blink
- class FileType < Blink::Type
- include Enumerable
-
- @namevar = :notused
- @name = :filetype
-
- attr_accessor :file, :splitchar, :childtype
-
- @@classes = Hash.new(nil)
-
- #---------------------------------------------------------------
- def FileType.[](name)
- return @@classes[name]
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.childtype=(childtype)
- @childtype = childtype
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.childtype
- return @childtype
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.name
- return @name
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.name=(name)
- @name = name
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.namevar
- return :notused
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.regex
- return @regex
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.splitchar=(splitchar)
- @regex = %r{#{splitchar}}
- @splitchar = splitchar
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.splitchar
- return @splitchar
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileType.newtype(arghash)
- options = [:name, :linesplit, :recordsplit, :fields, :namevar]
-
- #arghash = Hash[*args]
-
- unless arghash.include?(:linesplit)
- arghash[:linesplit] = "\n"
- end
-
- arghash.each { |key,value|
- unless options.include?(key)
- raise "Invalid argument %s on class %s" %
- [key,self]
- end
- }
- options.each { |option|
- unless arghash.include?(option)
- raise "Must pass %s to class %s" %
- [option,self.class.to_s]
- end
- }
-
- if @@classes.include?(arghash[:name])
- raise "File type %s already exists" % arghash[:name]
- end
-
- klassname = arghash[:name].capitalize
-
- # create the file type
- module_eval "
- class %s < FileType
- end" % klassname
- klass = eval(klassname)
-
- # now create the record type
- klass.childtype = Blink::FileRecord.newtype(
- :name => arghash[:name] + "_record",
- :splitchar => arghash[:recordsplit],
- :fields => arghash[:fields],
- :namevar => arghash[:namevar]
- )
- klass.splitchar = arghash[:linesplit]
- klass.name = arghash[:name]
-
- Blink.debug("adding class %s as a subclass of %s" % [arghash[:name],self])
- @@classes[arghash[:name]] = klass
-
- return klass
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def [](name)
- return @childhash[name]
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def []=(name,value)
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # we don't really have a 'less-than/greater-than' sense here
- # so i'm sticking with 'equals' until those make sense
- def ==(other)
- unless self.children.length == other.children.length
- Blink.debug("file has %s records instead of %s" %
- [self.children.length, other.children.length])
- return self.children.length == other.children.length
- end
- equal = true
- self.zip(other.children) { |schild,ochild|
- unless schild == ochild
- Blink.debug("%s has changed in %s" %
- [schild.name,self.name])
- equal = false
- break
- end
- }
-
- return equal
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # create a new record with a block
- def add(&block)
- obj = self.class.childtype.new(self,&block)
- Blink.debug("adding %s" % obj.name)
- @childary.push(obj)
- @childhash[obj.name] = obj
-
- return obj
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def children
- return @childary
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # remove a record
- def delete(name)
- if @childhash.has_key?(name)
- child = @childhash[name]
-
- @childhash.delete(child)
- @childary.delete(child)
- else
- raise "No such entry %s" % name
- end
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def each
- @childary.each { |child|
- yield child
- }
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # create a new file
- def initialize(file)
- @file = file
-
- @childary = []
- @childhash = {}
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # this is where we're pretty different from other objects
- # we can choose to either reparse the existing file and compare
- # the objects, or we can write our file out and do an
- # text comparison
- def insync?
- tmp = self.class.new(@file)
- tmp.retrieve
-
- return self == tmp
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # read the whole file in and turn it into each of the appropriate
- # objects
- def retrieve
- str = ""
- ::File.open(@file) { |fname|
- fname.each { |line|
- str += line
- }
- }
-
- @childary = str.split(self.class.regex).collect { |record|
- child = self.class.childtype.new(self)
- child.record = record
- #puts "adding child %s" % child.name
- child
- }
-
- @childary.each { |child|
- @childhash[child.name] = child
- }
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def sync
- #unless self.insync?
- self.write
- #end
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def to_s
- return @childary.collect { |child|
- child.to_s
- }.join(self.class.splitchar) + self.class.splitchar
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def write
- ::File.open(@file, "w") { |file|
- file.write(self.to_s)
- }
- end
- #---------------------------------------------------------------
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- class FileRecord < Blink::Type
- attr_accessor :fields, :namevar, :splitchar, :object
-
- @namevar = :notused
- @name = :filerecord
-
- @@subclasses = {}
-
- #---------------------------------------------------------------
- def FileRecord.fields=(ary)
- @fields = ary
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.fields
- return @fields
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.name=(name)
- @name = name
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.name
- return @name
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- # create a new record type
- def FileRecord.newtype(*args)
- options = [:name, :splitchar, :fields, :namevar]
-
- arghash = Hash[*args]
- arghash.each { |key,value|
- unless options.include?(key)
- raise "Invalid argument %s on class %s" %
- [key,self.class.to_s]
- end
- }
- options.each { |option|
- unless arghash.include?(option)
- raise "Must pass %s to class %s" %
- [option,self.class.to_s]
- end
- }
- klassname = arghash[:name].capitalize
-
- module_eval "
- class %s < FileRecord
- end" % klassname
- klass = eval(klassname)
-
- klass.fields = arghash[:fields]
- klass.splitchar = arghash[:splitchar]
- klass.namevar = arghash[:namevar]
- klass.name = arghash[:name]
-
- return klass
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.namevar=(field)
- @namevar = field
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.namevar
- return @namevar
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.regex
- return @regex
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.splitchar=(char)
- @splitchar = char
- @regex = %r{#{char}}
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def FileRecord.splitchar
- return @splitchar
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def [](field)
- @fields[field]
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def []=(field,value)
- @fields[field] = value
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def ==(other)
- unless self.class == other.class
- return false
- end
-
- unless self.name == other.name
- return false
- end
- @fields.keys { |field|
- unless self[field] == other[field]
- Blink.debug("%s -> %s has changed" % [self.name, field])
- return false
- end
- }
- return true
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def initialize(object)
- @object = object
- @fields = {}
- if block_given?
- yield self
- end
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def record=(record)
- ary = record.split(self.class.regex)
- self.class.fields.each { |field|
- @fields[field] = ary.shift
- #puts "%s => %s" % [field,@fields[field]]
- }
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def name
- if @fields.include?(self.class.namevar)
- return @fields[self.class.namevar]
- else
- raise "No namevar for objects of type %s" % self.class.to_s
- end
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- def to_s
- ary = self.class.fields.collect { |field|
- if ! @fields.include?(field)
- raise "Object %s is missing field %s" % [self.name,field]
- else
- @fields[field]
- end
- }.join(self.class.splitchar)
- end
- #---------------------------------------------------------------
- end
- #---------------------------------------------------------------
-
- #---------------------------------------------------------------
- class FileRecordState < Blink::State
- #---------------------------------------------------------------
- def initialize
- end
- #---------------------------------------------------------------
- end
- #---------------------------------------------------------------
-end
diff --git a/lib/blink/type/typegen.rb b/lib/blink/type/typegen.rb
index afca0afea..63bca953d 100644
--- a/lib/blink/type/typegen.rb
+++ b/lib/blink/type/typegen.rb
@@ -7,91 +7,108 @@
require 'etc'
require 'blink/type'
-module Blink
- class TypeGenerator < Blink::Type
- include Enumerable
+class Blink::Type::TypeGenerator < Blink::Type
+ include Enumerable
- @namevar = :notused
- @name = :typegen
+ @namevar = :notused
+ @name = :typegen
+ @abstract = true
- attr_accessor :childtype
-
- @@subclasses = Hash.new(nil)
+ #---------------------------------------------------------------
+ def TypeGenerator.[](name)
+ return @subclasses[name]
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.[](name)
- return @@classes[name]
- end
- #---------------------------------------------------------------
+ #---------------------------------------------------------------
+ def TypeGenerator.inherited(subclass)
+ subclass.initvars
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.childtype=(childtype)
- @childtype = childtype
- end
- #---------------------------------------------------------------
+ #---------------------------------------------------------------
+ def TypeGenerator.initvars
+ @subclasses = Hash.new(nil)
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.childtype
- return @childtype
- end
- #---------------------------------------------------------------
+ #---------------------------------------------------------------
+ def TypeGenerator.name
+ return @name
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.name
- return @name
- end
- #---------------------------------------------------------------
+ #---------------------------------------------------------------
+ def TypeGenerator.name=(name)
+ @name = name
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.name=(name)
- @name = name
- end
- #---------------------------------------------------------------
+ #---------------------------------------------------------------
+ def TypeGenerator.namevar
+ return :notused
+ end
+ #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.namevar
- return :notused
+ #---------------------------------------------------------------
+ def TypeGenerator.newtype(arghash)
+ unless defined? @options
+ raise "Type %s is set up incorrectly" % self
end
- #---------------------------------------------------------------
- #---------------------------------------------------------------
- def TypeGenerator.newtype(arghash)
- unless defined? @options
- raise "Type %s is set up incorrectly" % self
+ arghash.each { |key,value|
+ unless @options.include?(key)
+ raise "Invalid argument %s on class %s" %
+ [key,self]
end
-
- arghash.each { |key,value|
- unless options.include?(key)
- raise "Invalid argument %s on class %s" %
- [key,self]
- end
- }
- options.each { |option|
- unless arghash.include?(option)
- raise "Must pass %s to class %s" %
- [option,self.class.to_s]
- end
- }
-
- if @@subclasses.include?(arghash[:name])
- raise "File type %s already exists" % arghash[:name]
+ }
+ @options.each { |option|
+ unless arghash.include?(option)
+ p arghash
+ raise "Must pass %s to class %s" %
+ [option,self]
end
+ }
- klassname = arghash[:name].capitalize
+ if @subclasses.include?(arghash[:name])
+ raise "File type %s already exists" % arghash[:name]
+ end
- # create the file type
- module_eval "
- class %s < TypeGenerator
- end" % klassname
- klass = eval(klassname)
- klass.name = arghash[:name]
+ klassname = arghash[:name].capitalize
- Blink.debug("adding class %s as a subclass of %s" % [arghash[:name],self])
- @@subclasses[arghash[:name]] = klass
+ # create the file type
+ Blink::Type.module_eval "
+ class %s < %s
+ end" % [klassname,self]
+ klass = eval(klassname)
+ klass.name = arghash[:name]
- return klass
- end
- #---------------------------------------------------------------
+ @subclasses[arghash[:name]] = klass
+
+ @options.each { |option|
+ method = option.id2name + "="
+ if klass.respond_to?(method)
+ #Blink.debug "Setting %s on %s to '%s'" % [option,klass,arghash[option]]
+ klass.send(method,arghash[option])
+ else
+ Blink.debug "%s does not respond to %s" % [klass,method]
+ end
+ }
+
+ # i couldn't get the method definition stuff to work
+ # oh well
+ # probably wouldn't want it in the end anyway
+ #@options.each { |option|
+ # writer = option.id2name + "="
+ # readproc = proc { eval("@" + option.id2name) }
+ # klass.send(:define_method,option,readproc)
+ # writeproc = proc { |value| module_eval("@" + option.id2name) = value }
+ # klass.send(:define_method,writer,writeproc)
+ # klass.send(writer,hash[option])
+ #}
+
+ return klass
end
#---------------------------------------------------------------
end
+#---------------------------------------------------------------
diff --git a/lib/blink/type/typegen/filerecord.rb b/lib/blink/type/typegen/filerecord.rb
new file mode 100644
index 000000000..b7a432470
--- /dev/null
+++ b/lib/blink/type/typegen/filerecord.rb
@@ -0,0 +1,144 @@
+#!/usr/local/bin/ruby -w
+
+# $Id$
+
+# parse and write configuration files using objects with minimal parsing abilities
+
+require 'etc'
+require 'blink/type'
+require 'blink/type/typegen'
+
+#---------------------------------------------------------------
+class Blink::Type::FileRecord < Blink::Type::TypeGenerator
+ attr_accessor :fields, :namevar, :splitchar, :object
+
+ @options = [:name, :splitchar, :fields, :namevar]
+ @abstract = true
+
+ @name = :filerecord
+
+ #---------------------------------------------------------------
+ #def FileRecord.newtype(hash)
+ # klass = super(hash)
+ # return klass
+ #end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.fields=(ary)
+ @fields = ary
+ Blink.debug "fields are '%s'" % @fields
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.fields
+ return @fields
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.namevar=(field)
+ @namevar = field
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.namevar
+ return @namevar
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.regex
+ return @regex
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.splitchar=(char)
+ @splitchar = char
+ @regex = %r{#{char}}
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileRecord.splitchar
+ return @splitchar
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def [](field)
+ @fields[field]
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def []=(field,value)
+ @fields[field] = value
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def ==(other)
+ unless self.class == other.class
+ return false
+ end
+
+ unless self.name == other.name
+ return false
+ end
+ @fields.keys { |field|
+ unless self[field] == other[field]
+ Blink.debug("%s -> %s has changed" % [self.name, field])
+ return false
+ end
+ }
+ return true
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def initialize(object)
+ @object = object
+ @fields = {}
+ if block_given?
+ yield self
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def record=(record)
+ ary = record.split(self.class.regex)
+ self.class.fields.each { |field|
+ @fields[field] = ary.shift
+ #puts "%s => %s" % [field,@fields[field]]
+ }
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def name
+ if @fields.include?(self.class.namevar)
+ return @fields[self.class.namevar]
+ else
+ raise "No namevar for objects of type %s" % self.class.to_s
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def to_s
+ ary = self.class.fields.collect { |field|
+ if ! @fields.include?(field)
+ raise "Object %s is missing field %s" % [self.name,field]
+ else
+ @fields[field]
+ end
+ }.join(self.class.splitchar)
+ end
+ #---------------------------------------------------------------
+end
+#---------------------------------------------------------------
diff --git a/lib/blink/type/typegen/filetype.rb b/lib/blink/type/typegen/filetype.rb
new file mode 100644
index 000000000..c83796e0f
--- /dev/null
+++ b/lib/blink/type/typegen/filetype.rb
@@ -0,0 +1,231 @@
+#!/usr/local/bin/ruby -w
+
+# $Id$
+
+# parse and write configuration files using objects with minimal parsing abilities
+
+require 'blink/type'
+require 'blink/type/typegen'
+
+class Blink::Type::FileType < Blink::Type::TypeGenerator
+ attr_accessor :childtype
+
+ @options = [:name, :linesplit, :recordsplit, :fields, :namevar]
+ @abstract = true
+
+ @name = :filetype
+
+ #---------------------------------------------------------------
+ def FileType.newtype(hash)
+ unless hash.include?(:linesplit)
+ hash[:linesplit] = "\n"
+ end
+ klass = super(hash)
+
+ klass.childtype = Blink::Type::FileRecord.newtype(
+ :name => hash[:name] + "_record",
+ :splitchar => hash[:recordsplit],
+ :fields => hash[:fields],
+ :namevar => hash[:namevar]
+ )
+ #klass.addrecord(
+ # :name => hash[:name] + "_record",
+ # :splitchar => hash[:recordsplit],
+ # :fields => hash[:fields],
+ # :namevar => hash[:namevar]
+ #)
+
+ return klass
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # currently not used
+ def FileType.addrecord(hash)
+ unless defined? @records
+ @records = {}
+ end
+ @records[hash[:name]] = Blink::Type::FileRecord.newtype(hash)
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileType.childtype
+ unless defined? @childtype
+ @childtype = nil
+ end
+ return @childtype
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileType.childtype=(childtype)
+ @childtype = childtype
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileType.regex
+ return @regex
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileType.linesplit=(linesplit)
+ @regex = %r{#{linesplit}}
+ @linesplit = linesplit
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def FileType.linesplit
+ return @linesplit
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def [](name)
+ return @childhash[name]
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def []=(name,value)
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # we don't really have a 'less-than/greater-than' sense here
+ # so i'm sticking with 'equals' until those make sense
+ def ==(other)
+ unless self.children.length == other.children.length
+ Blink.debug("file has %s records instead of %s" %
+ [self.children.length, other.children.length])
+ return self.children.length == other.children.length
+ end
+ equal = true
+ self.zip(other.children) { |schild,ochild|
+ unless schild == ochild
+ Blink.debug("%s has changed in %s" %
+ [schild.name,self.name])
+ equal = false
+ break
+ end
+ }
+
+ return equal
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # create a new record with a block
+ def add(&block)
+ obj = self.class.childtype.new(self,&block)
+ Blink.debug("adding %s" % obj.name)
+ @childary.push(obj)
+ @childhash[obj.name] = obj
+
+ return obj
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def children
+ return @childary
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # remove a record
+ def delete(name)
+ if @childhash.has_key?(name)
+ child = @childhash[name]
+
+ @childhash.delete(child)
+ @childary.delete(child)
+ else
+ raise "No such entry %s" % name
+ end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def each
+ @childary.each { |child|
+ yield child
+ }
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # create a new file
+ def initialize(file)
+ @file = file
+
+ @childary = []
+ @childhash = {}
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # this is where we're pretty different from other objects
+ # we can choose to either reparse the existing file and compare
+ # the objects, or we can write our file out and do an
+ # text comparison
+ def insync?
+ tmp = self.class.new(@file)
+ tmp.retrieve
+
+ return self == tmp
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ # read the whole file in and turn it into each of the appropriate
+ # objects
+ def retrieve
+ str = ""
+ ::File.open(@file) { |fname|
+ fname.each { |line|
+ str += line
+ }
+ }
+
+ @childary = str.split(self.class.regex).collect { |record|
+ child = self.class.childtype.new(self)
+ child.record = record
+ #puts "adding child %s" % child.name
+ child
+ }
+
+ @childary.each { |child|
+ @childhash[child.name] = child
+ }
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def sync
+ #unless self.insync?
+ self.write
+ #end
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def to_s
+ return @childary.collect { |child|
+ child.to_s
+ }.join(self.class.linesplit) + self.class.linesplit
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
+ def write
+ ::File.open(@file, "w") { |file|
+ file.write(self.to_s)
+ }
+ end
+ #---------------------------------------------------------------
+end
+#---------------------------------------------------------------
diff --git a/test/types/tc_filetype.rb b/test/types/tc_filetype.rb
index 59d5eb946..5bd8aa415 100644
--- a/test/types/tc_filetype.rb
+++ b/test/types/tc_filetype.rb
@@ -5,8 +5,8 @@ if __FILE__ == $0
end
require 'blink'
-require 'blink/type'
-require 'blink/type/filetype'
+require 'blink/type/typegen/filetype'
+require 'blink/type/typegen/filerecord'
require 'test/unit'
# $Id$
@@ -15,10 +15,10 @@ class TestFileType < Test::Unit::TestCase
def setup
Blink[:debug] = 1
- @passwdtype = Blink::FileType["passwd"]
+ @passwdtype = Blink::Type::FileType["passwd"]
if @passwdtype.nil?
assert_nothing_raised() {
- @passwdtype = Blink::FileType.newtype(
+ @passwdtype = Blink::Type::FileType.newtype(
:name => "passwd",
:recordsplit => ":",
:fields => %w{name password uid gid gcos home shell},
@@ -41,7 +41,7 @@ class TestFileType < Test::Unit::TestCase
assert(file.insync?)
contents = ""
- File.open("/etc/passwd") { |ofile|
+ ::File.open("/etc/passwd") { |ofile|
ofile.each { |line|
contents += line
}