Class: Heartml::Fragment
- Inherits:
-
Object
- Object
- Heartml::Fragment
- Defined in:
- lib/heartml/fragment.rb
Instance Method Summary collapse
- #fragamatize(node_set) ⇒ Object
-
#initialize(fragment, component) ⇒ Fragment
constructor
A new instance of Fragment.
-
#process(fragment = @fragment) ⇒ Object
rubocop:disable Metrics.
-
#process_attribute_bindings(node) ⇒ Object
rubocop:todo Metrics.
-
#traverse(node) {|node| ... } ⇒ Object
NOTE: for some reason, the traverse method yields node children first, then the parent node.
Constructor Details
#initialize(fragment, component) ⇒ Fragment
Returns a new instance of Fragment.
5 6 7 8 9 10 11 |
# File 'lib/heartml/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
#fragamatize(node_set) ⇒ Object
69 70 71 72 73 |
# File 'lib/heartml/fragment.rb', line 69 def fragamatize(node_set) frag = Nokolexbor::DocumentFragment.new(@fragment.document) node_set.each { |child| child.parent = frag } frag end |
#process(fragment = @fragment) ⇒ Object
rubocop:disable Metrics
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/heartml/fragment.rb', line 21 def process(fragment = @fragment) # rubocop:disable Metrics traverse(fragment) do |node| # rubocop:disable Metrics/BlockLength process_attribute_bindings(node) component = Heartml.registered_elements.find { _1.tag_name == node.name } if component attrs = node.attributes.dup new_attrs = {} attrs.each do |k, attr| unless k == "server-args" new_attrs[k] = attr.value next end v = attr.value params = v.split(";").map(&:strip) params.each do |param| new_key, v2 = param.split(":").map(&:strip) v2 ||= new_key new_attrs[new_key] = @component.evaluate_attribute_expression(attr, v2) end attrs.delete(k) end attrs.merge!(new_attrs) attrs.reject! { |k| k.start_with?("server-") || k.start_with?("iso-") } attrs.transform_keys! { _1.tr("-", "_").to_sym } obj = component.new(**attrs) render_output = if obj.respond_to?(:render_in) obj.render_in(@component.context, rendering_mode: :node) do process(fragamatize(node.children)) end else obj.render_element( content: process(fragamatize(node.children)), context: @component.context ) end node.replace(render_output) end end fragment end |
#process_attribute_bindings(node) ⇒ Object
rubocop:todo Metrics
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/heartml/fragment.rb', line 75 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:) end rescue Exception => e # rubocop:disable Lint/RescueException line_segments = [@component.class.heart_module, @component._line_number_of_node(attr_node)] raise e.class, e..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.
16 17 18 19 |
# File 'lib/heartml/fragment.rb', line 16 def traverse(node, &block) yield(node) node.children.each { |child| traverse(child, &block) } end |