Class: Rmodel::Mongo::Repository
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#execute_query, #find_by_query, included, #query
Constructor Details
Returns a new instance of Repository.
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/rmodel/mongo/repository.rb', line 9
def initialize
@client = Rmodel.setup.establish_mongo_client(self.class.client_name || :default) or
raise ArgumentError.new('Client driver is not setup')
@collection = self.class.setting_collection ||
self.class.collection_by_convention or
raise ArgumentError.new('Collection can not be guessed')
@factory = self.class.setting_factory or
raise ArgumentError.new('Factory can not be guessed')
end
|
Class Attribute Details
.client_name ⇒ Object
Returns the value of attribute client_name.
44
45
46
|
# File 'lib/rmodel/mongo/repository.rb', line 44
def client_name
@client_name
end
|
.setting_collection ⇒ Object
Returns the value of attribute setting_collection.
44
45
46
|
# File 'lib/rmodel/mongo/repository.rb', line 44
def setting_collection
@setting_collection
end
|
.setting_factory ⇒ Object
Returns the value of attribute setting_factory.
44
45
46
|
# File 'lib/rmodel/mongo/repository.rb', line 44
def setting_factory
@setting_factory
end
|
Class Method Details
.client(name) ⇒ Object
46
47
48
|
# File 'lib/rmodel/mongo/repository.rb', line 46
def client(name)
@client_name = name
end
|
.collection(name) ⇒ Object
50
51
52
|
# File 'lib/rmodel/mongo/repository.rb', line 50
def collection(name)
@setting_collection = name
end
|
.collection_by_convention ⇒ Object
54
55
56
57
58
|
# File 'lib/rmodel/mongo/repository.rb', line 54
def collection_by_convention
if name =~ /(.*)Repository$/
ActiveSupport::Inflector.tableize($1).to_sym
end
end
|
.simple_factory(klass, *attributes) ⇒ Object
60
61
62
|
# File 'lib/rmodel/mongo/repository.rb', line 60
def simple_factory(klass, *attributes)
@setting_factory = SimpleFactory.new(klass, *attributes)
end
|
Instance Method Details
#find(id) ⇒ Object
21
22
23
24
|
# File 'lib/rmodel/mongo/repository.rb', line 21
def find(id)
result = @client[@collection].find(_id: id).first
result && @factory.fromHash(result)
end
|
#find!(id) ⇒ Object
26
27
28
|
# File 'lib/rmodel/mongo/repository.rb', line 26
def find!(id)
find(id) or raise Rmodel::NotFound.new(self, { id: id })
end
|
#insert(object) ⇒ Object
30
31
32
33
|
# File 'lib/rmodel/mongo/repository.rb', line 30
def insert(object)
object.id ||= BSON::ObjectId.new
@client[@collection].insert_one(@factory.toHash(object, true))
end
|
#remove(object) ⇒ Object
39
40
41
|
# File 'lib/rmodel/mongo/repository.rb', line 39
def remove(object)
@client[@collection].find(_id: object.id).delete_one
end
|
#update(object) ⇒ Object
35
36
37
|
# File 'lib/rmodel/mongo/repository.rb', line 35
def update(object)
@client[@collection].find(_id: object.id).update_one(@factory.toHash(object, false))
end
|