Class: Fish0::Repository
- Inherits:
-
Object
- Object
- Fish0::Repository
- Includes:
- Enumerable
- Defined in:
- lib/fish0/repository.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#entity_class ⇒ Object
readonly
Returns the value of attribute entity_class.
-
#limit_quantity ⇒ Object
readonly
Returns the value of attribute limit_quantity.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#skip_quantity ⇒ Object
readonly
Returns the value of attribute skip_quantity.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #distinct(field) ⇒ Object
- #fetch ⇒ Object
- #find(filter = nil, options = {}) ⇒ Object
- #first ⇒ Object
- #first! ⇒ Object
-
#initialize(collection, entity_class = nil) ⇒ Repository
constructor
A new instance of Repository.
- #limit(value) ⇒ Object
- #order_by(query) ⇒ Object
- #projection(values) ⇒ Object
- #search(string) ⇒ Object
- #skip(value) ⇒ Object
- #where(query) ⇒ Object
Constructor Details
#initialize(collection, entity_class = nil) ⇒ Repository
Returns a new instance of Repository.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fish0/repository.rb', line 16 def initialize(collection, entity_class = nil) raise ArgumentError, 'you should provide collection name' unless collection @collection = collection @source = Fish0.mongo_reader[collection] @conditions = default_conditions @order = {} @limit_quantity = 0 @skip_quantity = 0 @entity_class = entity_class || String(collection).singularize.camelize.constantize end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def collection @collection end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def conditions @conditions end |
#entity_class ⇒ Object (readonly)
Returns the value of attribute entity_class.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def entity_class @entity_class end |
#limit_quantity ⇒ Object (readonly)
Returns the value of attribute limit_quantity.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def limit_quantity @limit_quantity end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def order @order end |
#skip_quantity ⇒ Object (readonly)
Returns the value of attribute skip_quantity.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def skip_quantity @skip_quantity end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
3 4 5 |
# File 'lib/fish0/repository.rb', line 3 def source @source end |
Instance Method Details
#all ⇒ Object
31 32 33 |
# File 'lib/fish0/repository.rb', line 31 def all Fish0::Collection.new(fetch.map(&to_entity)) end |
#count ⇒ Object
86 87 88 |
# File 'lib/fish0/repository.rb', line 86 def count find(conditions).count end |
#distinct(field) ⇒ Object
40 41 42 |
# File 'lib/fish0/repository.rb', line 40 def distinct(field) @source.distinct field, @conditions end |
#fetch ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/fish0/repository.rb', line 78 def fetch scoped = find(conditions, sort: order) scoped = scoped.projection(@projection) if @projection scoped = scoped.skip(skip_quantity) if skip_quantity > 0 scoped = scoped.limit(limit_quantity) if limit_quantity > 0 scoped end |
#find(filter = nil, options = {}) ⇒ Object
27 28 29 |
# File 'lib/fish0/repository.rb', line 27 def find(filter = nil, = {}) @source.find filter.dup, end |
#first ⇒ Object
44 45 46 47 |
# File 'lib/fish0/repository.rb', line 44 def first element = fetch.limit(1).first to_entity.call(element) if element end |
#first! ⇒ Object
49 50 51 |
# File 'lib/fish0/repository.rb', line 49 def first! first || raise(RecordNotFound, "can't find in #{collection} with #{conditions}") end |
#limit(value) ⇒ Object
68 69 70 71 |
# File 'lib/fish0/repository.rb', line 68 def limit(value) @limit_quantity = value self end |
#order_by(query) ⇒ Object
63 64 65 66 |
# File 'lib/fish0/repository.rb', line 63 def order_by(query) order.merge!(query) self end |
#projection(values) ⇒ Object
35 36 37 38 |
# File 'lib/fish0/repository.rb', line 35 def projection(values) @projection = values self end |
#search(string) ⇒ Object
58 59 60 61 |
# File 'lib/fish0/repository.rb', line 58 def search(string) where('$text' => { '$search' => string }) self end |
#skip(value) ⇒ Object
73 74 75 76 |
# File 'lib/fish0/repository.rb', line 73 def skip(value) @skip_quantity = value self end |
#where(query) ⇒ Object
53 54 55 56 |
# File 'lib/fish0/repository.rb', line 53 def where(query) conditions.merge!(query) self end |