Class: HoTModuLe::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/hot_module/fragment.rb

Instance Method Summary collapse

Constructor Details

#initialize(fragment, component) ⇒ Fragment

Returns a new instance of Fragment.



5
6
7
8
9
10
11
# File 'lib/hot_module/fragment.rb', line 5

def initialize(fragment, component)
  @fragment = fragment
  @component = component
  @attribute_bindings = component.class.attribute_bindings.each do |attr_def|
    attr_def.method = component.method(attr_def.method_name)
  end
end

Instance Method Details

#process(fragment = @fragment) ⇒ Object



21
22
23
24
25
# File 'lib/hot_module/fragment.rb', line 21

def process(fragment = @fragment)
  traverse(fragment) do |node|
    process_attribute_bindings(node)
  end
end

#process_attribute_bindings(node) ⇒ Object

rubocop:todo Metrics



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hot_module/fragment.rb', line 27

def process_attribute_bindings(node) # rubocop:todo Metrics
  node.attributes.each do |name, attr_node|
    @attribute_bindings.each do |attribute_binding|
      next if attribute_binding.only_for_tag && node.name != attribute_binding.only_for_tag.to_s
      next unless attribute_binding.matcher.match?(name)
      next if attribute_binding.method.receiver._check_stack(node)

      break unless attribute_binding.method.(attribute: attr_node, node: node)
    end
  rescue Exception => e # rubocop:disable Lint/RescueException
    line_segments = [@component.class.html_module, @component.class.line_number_of_node(attr_node)]
    raise e.class, e.message.lines.first, [line_segments.join(":"), *e.backtrace]
  end
end

#traverse(node) {|node| ... } ⇒ Object

NOTE: for some reason, the traverse method yields node children first, then the parent node. That doesn’t work for our case. We want to go strictly in source order. So this is our own implementation of that.

Yields:

  • (node)


16
17
18
19
# File 'lib/hot_module/fragment.rb', line 16

def traverse(node, &block)
  yield(node)
  node.children.each { |child| traverse(child, &block) }
end