Class: Arbre::Form::Component::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/arbre/form/component/path.rb

Defined Under Namespace

Classes: Member

Instance Method Summary collapse

Constructor Details

#initialize(errors: [], successes: [], &block) ⇒ Path

Returns a new instance of Path.



5
6
7
8
9
10
# File 'lib/arbre/form/component/path.rb', line 5

def initialize(errors: [], successes: [], &block)
  @members = []
  @errors = errors
  @successes = successes
  instance_exec(&block)
end

Instance Method Details

#componentsObject



38
39
40
41
42
43
44
45
46
# File 'lib/arbre/form/component/path.rb', line 38

def components
  @members.map do |member|
    if member.index
      [member.name, member.index]
    else
      [member.name]
    end
  end
end

#errorsObject



48
49
50
# File 'lib/arbre/form/component/path.rb', line 48

def errors
  current_validations(@errors)
end

#id(*attributes) ⇒ Object



34
35
36
# File 'lib/arbre/form/component/path.rb', line 34

def id(*attributes)
  (components + attributes.compact).join('_')
end

#name(*attributes) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/arbre/form/component/path.rb', line 24

def name(*attributes)
  name_components = []
  (components + attributes.compact).each_with_index do |(name, member_index), index|
    next name_components << name if index == 0
    name_components << "[#{name}]"
    name_components << '[]' if member_index
  end
  name_components.join('')
end

#peekObject



20
21
22
# File 'lib/arbre/form/component/path.rb', line 20

def peek
  @members.last
end

#popObject



16
17
18
# File 'lib/arbre/form/component/path.rb', line 16

def pop
  @members.pop
end

#push(member, name, index = nil) ⇒ Object



12
13
14
# File 'lib/arbre/form/component/path.rb', line 12

def push(member, name, index = nil)
  @members << Member.new(member, name, index)
end

#successesObject



52
53
54
# File 'lib/arbre/form/component/path.rb', line 52

def successes
  current_validations(@successes)
end