Module: Curlybars

Defined in:
lib/curlybars.rb,
lib/curlybars/lexer.rb,
lib/curlybars/parser.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/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/rendering_support.rb,
lib/curlybars/dependency_tracker.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, Lexer, Parser, Position, Presenter, Railtie, RenderingSupport, SafeBuffer, TemplateHandler, Visitor

Constant Summary collapse

VERSION =
'1.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject



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

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.



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

def compile(source, identifier = nil)
  cache_key = ["Curlybars.compile", 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

.resetObject



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

def self.reset
  @configuration = Configuration.new
end

.valid?(presenter_class, source, identifier = nil, **options) ⇒ 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)


65
66
67
68
# File 'lib/curlybars.rb', line 65

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

.validate(dependency_tree, source, identifier = 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



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

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

  errors = begin
    branches = [dependency_tree]
    ast(source, identifier, run_processors: options[:run_processors]).validate(branches)
  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`).



75
76
77
78
# File 'lib/curlybars.rb', line 75

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