Sha256: a641ddd99b00287d732a8d65898fbfc55b56754900f82b0c7e8788a06549f72f

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module IMW
  module Formats

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

      # Return the content of this resource.
      #
      # Will try to be smart about iterating over the data when
      # passed a block.
      #
      # - if the outermost JSON data structure is an array, then
      #   yield each element
      #
      # - if the outermost JSON data structure is a mapping, then
      #   yield each key, value pair
      #
      # - otherwise just yield the structure
      #
      # @return [Hash, Array, String, Fixnum] whatever the JSON contained
      def load &block
        require 'json'
        json = JSON.parse(read)
        if block_given?
          case json
          when Array
            json.each      { |obj| yield obj }
          when Hash
            json.each_pair { |key, value| yield key, value }
          else
            yield json
          end
        else
          json
        end
      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

4 entries across 4 versions & 1 rubygems

Version Path
imw-0.2.4 lib/imw/formats/json.rb
imw-0.2.3 lib/imw/formats/json.rb
imw-0.2.2 lib/imw/formats/json.rb
imw-0.2.1 lib/imw/formats/json.rb