Sha256: 7859817ee0b012e22b991580176b4ec491f9057979f5cd1545c692e76c86318a

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 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 format_engine
        Representable::Hash
      end

      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

    alias_method :render, :to_json
    alias_method :parse, :from_json
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
representable-3.0.4 lib/representable/json.rb
representable-3.0.3 lib/representable/json.rb
representable-3.0.2 lib/representable/json.rb