Class: FormObj::Struct::Attributes
- Inherits:
-
Object
- Object
- FormObj::Struct::Attributes
- Defined in:
- lib/form_obj/struct/attributes.rb
Direct Known Subclasses
Instance Method Summary collapse
- #add(attribute) ⇒ Object
- #each(&block) ⇒ Object
- #find(name) ⇒ Object
-
#initialize(items = []) ⇒ Attributes
constructor
A new instance of Attributes.
- #map(*args, &block) ⇒ Object
- #reduce(*args, &block) ⇒ Object
Constructor Details
#initialize(items = []) ⇒ Attributes
Returns a new instance of Attributes.
4 5 6 |
# File 'lib/form_obj/struct/attributes.rb', line 4 def initialize(items = []) @items = items end |
Instance Method Details
#add(attribute) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/form_obj/struct/attributes.rb', line 8 def add(attribute) if @items.map(&:name).include? attribute.name self.class.new(@items.map { |item| item.name == attribute.name ? attribute : item }) else self.class.new(@items + [attribute]) end end |
#each(&block) ⇒ Object
16 17 18 |
# File 'lib/form_obj/struct/attributes.rb', line 16 def each(&block) @items.each(&block) end |
#find(name) ⇒ Object
20 21 22 |
# File 'lib/form_obj/struct/attributes.rb', line 20 def find(name) @items.find { |item| item.name == name.to_sym } end |
#map(*args, &block) ⇒ Object
24 25 26 |
# File 'lib/form_obj/struct/attributes.rb', line 24 def map(*args, &block) @items.map(*args, &block) end |
#reduce(*args, &block) ⇒ Object
28 29 30 |
# File 'lib/form_obj/struct/attributes.rb', line 28 def reduce(*args, &block) @items.reduce(*args, &block) end |