Class: ActionComponent::ElementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/action_component/element_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, element_name = nil, classes = nil, id = nil) ⇒ ElementBuilder

Returns a new instance of ElementBuilder.



3
4
5
6
7
8
# File 'lib/action_component/element_builder.rb', line 3

def initialize(base, element_name = nil, classes = nil, id = nil)
  @base = base
  @element_name = element_name
  @classes = classes
  @id = id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_component/element_builder.rb', line 10

def method_missing(method, *args, &block)
  element_name = @element_name
  classes = @classes
  id = @id

  command = method.to_s

  command = command[-1] == '?' ? command[0..-2] : command.gsub('_', '-')

  if element_name
    if command[-1] == '!'
      id = command[0..-2]
    else
      classes = classes ? "#{classes} #{command}" : command
    end
  else
    element_name = command
  end

  if !args.empty? || block
    first, second = args

    if second.is_a?(Hash)
      build_options(classes, id, second)
    elsif first.is_a?(Hash)
      build_options(classes, id, first)
    elsif first
      second = build_options(classes, id)
    else
      first = build_options(classes, id)
    end

    @base.send(:element, element_name, first, second, &block)
  else
    self.class.new(@base, element_name, classes, id)
  end
end