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

#hash

Instance Method Summary collapse

Methods inherited from JsonNode

#get, #group, #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.



29
30
31
32
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
# File 'lib/arel_extensions/nodes/json.rb', line 29

def initialize *expr
  if expr.length == 1
    case expr.first
    when JsonNode
      @hash = expr.first.hash
    when Array
      @hash = expr.first.map{|e|
        (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
      }
    when Hash
      @hash = 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
      @hash = convert_to_node(expr.first)
    when NilClass
      @hash = 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)
        @hash = convert_to_node(expr.first)
      else
        @hash = [convert_to_node(expr.first)]
      end
    end
  else
    @hash = expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
  end
end