Module: Representable::JSON

Extended by:
Hash::ClassMethods
Includes:
Hash
Included in:
Collection
Defined in:
lib/representable/json.rb,
lib/representable/json/hash.rb,
lib/representable/json/collection.rb

Overview

Brings #to_json and #from_json to your object.

Defined Under Namespace

Modules: ClassMethods, Collection, Hash, MultiJSON

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hash::ClassMethods

collection_representer_class, format_engine

Class Method Details

.included(base) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/representable/json.rb', line 26

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

Instance Method Details

#from_json(data, *args) ⇒ Object Also known as: parse

Parses the body as JSON and delegates to #from_hash.



47
48
49
50
# File 'lib/representable/json.rb', line 47

def from_json(data, *args)
  data = MultiJSON.parse(data)
  from_hash(data, *args)
end

#to_json(*args) ⇒ Object Also known as: render

Returns a JSON string representing this object.



53
54
55
# File 'lib/representable/json.rb', line 53

def to_json(*args)
  MultiJSON.generate to_hash(*args)
end