Class: MongoQL::Expression::ValueNode

Inherits:
MongoQL::Expression show all
Defined in:
lib/mongo_ql/expression/value_node.rb

Constant Summary collapse

SUPPORTED_TYPES =
[
  String, Integer, Float,
  Array, Hash, TrueClass,
  FalseClass, Date, DateTime, Symbol,
  MongoQL::Expression::ValueNode
].freeze

Constants inherited from MongoQL::Expression

FORMATING_OPS

Constants included from CollectionOperators

CollectionOperators::AGGREGATE_OPS

Constants included from UnaryOperators

UnaryOperators::UNARY_OPS

Constants included from BinaryOperators

BinaryOperators::BINARY_OPS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MongoQL::Expression

#as_date, #if_null, #then, #type

Methods included from StringOperators

#concat, #substr, #trim

Methods included from CollectionOperators

#any?, #concat_arrays, #contains, #filter, #map, #reduce

Constructor Details

#initialize(val) ⇒ ValueNode

Returns a new instance of ValueNode.



14
15
16
17
18
# File 'lib/mongo_ql/expression/value_node.rb', line 14

def initialize(val)
  Expression::ValueNode.valid!(val)
  @value = val
  convert_to_date_if_possible
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/mongo_ql/expression/value_node.rb', line 12

def value
  @value
end

Class Method Details

.valid!(value) ⇒ Object



42
43
44
45
46
# File 'lib/mongo_ql/expression/value_node.rb', line 42

def self.valid!(value)
  unless value.nil? || valid?(value)
    raise InvalidValueExpression, "#{value} must be in type #{SUPPORTED_TYPES.map(&:name).join(",")}"
  end
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mongo_ql/expression/value_node.rb', line 38

def self.valid?(value)
  SUPPORTED_TYPES.any? { |type| value.is_a?(type) }
end

Instance Method Details

#convert_to_date_if_possibleObject



32
33
34
35
36
# File 'lib/mongo_ql/expression/value_node.rb', line 32

def convert_to_date_if_possible
  self.value = DateTime.iso8601(value)
rescue => _
  value
end

#to_astObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongo_ql/expression/value_node.rb', line 20

def to_ast
  case value
  when Date, DateTime
    # Expression::ValueNode.new(value.iso8601).to_date.to_ast
    { "$toDate" => value.iso8601 }
  when MongoQL::Expression::ValueNode
    value.to_ast
  else
    value
  end
end