Sha256: fcc0800c44ed1a73c5ae95b9ca792e8bd3274759d9d15289a21d0213bc682193

Contents?: true

Size: 921 Bytes

Versions: 11

Compression:

Stored size: 921 Bytes

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

      # Emit the +data+ into this resource.  It must be opened for
      # writing.
      #
      # @param [Hash, String, Array, Fixnum] data the Ruby object to emit
      def emit data, options={}
        require 'json'
        write(data.to_json)
        self
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
imw-0.2.18 lib/imw/formats/json.rb
imw-0.2.17 lib/imw/formats/json.rb
imw-0.2.16 lib/imw/formats/json.rb
imw-0.2.15 lib/imw/formats/json.rb
imw-0.2.14 lib/imw/formats/json.rb
imw-0.2.13 lib/imw/formats/json.rb
imw-0.2.12 lib/imw/formats/json.rb
imw-0.2.11 lib/imw/formats/json.rb
imw-0.2.10 lib/imw/formats/json.rb
imw-0.2.9 lib/imw/formats/json.rb
imw-0.2.8 lib/imw/formats/json.rb