Sha256: c2ceaae2f8c2b699c30cb94cf4eadde9af98936978baef93ae74b5103df3f896

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

gem "multi_json", '>= 1.14.1'
require "multi_json"

require "representable"

module Representable
  # Brings #to_json and #from_json to your object.
  module JSON
    autoload :Collection, "representable/json/collection"

    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

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/lib/representable/json.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/lib/representable/json.rb
representable-3.2.0 lib/representable/json.rb
representable-3.1.1 lib/representable/json.rb
representable-3.1.0 lib/representable/json.rb