Class: Curlybars::Node::EachElse
- Inherits:
-
Struct
- Object
- Struct
- Curlybars::Node::EachElse
- Defined in:
- lib/curlybars/node/each_else.rb
Instance Attribute Summary collapse
-
#each_template ⇒ Object
Returns the value of attribute each_template.
-
#else_template ⇒ Object
Returns the value of attribute else_template.
-
#path ⇒ Object
Returns the value of attribute path.
-
#position ⇒ Object
Returns the value of attribute position.
Instance Method Summary collapse
Instance Attribute Details
#each_template ⇒ Object
Returns the value of attribute each_template
3 4 5 |
# File 'lib/curlybars/node/each_else.rb', line 3 def each_template @each_template end |
#else_template ⇒ Object
Returns the value of attribute else_template
3 4 5 |
# File 'lib/curlybars/node/each_else.rb', line 3 def else_template @else_template end |
#path ⇒ Object
Returns the value of attribute path
3 4 5 |
# File 'lib/curlybars/node/each_else.rb', line 3 def path @path end |
#position ⇒ Object
Returns the value of attribute position
3 4 5 |
# File 'lib/curlybars/node/each_else.rb', line 3 def position @position end |
Instance Method Details
#cache_key ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/curlybars/node/each_else.rb', line 64 def cache_key [ path, each_template, else_template ].map(&:cache_key).push(self.class.name).join("/") end |
#collection_path ⇒ Object
60 61 62 |
# File 'lib/curlybars/node/each_else.rb', line 60 def collection_path path.subexpression? ? path.helper : path end |
#compile ⇒ Object
4 5 6 7 8 9 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 |
# File 'lib/curlybars/node/each_else.rb', line 4 def compile # NOTE: the following is a heredoc string, representing the ruby code fragment # outputted by this node. " collection = rendering.cached_call(\#{path.compile})\n\n if rendering.to_bool(collection)\n position = rendering.position(\#{position.line_number}, \#{position.line_offset})\n template_cache_key = '\#{each_template.cache_key}'\n\n collection = rendering.coerce_to_hash!(collection, \#{collection_path.path.inspect}, position)\n collection.each.with_index.map do |key_and_presenter, index|\n rendering.check_timeout!\n rendering.optional_presenter_cache(key_and_presenter[1], template_cache_key, buffer) do |buffer|\n begin\n contexts.push(key_and_presenter[1])\n variables.push({\n index: index,\n key: key_and_presenter[0],\n first: index == 0,\n last: index == (collection.length - 1),\n })\n \#{each_template.compile}\n ensure\n variables.pop\n contexts.pop\n end\n end\n end\n else\n \#{else_template.compile}\n end\n RUBY\nend\n" |
#validate(branches) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/curlybars/node/each_else.rb', line 39 def validate(branches) resolved = path.resolve_and_check!(branches, check_type: :collectionlike) sub_tree = resolved.first each_template_errors = begin branches.push(sub_tree) each_template.validate(branches) ensure branches.pop end else_template_errors = else_template.validate(branches) [ each_template_errors, else_template_errors ] rescue ::Error::Validate => path_error path_error end |