Module: Representable::JSON::InstanceMethods

Defined in:
lib/representable/json.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#to_hash(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/representable/json.rb', line 52

def to_hash(options={})
  hash = {}.tap do |attrs|
    refs = self.class.representable_attrs.map {|attr| JSON.binding_for_definition(attr) }
    
    refs.each do |ref|
      value = public_send(ref.definition.accessor) # DISCUSS: eventually move back to Ref.
      ref.update_json(attrs, value) if value
    end
  end
  
  # DISCUSS: where to wrap?
  options[:wrap] == false ? hash : {self.class.representation_name => hash}
end

#to_json(options = {}) ⇒ Object

Returns a Nokogiri::XML object representing this object.



67
68
69
# File 'lib/representable/json.rb', line 67

def to_json(options={})
  to_hash(options).to_json
end