Class: StoresInMongo::MongoStore

Inherits:
Object
  • Object
show all
Defined in:
lib/stores_in_mongo/mongo_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, foreign_key) ⇒ MongoStore

Returns a new instance of MongoStore.



5
6
7
8
# File 'lib/stores_in_mongo/mongo_store.rb', line 5

def initialize(klass, foreign_key)
  @klass = klass
  @foreign_key = foreign_key      
end

Instance Attribute Details

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



3
4
5
# File 'lib/stores_in_mongo/mongo_store.rb', line 3

def foreign_key
  @foreign_key
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/stores_in_mongo/mongo_store.rb', line 3

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/stores_in_mongo/mongo_store.rb', line 3

def name
  @name
end

Instance Method Details

#find_or_initialize_for(owner) ⇒ Object



10
11
12
# File 'lib/stores_in_mongo/mongo_store.rb', line 10

def find_or_initialize_for(owner)
  owner[@foreign_key].present? && @klass.where(id: owner[@foreign_key]).first || @klass.new
end