Class: Liquid::Assign

Inherits:
Tag
  • Object
show all
Includes:
ParserSwitching
Defined in:
lib/liquid/tags/assign.rb

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/(#{VariableSignature}+)\s*=\s*(.*)\s*/om

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #parse_context, #tag_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParserSwitching

#parse_with_selected_parser, #strict2_mode?, #strict_parse_with_error_mode_fallback

Methods inherited from Tag

disable_tags, #name, parse, #parse, #raw, #render

Constructor Details

#initialize(tag_name, markup, parse_context) ⇒ Assign

Returns a new instance of Assign.



32
33
34
35
# File 'lib/liquid/tags/assign.rb', line 32

def initialize(tag_name, markup, parse_context)
  super
  parse_with_selected_parser(markup)
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



30
31
32
# File 'lib/liquid/tags/assign.rb', line 30

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



30
31
32
# File 'lib/liquid/tags/assign.rb', line 30

def to
  @to
end

Class Method Details

.raise_syntax_error(parse_context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/liquid/tags/assign.rb', line 26

def self.raise_syntax_error(parse_context)
  raise Liquid::SyntaxError, parse_context.locale.t('errors.syntax.assign')
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/liquid/tags/assign.rb', line 72

def blank?
  true
end

#lax_parse(markup) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/liquid/tags/assign.rb', line 37

def lax_parse(markup)
  if markup =~ Syntax
    @to   = Regexp.last_match(1)
    @from = Variable.new(Regexp.last_match(2), parse_context)
  else
    self.class.raise_syntax_error(parse_context)
  end
end

#render_to_output_buffer(context, output) ⇒ Object



65
66
67
68
69
70
# File 'lib/liquid/tags/assign.rb', line 65

def render_to_output_buffer(context, output)
  val = @from.render(context)
  context.scopes.last[@to] = val
  context.resource_limits.increment_assign_score(assign_score_of(val))
  output
end

#strict2_parse(markup) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/tags/assign.rb', line 50

def strict2_parse(markup)
  unless markup =~ Syntax
    self.class.raise_syntax_error(parse_context)
  end

  lhs = Regexp.last_match(1).strip
  rhs = Regexp.last_match(2)

  p = @parse_context.new_parser(lhs)
  @to = p.consume(:id)
  p.consume(:end_of_string)

  @from = Variable.new(rhs, parse_context)
end

#strict_parse(markup) ⇒ Object



46
47
48
# File 'lib/liquid/tags/assign.rb', line 46

def strict_parse(markup)
  lax_parse(markup)
end