Class: MemoryModel::Base::Fieldable::FieldSet

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_model/base/fieldable/field_set.rb

Constant Summary collapse

Field =
MemoryModel::Base::Fieldable::Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldSet

Returns a new instance of FieldSet.



11
12
13
# File 'lib/memory_model/base/fieldable/field_set.rb', line 11

def initialize
  @fields = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)



66
67
68
69
70
71
72
# File 'lib/memory_model/base/fieldable/field_set.rb', line 66

def method_missing(m, *args, &block)
  if to_a.respond_to? m
    to_a.send m, *args, &block
  else
    super
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/memory_model/base/fieldable/field_set.rb', line 8

def fields
  @fields
end

Instance Method Details

#<<(attr) ⇒ Object



19
20
21
22
# File 'lib/memory_model/base/fieldable/field_set.rb', line 19

def <<(attr)
  attr = Field.new(attr) unless attr.is_a? Field
  @fields << attr
end

#[](name) ⇒ Object



15
16
17
# File 'lib/memory_model/base/fieldable/field_set.rb', line 15

def [](name)
  @fields.find { |f| f.name == name.to_sym }
end

#add(attr, options = { }) ⇒ Object



24
25
26
27
# File 'lib/memory_model/base/fieldable/field_set.rb', line 24

def add(attr, options={ })
  @fields.delete_if { |f| f == attr }
  @fields << Field.new(attr, options)
end

#comparableObject



29
30
31
# File 'lib/memory_model/base/fieldable/field_set.rb', line 29

def comparable
  @fields.select(&:comparable?).map(&:to_sym)
end

#default_values(model, attributes = { }) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/memory_model/base/fieldable/field_set.rb', line 37

def default_values(model, attributes={ })
  @fields.reduce(attributes.symbolize_keys) do |attrs, field|
    raise MemoryModel::ReadonlyFieldError if attrs[field.name].present? && field.readonly?
    default           = field.default.is_a?(Symbol) ? field.default.to_proc : field.default
    attrs[field.name] ||= if default.nil?
                            nil
                          elsif default.is_a? String
                            default
                          elsif default.not_a?(::Proc)
                            raise ArgumentError, "#{default} must be a string, symbol, lamba or proc"
                          elsif default.lambda? && default.arity == 0
                            default.call
                          elsif default.arity.in? -1..0
                            model.instance_eval(&default)
                          elsif default.arity == 1
                            default.yield model
                          else
                            raise ArgumentError, "#{default} must have an arity of 0..1, got #{default.arity}"
                          end
    attrs
  end
end

#inspectObject



33
34
35
# File 'lib/memory_model/base/fieldable/field_set.rb', line 33

def inspect
  to_a.inspect
end

#to_aObject



60
61
62
# File 'lib/memory_model/base/fieldable/field_set.rb', line 60

def to_a
  @fields.map(&:to_sym)
end