Class: Presenting::FieldSet
- Inherits:
-
Array
- Object
- Array
- Presenting::FieldSet
- Defined in:
- lib/presenting/field_set.rb
Instance Method Summary collapse
- #<<(field) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#initialize(field_class, primary, secondary) ⇒ FieldSet
constructor
A new instance of FieldSet.
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 |