Class: FormObj::Struct::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/form_obj/struct/attributes.rb

Direct Known Subclasses

Form::Attributes

Instance Method Summary collapse

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