class Array
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.
Public Class Methods
[](*elements)
click to toggle source
# File lib/arrayfields.rb, line 374 def self.[] *elements array = new array.replace elements array end
fields(*fields, &block)
click to toggle source
# File lib/arrayfields.rb, line 381 def fields *fields, &block (( array = new(&block) )).fields = fields.map{|x| Enumerable === x ? x.to_a : x}.flatten array end
initialize(*a, &b)
click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 369 def initialize *a, &b super ensure @fieldset = self.class.const_get :FIELDS end
struct(*fields)
click to toggle source
# File lib/arrayfields.rb, line 347 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 =~ %r/^[a-zA-Z_][a-zA-Z0-9_]*$/ begin module_eval <<-code def #{ field } *a a.size == 0 ? self['#{ field }'] : (self.#{ field } = a.shift) end def #{ field }= value self['#{ field }'] = value end code 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