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
- #find_one(query) ⇒ Object
- #find_one!(query) ⇒ Object
- #first ⇒ Object
- #first! ⇒ Object
-
#initialize(collection:, entity_class: nil, source: nil) ⇒ Repository
constructor
A new instance of Repository.
- #limit(value) ⇒ Object
- #order_by(query) ⇒ Object
- #projection(values) ⇒ Object
- #scope(name, body) ⇒ Object
- #search(string) ⇒ Object
- #skip(value) ⇒ Object
- #to_collection ⇒ Object
- #where(query) ⇒ Object
Constructor Details
#initialize(collection:, entity_class: nil, source: 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, source: nil) raise ArgumentError, 'you should provide collection name' unless collection @collection = collection @source = source ? source[collection] : 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
43 44 45 |
# File 'lib/fish0/repository.rb', line 43 def all self end |
#count ⇒ Object
111 112 113 |
# File 'lib/fish0/repository.rb', line 111 def count find(conditions).count end |
#distinct(field) ⇒ Object
52 53 54 |
# File 'lib/fish0/repository.rb', line 52 def distinct(field) @source.distinct field, @conditions end |
#fetch ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/fish0/repository.rb', line 103 def fetch scoped = find(conditions, sort: order) scoped = scoped.projection(@projection) if @projection scoped = scoped.skip(skip_quantity) if skip_quantity.positive? scoped = scoped.limit(limit_quantity) if limit_quantity.positive? scoped end |
#find(filter = nil, options = {}) ⇒ Object
39 40 41 |
# File 'lib/fish0/repository.rb', line 39 def find(filter = nil, = {}) @source.find filter.dup, end |
#find_one(query) ⇒ Object
27 28 29 |
# File 'lib/fish0/repository.rb', line 27 def find_one(query) where(query).first end |
#find_one!(query) ⇒ Object
31 32 33 |
# File 'lib/fish0/repository.rb', line 31 def find_one!(query) find_one(query) || raise(RecordNotFound, "can't find in #{collection} with #{conditions}") end |
#first ⇒ Object
56 57 58 59 |
# File 'lib/fish0/repository.rb', line 56 def first element = fetch.limit(1).first to_entity.call(element) if element end |
#first! ⇒ Object
61 62 63 |
# File 'lib/fish0/repository.rb', line 61 def first! first || raise(RecordNotFound, "can't find in #{collection} with #{conditions}") end |
#limit(value) ⇒ Object
80 81 82 83 |
# File 'lib/fish0/repository.rb', line 80 def limit(value) @limit_quantity = value self end |
#order_by(query) ⇒ Object
75 76 77 78 |
# File 'lib/fish0/repository.rb', line 75 def order_by(query) order.merge!(query) self end |
#projection(values) ⇒ Object
47 48 49 50 |
# File 'lib/fish0/repository.rb', line 47 def projection(values) @projection = values self end |
#scope(name, body) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fish0/repository.rb', line 90 def scope(name, body) return if respond_to?(name) unless body.respond_to?(:call) raise ArgumentError, 'The scope body needs to be callable.' end define_singleton_method(name) do |*args| instance_exec(*args, &body) self end end |
#search(string) ⇒ Object
70 71 72 73 |
# File 'lib/fish0/repository.rb', line 70 def search(string) where('$text' => { '$search' => string }) self end |
#skip(value) ⇒ Object
85 86 87 88 |
# File 'lib/fish0/repository.rb', line 85 def skip(value) @skip_quantity = value self end |
#to_collection ⇒ Object
35 36 37 |
# File 'lib/fish0/repository.rb', line 35 def to_collection Fish0::Collection.new(fetch.map(&to_entity)) end |
#where(query) ⇒ Object
65 66 67 68 |
# File 'lib/fish0/repository.rb', line 65 def where(query) conditions.merge!(query) self end |