Class: Rooftop::Content::Collection
- Inherits:
-
Array
- Object
- Array
- Rooftop::Content::Collection
- Defined in:
- lib/rooftop/content_fields/collection.rb
Instance Method Summary collapse
-
#find_by(hash) ⇒ Object
Find content_fields by attribute.
-
#initialize(content_fields) ⇒ Collection
constructor
A new instance of Collection.
- #named(name) ⇒ Object
Constructor Details
#initialize(content_fields) ⇒ Collection
Returns a new instance of Collection.
4 5 6 7 8 |
# File 'lib/rooftop/content_fields/collection.rb', line 4 def initialize(content_fields) content_fields.each do |field| self << Rooftop::Content::Field.new(field) end end |
Instance Method Details
#find_by(hash) ⇒ Object
Find content_fields by attribute. Assume there will only be one attribute in the search
11 12 13 14 15 16 |
# File 'lib/rooftop/content_fields/collection.rb', line 11 def find_by(hash) raise ArgumentError, "you can only find a field by one attribute at a time" unless hash.length == 1 attr = hash.first.first val = hash.first.last self.select {|l| l.send(attr) == val.to_s} end |
#named(name) ⇒ Object
18 19 20 |
# File 'lib/rooftop/content_fields/collection.rb', line 18 def named(name) find_by(name: name.to_s).first end |