Class: Rjq::AST::Format

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

Methods inherited from Node

#assign_value, #invalid_path_message, #paths, #take, #with_source_span

Constructor Details

#initialize(name, expression = nil) ⇒ Format

Returns a new instance of Format.



238
239
240
241
# File 'lib/rjq/ast.rb', line 238

def initialize(name, expression = nil)
  @name = name
  @expression = expression
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



236
237
238
# File 'lib/rjq/ast.rb', line 236

def expression
  @expression
end

#nameObject (readonly)

Returns the value of attribute name.



236
237
238
# File 'lib/rjq/ast.rb', line 236

def name
  @name
end

Instance Method Details

#eval(input, context) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rjq/ast.rb', line 243

def eval(input, context)
  return Builtins.call(@name, input, context, []) unless @expression

  if @expression.is_a?(StringLiteral) && @expression.value.is_a?(Array)
    return @expression.value.reduce(['']) do |prefixes, (kind, value)|
      suffixes = format_segment(kind, value, input, context)
      prefixes.flat_map { |prefix| suffixes.map { |suffix| prefix + suffix } }
    end
  end

  @expression.eval(input, context).flat_map { |value| Builtins.call(@name, value, context, []) }
end