Sha256: d65bd9bf12493dd01e03292c2a2076cd4f45eab2ed777d5d9a4c85f68ee1e416
Contents?: true
Size: 1.04 KB
Versions: 10
Compression:
Stored size: 1.04 KB
Contents
require 'representable/hash' require 'multi_json' 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? extend Representable::Hash::ClassMethods # DISCUSS: this is only for .from_hash, remove in 2.3? end end module ClassMethods # Creates a new object from the passed JSON document. def from_json(*args, &block) create_represented(*args, &block).from_json(*args) end private def representer_engine Representable::JSON 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
10 entries across 10 versions & 1 rubygems