Array instances are extened with two methods only: Fieldable#fields= and Fieldable#fields. only when Fieldable#fields= is called will the full set of ArrayFields methods auto-extend the Array instance. the Array class also has added a class generator when the fields are known apriori.
# File lib/arrayfields.rb, line 370 def self.[] *elements array = new array.replace elements array end
# File lib/arrayfields.rb, line 377 def fields *fields, &block (( array = new(&block) )).fields = fields.map{|x| Enumerable === x ? x.to_a : x}.flatten array end
# File lib/arrayfields.rb, line 365 def initialize *a, &b super ensure @fieldset = self.class.const_get :FIELDS end
# File lib/arrayfields.rb, line 343 def struct *fields fields = fields.flatten Class.new(self) do include ArrayFields const_set :FIELDS, ArrayFields::FieldSet.new(fields) fields.each do |field| field = field.to_s if field =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ begin module_eval def #{ field } *a a.size == 0 ? self['#{ field }'] : (self.#{ field } = a.shift) end def #{ field }= value self['#{ field }'] = value end rescue SyntaxError :by_ignoring_it end end end def initialize *a, &b super ensure @fieldset = self.class.const_get :FIELDS end def self.[] *elements array = new array.replace elements array end end end
Generated with the Darkfish Rdoc Generator 2.