Class: ArelExtensions::Nodes::Json

Inherits:
JsonNode show all
Defined in:
lib/arel_extensions/nodes/json.rb

Constant Summary

Constants inherited from JsonNode

ArelExtensions::Nodes::JsonNode::RETURN_TYPE

Constants inherited from Function

Function::RETURN_TYPE

Instance Attribute Summary

Attributes inherited from JsonNode

#dict

Instance Method Summary collapse

Methods inherited from JsonNode

#get, #group, #hash, #merge, #set

Methods inherited from Function

#!=, #==, #as, #convert_to_date_node, #convert_to_datetime_node, #convert_to_node, #convert_to_number, #convert_to_string_node, #expr, #left, #return_type, #right, #type_of_attribute

Methods included from Predications

#cast, #convert_to_node, #imatches, #in, #matches, #not_in, #when

Constructor Details

#initialize(*expr) ⇒ Json

Returns a new instance of Json.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arel_extensions/nodes/json.rb', line 33

def initialize *expr
  if expr.length == 1
    case expr.first
    when JsonNode
      @dict = expr.first.dict
    when Array
      @dict = expr.first.map{|e|
        (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
      }
    when Hash
      @dict = expr.first.inject({}){|acc,v|
        acc[convert_to_node(v[0])] =  (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) :  convert_to_node(v[1])
        acc
      }
    when String, Numeric, TrueClass, FalseClass
      @dict = convert_to_node(expr.first)
    when NilClass
      @dict = Arel.sql('null')
    else
      if expr.first.is_a?(String) || (expr.first.is_a?(Arel::Attributes::Attribute) && type_of_attribute(expr.first) == :string) || (expr.first.return_type == :string)
        @dict = convert_to_node(expr.first)
      else
        @dict = [convert_to_node(expr.first)]
      end
    end
  else
    @dict = expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
  end
  super
end