Module: IMW::Formats::Json

Includes:
Enumerable
Defined in:
lib/imw/formats/json.rb

Overview

Defines methods for reading and writing JSON data.

Instance Method Summary collapse

Instance Method Details

#each(&block) ⇒ Object

Iterate over the elements in the JSON.



26
27
28
# File 'lib/imw/formats/json.rb', line 26

def each &block
  load(&block)
end

#emit(data, options = {}) ⇒ Object

Emit the data into this resource. It must be opened for writing.

Parameters:



34
35
36
37
38
# File 'lib/imw/formats/json.rb', line 34

def emit data, options={}
  require 'json'
  write(data.to_json)
  self
end

#load(&block) ⇒ Hash, ...

Return the content of this resource.

Will pass a block to the outermost JSON data structure’s each method.

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/imw/formats/json.rb', line 15

def load &block
  require 'json'
  json = JSON.parse(read)
  if block_given?
    json.each(&block)
  else
    json
  end
end