Class: Luoma::AssignTag

Inherits:
Markup
  • Object
show all
Defined in:
lib/luoma/tags/assign.rb,
sig/luoma/tags/assign.rbs

Instance Attribute Summary collapse

Attributes inherited from Markup

#blank, #tag_name, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Markup

#block_scope, #children, #partial

Constructor Details

#initialize(token, tag_name, bindings) ⇒ AssignTag

(t_token, Array) -> void

Parameters:

  • token (t_token)
  • tag_name (String)
  • (Array[Item])


43
44
45
46
47
48
# File 'lib/luoma/tags/assign.rb', line 43

def initialize(token, tag_name, bindings)
  super(token)
  @blank = true
  @tag_name = tag_name
  @bindings = bindings
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



5
6
7
# File 'lib/luoma/tags/assign.rb', line 5

def bindings
  @bindings
end

Class Method Details

.parse(token, tag_name, parser) ⇒ Markup

(t_token, String, Parser) -> Markup

Parameters:

  • token (t_token)
  • tag_name (String)
  • parser (Parser)

Returns:



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
38
39
40
# File 'lib/luoma/tags/assign.rb', line 8

def self.parse(token, tag_name, parser)
  bindings = [] #: Array[Item]

  name = parser.parse_ident
  assign_token = parser.eat(
    :token_assign,
    message: "bad identifier or missing assignment operator"
  )

  bindings << Item.new(assign_token, name, parser.parse_expression)

  until parser.kind != :token_comma
    parser.next

    # Trailing commas are OK.
    break unless parser.kind == :token_ident

    name = parser.parse_ident
    assign_token = parser.eat(
      :token_assign,
      message: "bad identifier or missing assignment operator"
    )

    bindings << Item.new(assign_token, name, parser.parse_expression)
  end

  parser.carry_whitespace_control
  parser.eat(
    :token_tag_end,
    message: "bad expression or missing markup delimiter"
  )
  new(token, tag_name, bindings)
end

Instance Method Details

#expressionsObject

Signature:

  • () -> Array[Expression]



58
59
60
# File 'lib/luoma/tags/assign.rb', line 58

def expressions
  @bindings.map(&:expr)
end

#render(context, buffer) ⇒ Object

Signature:

  • (RenderContext, String) -> void



51
52
53
54
55
# File 'lib/luoma/tags/assign.rb', line 51

def render(context, buffer)
  @bindings.each do |binding|
    context.assign(binding.key.value, binding.expr.evaluate(context))
  end
end

#template_scopeObject

Signature:

  • () -> Array[Name]



63
64
65
# File 'lib/luoma/tags/assign.rb', line 63

def template_scope
  @bindings.map(&:key)
end