Class: HashSerializer::JSON

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/hash_serializer/json.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.attributesObject



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_jsonObject



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_jsonObject



31
32
33
# File 'lib/hash_serializer/json.rb', line 31

def to_json
  ::JSON.generate(as_json)
end