Class: HashSerializer::JSON
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HashSerializer::JSON
- Defined in:
- lib/hash_serializer/json.rb
Class Method Summary collapse
- .attributes ⇒ Object
- .default_attribute_options ⇒ Object
- .format(format_name, &formatter) ⇒ Object
- .formats ⇒ Object
- .reveal(*attrs, as: nil, format: nil) ⇒ Object
- .with_format(format_name) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #formats ⇒ Object
-
#initialize(hash) ⇒ JSON
constructor
A new instance of JSON.
- #to_json ⇒ Object
Constructor Details
#initialize(hash) ⇒ JSON
Returns a new instance of JSON.
8 9 10 |
# File 'lib/hash_serializer/json.rb', line 8 def initialize(hash) @__hash__ = hash end |
Class Method Details
.attributes ⇒ Object
19 20 21 |
# File 'lib/hash_serializer/json.rb', line 19 def attributes @attributes end |
.default_attribute_options ⇒ Object
23 24 25 |
# File 'lib/hash_serializer/json.rb', line 23 def @default_attribute_options || {} end |
.format(format_name, &formatter) ⇒ Object
37 38 39 40 |
# File 'lib/hash_serializer/json.rb', line 37 def format(format_name, &formatter) @formats ||= {} @formats[format_name] = formatter end |
.formats ⇒ Object
27 28 29 |
# File 'lib/hash_serializer/json.rb', line 27 def formats @formats end |
.reveal(*attrs, as: nil, format: nil) ⇒ Object
13 14 15 16 17 |
# File 'lib/hash_serializer/json.rb', line 13 def reveal(*attrs, as: nil, format: nil) @attributes ||= [] = {as: as, format: format}.merge() @attributes.push(*attrs.map {|name| HashSerializer::Attribute.new(name, )}) end |
.with_format(format_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/hash_serializer/json.rb', line 31 def with_format(format_name) @default_attribute_options = { format: format_name } yield @default_attribute_options = nil end |
Instance Method Details
#as_json ⇒ Object
43 44 45 46 47 48 |
# File 'lib/hash_serializer/json.rb', line 43 def as_json hash_to_object! attributes.inject({}) do |memo, attribute| memo.merge(attribute.as_json(self)) end end |
#formats ⇒ Object
54 55 56 |
# File 'lib/hash_serializer/json.rb', line 54 def formats self.class.formats end |
#to_json ⇒ Object
50 51 52 |
# File 'lib/hash_serializer/json.rb', line 50 def to_json ::JSON.generate(as_json) end |