Sha256: 30179d44cde184f34444429bc4c0d7ba68489ec952916960e8b4105b530b3c7f

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module IMW
  module Formats

    # Defines methods for reading and writing JSON data.
    module Json

      include Enumerable

      # Return the content of this resource.
      #
      # Will pass a block to the outermost JSON data structure's each
      # method.
      #
      # @return [Hash, Array, String, Fixnum] whatever the JSON contained
      def load &block
        require 'json'
        json = JSON.parse(read)
        if block_given?
          json.each(&block)
        else
          json
        end
      end

      # Iterate over the elements in the JSON.
      def each &block
        load(&block)
      end

      # Dump the +data+ into this resource.  It must be opened for
      # writing.
      #
      # @param [Hash, String, Array, Fixnum] data the Ruby object to dump
      # @option options [true, false] :persist (false) Don't close the IO object after writing
      def dump data, options={}
        require 'json'
        write(data.to_json)
        io.close unless options[:persist]
        self
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
imw-0.2.7 lib/imw/formats/json.rb
imw-0.2.6 lib/imw/formats/json.rb
imw-0.2.5 lib/imw/formats/json.rb