Sha256: 5babbea85d300a0f5b1b1796a3b11edd7611a1f1590263a6e1b28673948bdc7c

Contents?: true

Size: 879 Bytes

Versions: 13

Compression:

Stored size: 879 Bytes

Contents

require 'multi_json'
require 'representable/hash'
require 'representable/json/collection'

module Representable
  # Brings #to_json and #from_json to your object.
  module JSON
    extend Hash::ClassMethods
    include Hash

    def self.included(base)
      base.class_eval do
        include Representable # either in Hero or HeroRepresentation.
        extend ClassMethods # DISCUSS: do that only for classes?
        register_feature Representable::JSON
      end
    end


    module ClassMethods
      def collection_representer_class
        JSON::Collection
      end
    end


    # Parses the body as JSON and delegates to #from_hash.
    def from_json(data, *args)
      data = MultiJson.load(data)
      from_hash(data, *args)
    end

    # Returns a JSON string representing this object.
    def to_json(*args)
      MultiJson.dump to_hash(*args)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
representable-2.2.3 lib/representable/json.rb
representable-2.2.2 lib/representable/json.rb
representable-2.2.1 lib/representable/json.rb
representable-2.2.0 lib/representable/json.rb
representable-2.1.8 lib/representable/json.rb
representable-2.1.7 lib/representable/json.rb
representable-2.1.6 lib/representable/json.rb
representable-2.1.5 lib/representable/json.rb
representable-2.1.4 lib/representable/json.rb
representable-2.1.3 lib/representable/json.rb
representable-2.1.1 lib/representable/json.rb
representable-2.1.0 lib/representable/json.rb
representable-2.0.4 lib/representable/json.rb