Class: MongoBijou::Crusher

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_bijou/crusher.rb

Overview

Class able to create deep-nested hash of object’s attributes.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Crusher

Returns a new instance of AttributesFormatter.



9
10
11
# File 'lib/mongo_bijou/crusher.rb', line 9

def initialize(object)
  @object = object
end

Instance Method Details

#crush(hash) ⇒ Object

Runs #scan_attr(attributes) method and deletes :config_attr from received hash. Then it adds class name, that included module, to formatted hash (necessary when retrieving from database).



17
18
19
# File 'lib/mongo_bijou/crusher.rb', line 17

def crush(hash)
  { mark_class(@object) => scan(hash) }
end

#to_hash(object) ⇒ Object

Creates hash of given object’s instance variables.



22
23
24
25
26
# File 'lib/mongo_bijou/crusher.rb', line 22

def to_hash(object)
  object.instance_variables.each_with_object({}) do |attr, hash|
    hash[attr.to_s.delete('@').to_sym] = object.instance_variable_get(attr)
  end
end