Module: Gorillib::Model::LoadFromJson::ClassMethods

Defined in:
lib/gorillib/model/serialization/json.rb

Instance Method Summary collapse

Instance Method Details

#_each_from_json(filename, options = {}) { ... } ⇒ Object

Iterate a block over each line of a file having JSON records, one per line, in a big stack

Yields:

  • an object instantiated from each line in the file.



16
17
18
19
20
21
# File 'lib/gorillib/model/serialization/json.rb', line 16

def _each_from_json(filename, options={})
  _each_raw_line(filename, options) do |line|
    hsh = MultiJson.load(line)
    yield receive(hsh)
  end
end

#load_json(*args) ⇒ Object

With a block, calls block on each object in turn (and returns nil)

With no block, accumulates all the instances into the array it returns. As opposed to the with-a-block case, the memory footprint of this increases as the filesize does, so use caution with large files.

Returns:

  • with a block, returns nil; with no block, an array of this class’ instances



30
31
32
33
34
35
36
37
38
# File 'lib/gorillib/model/serialization/json.rb', line 30

def load_json(*args)
  if block_given?
    _each_from_json(*args, &Proc.new)
  else
    objs = []
    _each_from_json(*args){|obj| objs << obj }
    objs
  end
end