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 361
def self.[] *elements
  array = new
  array.replace elements
  array
end
new(*a, &b) click to toggle source
Calls superclass method Arrayfields.new
# File lib/arrayfields.rb, line 356
def initialize *a, &b
  super
ensure
  @fieldset = self.class.const_get :FIELDS
end

Public Instance Methods

fields(*fields, &block) click to toggle source
# File lib/arrayfields.rb, line 368
def fields *fields, &block
  (( array = new(&block) )).fields = fields.map{|x| Enumerable === x ? x.to_a : x}.flatten
  array
end
struct(*fields) click to toggle source
# File lib/arrayfields.rb, line 334
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 <<-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