Sha256: 30b654147df6b2dc7fe4a3f49c1450a3aca3dff0cdb3f173f3243dc824d65f9c

Contents?: true

Size: 604 Bytes

Versions: 5

Compression:

Stored size: 604 Bytes

Contents

class Presenting::FieldSet < Array
  def initialize(field_class, primary, secondary)
    @klass = field_class
    @primary_attribute = primary
    @secondary_attribute = secondary
  end

  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
  
  def [](key)
    detect{|i| i.send(@primary_attribute) == key}
  end
  
  def []=(key, val)
    self << {key => val}
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
presenting-3.0.0 lib/presenting/field_set.rb
presenting-2.1.0 lib/presenting/field_set.rb
presenting-2.0.3 lib/presenting/field_set.rb
presenting-2.0.2 lib/presenting/field_set.rb
presenting-2.0.1 lib/presenting/field_set.rb