Class: HashSerializer::JSON
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HashSerializer::JSON
- Defined in:
- lib/hash_serializer/json.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(hash) ⇒ JSON
constructor
A new instance of JSON.
- #to_json ⇒ Object
Constructor Details
#initialize(hash) ⇒ JSON
8 9 10 |
# File 'lib/hash_serializer/json.rb', line 8 def initialize(hash) @__hash__ = hash end |
Class Method Details
.attributes ⇒ Object
18 19 20 |
# File 'lib/hash_serializer/json.rb', line 18 def attributes @attributes end |
.reveal(*attrs, as: nil) ⇒ Object
13 14 15 16 |
# File 'lib/hash_serializer/json.rb', line 13 def reveal(*attrs, as: nil) @attributes ||= [] @attributes.push(*attrs.map {|name| HashSerializer::Attribute.new(name, as: as)}) end |
Instance Method Details
#as_json ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/hash_serializer/json.rb', line 23 def as_json hash_to_object! attributes.inject({}) do |memo, attribute| memo[attribute.key_name] = self.public_send(attribute.name) memo end end |
#to_json ⇒ Object
31 32 33 |
# File 'lib/hash_serializer/json.rb', line 31 def to_json ::JSON.generate(as_json) end |