Class: JSONAPI::CompiledJson

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/compiled_json.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, h = nil) ⇒ CompiledJson

Returns a new instance of CompiledJson.



17
18
19
20
# File 'lib/jsonapi/compiled_json.rb', line 17

def initialize(json, h = nil)
  @json = json
  @h = h
end

Class Method Details

.compile(h) ⇒ Object



3
4
5
# File 'lib/jsonapi/compiled_json.rb', line 3

def self.compile(h)
  new(JSON.generate(h), h)
end

.of(obj) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/jsonapi/compiled_json.rb', line 7

def self.of(obj)
  case obj
    when NilClass then nil
    when CompiledJson then obj
    when String then CompiledJson.new(obj)
    when Hash then CompiledJson.compile(obj)
    else raise "Can't figure out how to turn #{obj.inspect} into CompiledJson"
  end
end

Instance Method Details

#to_hObject



30
31
32
# File 'lib/jsonapi/compiled_json.rb', line 30

def to_h
  @h ||= JSON.parse(@json)
end

#to_json(*args) ⇒ Object



22
23
24
# File 'lib/jsonapi/compiled_json.rb', line 22

def to_json(*args)
  @json
end

#to_sObject



26
27
28
# File 'lib/jsonapi/compiled_json.rb', line 26

def to_s
  @json
end