Class: Presenting::FieldSet

Inherits:
Array
  • Object
show all
Defined in:
lib/presenting/field_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(field_class, primary, secondary) ⇒ FieldSet

Returns a new instance of FieldSet.



2
3
4
5
6
# File 'lib/presenting/field_set.rb', line 2

def initialize(field_class, primary, secondary)
  @klass = field_class
  @primary_attribute = primary
  @secondary_attribute = secondary
end

Instance Method Details

#<<(field) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/presenting/field_set.rb', line 8

def <<(field)
  if field.is_a? Hash
    k, v = *field.to_a.first
    opts = v.is_a?(Hash) ? v : {@secondary_attribute => v}
    opts[@primary_attribute] = k
  else
    opts = {@primary_attribute => field}
  end
  super @klass.new(opts)
end

#[](key) ⇒ Object



19
20
21
# File 'lib/presenting/field_set.rb', line 19

def [](key)
  detect{|i| i.send(@primary_attribute) == key}
end

#[]=(key, val) ⇒ Object



23
24
25
# File 'lib/presenting/field_set.rb', line 23

def []=(key, val)
  self << {key => val}
end