Module: InJson::InstanceMethods
- Defined in:
- lib/in_json.rb
Instance Method Summary collapse
-
#in_json(name = :default, injected_definition = nil) ⇒ Hash
(also: #as_json)
Returns a Hash that can be used as this object in JSON format.
Instance Method Details
#in_json(name = :default, injected_definition = nil) ⇒ Hash Also known as: as_json
Returns a Hash that can be used as this object in JSON format
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/in_json.rb', line 110 def in_json(name = :default, injected_definition = nil) definition = self.class.in_json_definition(name, injected_definition) attrs = attributes.freeze.symbolize_keys return attrs unless definition definition.inject({}) do |result, attr_dfn| attr, definition = attr_dfn result_at = attrs.has_key?(attr) ? attrs[attr] : send(attr) result_at = result_at.in_json(name, definition) if result_at.respond_to?(:in_json) && !result_at.kind_of?(Class) result[attr] = result_at result end end |