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.
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/rmodel/mongo/repository.rb', line 11
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.
46
47
48
|
# File 'lib/rmodel/mongo/repository.rb', line 46
def client_name
@client_name
end
|
.setting_collection ⇒ Object
Returns the value of attribute setting_collection.
46
47
48
|
# File 'lib/rmodel/mongo/repository.rb', line 46
def setting_collection
@setting_collection
end
|
.setting_factory ⇒ Object
Returns the value of attribute setting_factory.
46
47
48
|
# File 'lib/rmodel/mongo/repository.rb', line 46
def setting_factory
@setting_factory
end
|
Class Method Details
.client(name) ⇒ Object
48
49
50
|
# File 'lib/rmodel/mongo/repository.rb', line 48
def client(name)
@client_name = name
end
|
.collection(name) ⇒ Object
52
53
54
|
# File 'lib/rmodel/mongo/repository.rb', line 52
def collection(name)
@setting_collection = name
end
|
.collection_by_convention ⇒ Object
56
57
58
59
60
|
# File 'lib/rmodel/mongo/repository.rb', line 56
def collection_by_convention
if name =~ /(.*)Repository$/
ActiveSupport::Inflector.tableize($1).to_sym
end
end
|
.simple_factory(klass, *attributes) ⇒ Object
62
63
64
|
# File 'lib/rmodel/mongo/repository.rb', line 62
def simple_factory(klass, *attributes)
@setting_factory = SimpleFactory.new(klass, *attributes)
end
|
Instance Method Details
#find(id) ⇒ Object
23
24
25
26
|
# File 'lib/rmodel/mongo/repository.rb', line 23
def find(id)
result = @client[@collection].find(_id: id).first
result && @factory.fromHash(result)
end
|
#find!(id) ⇒ Object
28
29
30
|
# File 'lib/rmodel/mongo/repository.rb', line 28
def find!(id)
find(id) or raise Rmodel::NotFound.new(self, { id: id })
end
|
#insert(object) ⇒ Object
32
33
34
35
|
# File 'lib/rmodel/mongo/repository.rb', line 32
def insert(object)
object.id ||= BSON::ObjectId.new
@client[@collection].insert_one(@factory.toHash(object, true))
end
|
#remove(object) ⇒ Object
41
42
43
|
# File 'lib/rmodel/mongo/repository.rb', line 41
def remove(object)
@client[@collection].find(_id: object.id).delete_one
end
|
#update(object) ⇒ Object
37
38
39
|
# File 'lib/rmodel/mongo/repository.rb', line 37
def update(object)
@client[@collection].find(_id: object.id).update_one(@factory.toHash(object, false))
end
|