Sha256: 45db791abbefa2574e83e9b1b96a5f01038410b9c447771d1b4ba1d8b8c3a546

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

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

begin
  require 'multi_json'
rescue LoadError => _
  abort "Missing dependency 'multi_json' for Representable::JSON. See dependencies section in README.md for details."
end

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

1 entries across 1 versions & 1 rubygems

Version Path
representable-2.3.0 lib/representable/json.rb