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.



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

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

Class Method Details

.compile(h) ⇒ Object



5
6
7
# File 'lib/jsonapi/compiled_json.rb', line 5

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

.of(obj) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jsonapi/compiled_json.rb', line 9

def self.of(obj)
  # :nocov:
  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
  # :nocov:
end

Instance Method Details

#[](key) ⇒ Object

:nocov:



40
41
42
43
44
# File 'lib/jsonapi/compiled_json.rb', line 40

def [](key)
  # :nocov:
  to_h[key]
  # :nocov:
end

#to_hObject

:nocov:



35
36
37
# File 'lib/jsonapi/compiled_json.rb', line 35

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

#to_json(*_args) ⇒ Object



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

def to_json(*_args)
  @json
end

#to_sObject



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

def to_s
  @json
end