Module: Gorillib::Model::LoadFromJson::ClassMethods
- Defined in:
- lib/gorillib/model/serialization/json.rb
Instance Method Summary collapse
-
#_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.
-
#load_json(*args) ⇒ Object
With a block, calls block on each object in turn (and returns nil).
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
16 17 18 19 20 21 |
# File 'lib/gorillib/model/serialization/json.rb', line 16 def _each_from_json(filename, ={}) _each_raw_line(filename, ) 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.
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 |