Class: Fish0::Repository

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fish0/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, entity_class = nil) ⇒ Repository

Returns a new instance of Repository.

Raises:

  • (ArgumentError)


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

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def collection
  @collection
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def conditions
  @conditions
end

#entity_classObject (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_quantityObject (readonly)

Returns the value of attribute limit_quantity.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def limit_quantity
  @limit_quantity
end

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def order
  @order
end

#skip_quantityObject (readonly)

Returns the value of attribute skip_quantity.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def skip_quantity
  @skip_quantity
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/fish0/repository.rb', line 3

def source
  @source
end

Instance Method Details

#allObject



31
32
33
# File 'lib/fish0/repository.rb', line 31

def all
  Fish0::Collection.new(fetch.map(&to_entity))
end

#countObject



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

#fetchObject



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, options = {})
  @source.find filter.dup, options
end

#firstObject



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