Class: Rmodel::Mongo::SimpleFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rmodel/mongo/simple_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, *attributes) ⇒ SimpleFactory

Returns a new instance of SimpleFactory.



3
4
5
6
# File 'lib/rmodel/mongo/simple_factory.rb', line 3

def initialize(klass, *attributes)
  @klass = klass
  @attributes = attributes
end

Instance Method Details

#fromHash(hash) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/rmodel/mongo/simple_factory.rb', line 8

def fromHash(hash)
  object = @klass.new
  object.id = hash['_id']
  @attributes.each do |attribute|
    object.public_send "#{attribute}=", hash[attribute.to_s]
  end
  object
end

#toHash(object, id_included) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/rmodel/mongo/simple_factory.rb', line 17

def toHash(object, id_included)
  hash = {}
  @attributes.each do |attribute|
    hash[attribute.to_s] = object.public_send(attribute)
  end
  if id_included
    hash['_id'] = object.id
  end
  hash
end