Sha256: a9decc07e322ed2fc431928dc7164087e286150de673f8447c99012462707826

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

require_relative './lines'

module Gorillib
  module Model

    module LoadFromJson
      extend  Gorillib::Concern
      include LoadLines

      module ClassMethods

        # Iterate a block over each line of a file having JSON records, one per
        # line, in a big stack
        #
        # @yield an object instantiated from each line in the file.
        def _each_from_json(filename, options={})
          _each_raw_line(filename, options) do |line|
            hsh = MultiJson.load(line)
            yield receive(hsh)
          end
        end

        # 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.
        #
        # @return with a block, returns nil; with no block, an array of this class' instances
        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

      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gorillib-0.6.0 lib/gorillib/model/serialization/json.rb
gorillib-0.5.2 lib/gorillib/model/serialization/json.rb
gorillib-0.5.0 lib/gorillib/model/serialization/json.rb