Class: Estreet::Literal
- Inherits:
-
Expression
- Object
- Node
- Expression
- Estreet::Literal
- Defined in:
- lib/estreet/literal.rb
Instance Attribute Summary
Attributes inherited from Node
Class Method Summary collapse
- .[](value) ⇒ Object
-
.from_ruby(value) ⇒ Object
Convert a ruby literal into a node that represents the same value.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(value) ⇒ Literal
constructor
A new instance of Literal.
Methods inherited from Expression
#[], #call, coerce, #property, #to_expression, #to_statement
Methods inherited from Node
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
#attributes ⇒ Object
31 32 33 |
# File 'lib/estreet/literal.rb', line 31 def attributes super.merge(value: @value) end |