Class: Perpetuity::Postgres::JSONHash

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/json_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, location = :outer) ⇒ JSONHash

Returns a new instance of JSONHash.



7
8
9
10
# File 'lib/perpetuity/postgres/json_hash.rb', line 7

def initialize value, location=:outer
  @value = value
  @location = location
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
# File 'lib/perpetuity/postgres/json_hash.rb', line 45

def == other
  other.is_a? self.class and
  other.to_hash == to_hash
end

#serialize_elementsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/perpetuity/postgres/json_hash.rb', line 24

def serialize_elements
  @value.map do |key, value|
    string = ''
    string << JSONStringValue.new(key) << ':'

    string << if [String, Class].include? value.class
      JSONStringValue.new(value.to_s)
    elsif [true, false].include? value
      value.to_s
    elsif value.nil?
      'null'
    else
      SQLValue.new(value).to_s
    end
  end.join(',')
end

#to_hashObject



20
21
22
# File 'lib/perpetuity/postgres/json_hash.rb', line 20

def to_hash
  @value
end

#to_sObject



12
13
14
15
16
17
18
# File 'lib/perpetuity/postgres/json_hash.rb', line 12

def to_s
  if @location == :outer
    "'{#{serialize_elements}}'"
  else
    "{#{serialize_elements}}"
  end
end

#to_strObject



41
42
43
# File 'lib/perpetuity/postgres/json_hash.rb', line 41

def to_str
  to_s
end