Module: Curlybars

Defined in:
lib/curlybars.rb,
lib/curlybars/lexer.rb,
lib/curlybars/parser.rb,
lib/curlybars/generic.rb,
lib/curlybars/railtie.rb,
lib/curlybars/version.rb,
lib/curlybars/visitor.rb,
lib/curlybars/position.rb,
lib/curlybars/error/lex.rb,
lib/curlybars/node/item.rb,
lib/curlybars/node/path.rb,
lib/curlybars/node/root.rb,
lib/curlybars/node/text.rb,
lib/curlybars/presenter.rb,
lib/curlybars/error/base.rb,
lib/curlybars/error/parse.rb,
lib/curlybars/node/option.rb,
lib/curlybars/node/output.rb,
lib/curlybars/node/string.rb,
lib/curlybars/path_finder.rb,
lib/curlybars/safe_buffer.rb,
lib/curlybars/error/render.rb,
lib/curlybars/node/boolean.rb,
lib/curlybars/node/if_else.rb,
lib/curlybars/node/literal.rb,
lib/curlybars/node/partial.rb,
lib/curlybars/configuration.rb,
lib/curlybars/error/compile.rb,
lib/curlybars/node/template.rb,
lib/curlybars/node/variable.rb,
lib/curlybars/error/validate.rb,
lib/curlybars/node/each_else.rb,
lib/curlybars/node/with_else.rb,
lib/curlybars/processor/tilde.rb,
lib/curlybars/method_whitelist.rb,
lib/curlybars/node/unless_else.rb,
lib/curlybars/template_handler.rb,
lib/curlybars/partial_presenter.rb,
lib/curlybars/rendering_support.rb,
lib/curlybars/dependency_tracker.rb,
lib/curlybars/validation_context.rb,
lib/curlybars/node/sub_expression.rb,
lib/curlybars/node/block_helper_else.rb,
lib/curlybars/processor/token_factory.rb,
lib/curlybars/error/presenter/not_found.rb

Overview

rubocop:disable Style/RegexpLiteral, Style/Semicolon

Defined Under Namespace

Modules: Error, MethodWhitelist, Node, Processor Classes: Configuration, DependencyTracker, Generic, Lexer, Parser, PartialPresenter, PathFinder, Position, Presenter, Railtie, RenderingSupport, SafeBuffer, TemplateHandler, ValidationContext, Visitor

Constant Summary collapse

VERSION =
'1.16.1.pre.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject



114
115
116
# File 'lib/curlybars.rb', line 114

def cache
  @cache ||= ActiveSupport::Cache::MemoryStore.new
end

.configurationObject



6
7
8
# File 'lib/curlybars/configuration.rb', line 6

def self.configuration
  @configuration ||= Configuration.new
end

Class Method Details

.compile(source, identifier = nil) ⇒ Object

Compiles a Curlybars template to Ruby code.

source - The source HBS String that should be compiled. identifier - The the file name of the template being compiled (defaults to ‘nil`).

Returns a String containing the Ruby code.



28
29
30
31
32
33
34
# File 'lib/curlybars.rb', line 28

def compile(source, identifier = nil)
  cache_key = ["Curlybars.compile", Curlybars::VERSION, identifier, Digest::SHA256.hexdigest(source)]

  cache.fetch(cache_key) do
    ast(transformed_source(source), identifier, run_processors: true).compile
  end
end

.configure {|configuration| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/curlybars/configuration.rb', line 10

def self.configure
  yield(configuration)
end

.find(target_path, source, identifier = nil, role: nil) ⇒ Object

Find all path nodes in the AST that resolve to a given path. Takes into account contextual scope from block helpers like #with, #each, etc.

target_path - The path String to search for (e.g., “user.name” or “user.organizations.id”). source - The source HBS String used to generate an AST. identifier - The the file name of the template being checked (defaults to ‘nil`). role - Optional Symbol or Array of Symbols to filter by syntactic role:

  :output     - bare output value         {{title}}
  :helper     - helper name               {{truncate …}}, {{#block …}}
  :argument   - positional argument        {{truncate title}}, (math score)
  :option     - keyword option value       key=title
  :condition  - #if / #unless expression   {{#if visible}}
  :collection - #each collection path      {{#each posts}}
  :scope      - #with scope path           {{#with author}}
  :partial    - partial name               {{> header}}
When nil (default), all roles match (backward compatible).

Returns an Array of Curlybars::Node::Path instances that match the target path.



101
102
103
104
105
# File 'lib/curlybars.rb', line 101

def find(target_path, source, identifier = nil, role: nil)
  tree = ast(transformed_source(source), identifier, run_processors: true)
  finder = Curlybars::PathFinder.new(tree)
  finder.find(target_path, role: role)
end

.global_helpers_dependency_treeObject



107
108
109
110
111
112
# File 'lib/curlybars.rb', line 107

def global_helpers_dependency_tree
  @global_helpers_dependency_tree ||= begin
    classes = Curlybars.configuration.global_helpers_provider_classes
    classes.map(&:dependency_tree).inject({}, :merge)
  end
end

.resetObject



14
15
16
# File 'lib/curlybars/configuration.rb', line 14

def self.reset
  @configuration = Configuration.new
end

.valid?(presenter_class, source, identifier = nil) ⇒ Boolean

Check if the source is valid for a given presenter.

presenter_class - the presenter class, used to check if the source is valid. source - The source HBS String that should be check to be valid. identifier - The the file name of the template being checked (defaults to ‘nil`).

Returns true if the template is valid, false otherwise.

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/curlybars.rb', line 68

def valid?(presenter_class, source, identifier = nil, **)
  errors = validate(presenter_class, source, identifier, **)
  errors.empty?
end

.validate(dependency_tree, source, identifier = nil, partial_resolver: nil, validation_context: nil, **options) ⇒ Object

Validates the source against a presenter.

dependency_tree - a presenter dependency tree as defined in Curlybars::MethodWhitelist source - The source HBS String that should be validated. identifier - The the file name of the template being validated (defaults to ‘nil`).

Returns an array of Curlybars::Error::Validation



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/curlybars.rb', line 43

def validate(dependency_tree, source, identifier = nil, partial_resolver: nil, validation_context: nil, **options)
  options.reverse_merge!(
    run_processors: true
  )

  validation_context ||= ValidationContext.new(partial_resolver: partial_resolver) if partial_resolver

  errors = begin
    branches = [dependency_tree]
    ast(source, identifier, run_processors: options[:run_processors]).validate(branches, context: validation_context)
  rescue Curlybars::Error::Base => ast_error
    [ast_error]
  end
  errors.flatten!
  errors.compact!
  errors
end

.visit(visitor, source, identifier = nil) ⇒ Object

Visit nodes in the AST.

visitor - An instance of a subclass of ‘Curlybars::Visitor`. source - The source HBS String used to generate an AST. identifier - The the file name of the template being checked (defaults to `nil`).



78
79
80
81
# File 'lib/curlybars.rb', line 78

def visit(visitor, source, identifier = nil)
  tree = ast(transformed_source(source), identifier, run_processors: true)
  visitor.accept(tree)
end