Class: Estreet::Literal

Inherits:
Expression show all
Defined in:
lib/estreet/literal.rb

Instance Attribute Summary

Attributes inherited from Node

#source_location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#[], #call, coerce, #property, #to_expression, #to_statement

Methods inherited from Node

#as_json, #loc, #type

Constructor Details

#initialize(value) ⇒ Literal

Returns a new instance of Literal.



3
4
5
6
# File 'lib/estreet/literal.rb', line 3

def initialize(value)
  # TODO: Regexp? I guess?
  @value = value
end

Class Method Details

.[](value) ⇒ Object



27
28
29
# File 'lib/estreet/literal.rb', line 27

def self.[](value)
  from_ruby(value)
end

.from_ruby(value) ⇒ Object

Convert a ruby literal into a node that represents the same value



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/estreet/literal.rb', line 9

def self.from_ruby(value)
  case value
  when Array
    # values are expected to be JS nodes already
    ArrayExpression.new(value)
  when String, TrueClass, FalseClass, NilClass
    Literal.new(value)
  when Fixnum, Bignum, Float
    if value < 0 # negative numbers have to be specially handled -- a negative literal is apparently not allowed?
      UnaryExpression.new("-", Literal.new(value.abs))
    else
      Literal.new(value)
    end
  else
    raise ArgumentError, "Can't convert to a literal: #{value}"
  end
end

Instance Method Details

#attributesObject



31
32
33
# File 'lib/estreet/literal.rb', line 31

def attributes
  super.merge(value: @value)
end