Class: ORB::Temple::Filters
- Inherits:
-
Temple::Filter
- Object
- Temple::Filter
- ORB::Temple::Filters
- Defined in:
- lib/orb/temple/filters.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Filters
constructor
A new instance of Filters.
-
#on_orb_component(node, content = []) ⇒ Array
Handle a component tag expression ‘[:orb, :component, name, attributes, content]`.
-
#on_orb_dynamic(node, content) ⇒ Object
Handle a dynamic node expression ‘[:orb, :dynamic, node, content]`.
-
#on_orb_for(expression, content) ⇒ Array
Handle a for block expression ‘[:orb, :for, expression, content]`.
-
#on_orb_if(condition, yes, no = nil) ⇒ Array
Handle an if block expression ‘[:orb, :if, condition, yes, no]`.
-
#on_orb_slot(node, content = []) ⇒ Array
Handle a component slot tag expression ‘[:orb, :slot, name, attributes, content]`.
-
#on_orb_tag(name, attributes, content = nil) ⇒ Array
Handle an HTML tag expression ‘[:orb, :tag, name, attributes, content]`.
Constructor Details
#initialize(options = {}) ⇒ Filters
Returns a new instance of Filters.
6 7 8 9 |
# File 'lib/orb/temple/filters.rb', line 6 def initialize( = {}) = @attributes_compiler = AttributesCompiler.new end |
Instance Method Details
#on_orb_component(node, content = []) ⇒ Array
Handle a component tag expression ‘[:orb, :component, name, attributes, content]`
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 |
# File 'lib/orb/temple/filters.rb', line 27 def on_orb_component(node, content = []) tmp = unique_name # Lookup the component class name using the ORB lookup mechanism # that traverses the configured namespaces name = node.tag.gsub('.', '::') komponent = ORB.lookup_component(name) komponent_name = komponent || name block_name = "__orb__#{komponent_name.rpartition('::').last.underscore}" block_name = node.directives.fetch(:with, block_name) # We need to compile the attributes into a set of captures and a set of arguments # since arguments passed to the view component constructor may be defined as # dynamic expressions in our template, and we need to first capture their results. arg_captures = @attributes_compiler.compile_captures(node.attributes, tmp) args = @attributes_compiler.compile_komponent_args(node.attributes, tmp) # Construct the render call for the view component code = "render #{komponent_name}.new(#{args}) do |#{block_name}|" # Return a compiled Temple expression that captures the component render call # and then evaluates the result into the OutputBuffer. [:multi, *arg_captures, # Capture the result of the component render call into a variable # we can't do :dynamic here because it's probably not a complete expression [:block, "#{tmp} = #{code}", # Capture the content of the block into a separate buffer # [:capture, unique_name, compile(content)] compile(content)], # Output the content [:escape, true, [:dynamic, tmp.to_s]]] end |
#on_orb_dynamic(node, content) ⇒ Object
Handle a dynamic node expression ‘[:orb, :dynamic, node, content]`
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/orb/temple/filters.rb', line 120 def on_orb_dynamic(node, content) # TODO: Determine whether the node is an html_tag, component, or slot node tmp = unique_name splats = @attributes_compiler.compile_splat_attributes(node.splat_attributes) code = "content_tag('#{node.tag}', #{splats}) do" [:multi, [:block, "#{tmp} = #{code}", compile(content)], [:escape, true, [:dynamic, tmp]]] end |
#on_orb_for(expression, content) ⇒ Array
Handle a for block expression ‘[:orb, :for, expression, content]`
109 110 111 112 113 114 115 116 |
# File 'lib/orb/temple/filters.rb', line 109 def on_orb_for(expression, content) enumerator, collection = expression.split(' in ') code = "#{collection}.each do |#{enumerator}|" [:multi, [:code, code], compile(content), [:code, "end"]] end |
#on_orb_if(condition, yes, no = nil) ⇒ Array
Handle an if block expression ‘[:orb, :if, condition, yes, no]`
98 99 100 101 102 |
# File 'lib/orb/temple/filters.rb', line 98 def on_orb_if(condition, yes, no = nil) result = [:if, condition, compile(yes)] result << compile(no) if no result end |
#on_orb_slot(node, content = []) ⇒ Array
Handle a component slot tag expression ‘[:orb, :slot, name, attributes, content]`
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/orb/temple/filters.rb', line 68 def on_orb_slot(node, content = []) tmp = unique_name # We need to compile the attributes into a set of captures and a set of arguments # since arguments passed to the view component constructor may be defined as # dynamic expressions in our template, and we need to first capture their results. arg_captures = @attributes_compiler.compile_captures(node.attributes, tmp) args = @attributes_compiler.compile_komponent_args(node.attributes, tmp) # Prepare the slot name, parent name, and block name slot_name = node.slot parent_name = "__orb__#{node.component.underscore}" block_name = node.directives.fetch(:with, "__orb__#{slot_name}") # Construct the code to call the slot on the parent component code = "#{parent_name}.with_#{slot_name}(#{args}) do |#{block_name}|" # Return a compiled Temple expression that captures the slot call [:multi, *arg_captures, [:code, code], compile(content), [:code, "end"]] end |
#on_orb_tag(name, attributes, content = nil) ⇒ Array
Handle an HTML tag expression ‘[:orb, :tag, name, attributes, content]`
17 18 19 |
# File 'lib/orb/temple/filters.rb', line 17 def on_orb_tag(name, attributes, content = nil) [:html, :tag, name, @attributes_compiler.compile_attributes(attributes), compile(content)] end |