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
63
64
# File 'lib/arel_extensions/nodes/json.rb', line 33

def initialize *expr
  @dict =
    if expr.length == 1
      case exp = expr.first
      when JsonNode
        exp.dict
      when Array
        exp.map{|e|
          (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
        }
      when Hash
        exp.reduce({}){|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
        convert_to_node(exp)
      when NilClass
        Arel.sql('null')
      else
        if (exp.is_a?(Arel::Attributes::Attribute) && type_of_attribute(exp) == :string) \
           || (exp.return_type == :string)
          convert_to_node(exp)
        else
          [convert_to_node(exp)]
        end
      end
    else
      expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
    end
  super
end