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



27
28
29
# File 'lib/fish0/repository.rb', line 27

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

#countObject



82
83
84
# File 'lib/fish0/repository.rb', line 82

def count
  find(conditions).count
end

#distinct(field) ⇒ Object



36
37
38
# File 'lib/fish0/repository.rb', line 36

def distinct(field)
  @source.distinct field, @conditions
end

#fetchObject



74
75
76
77
78
79
80
# File 'lib/fish0/repository.rb', line 74

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

#firstObject



40
41
42
43
# File 'lib/fish0/repository.rb', line 40

def first
  element = fetch.limit(1).first
  to_entity.call(element) if element
end

#first!Object



45
46
47
# File 'lib/fish0/repository.rb', line 45

def first!
  first || raise(RecordNotFound, "can't find in #{collection} with #{conditions}")
end

#limit(value) ⇒ Object



64
65
66
67
# File 'lib/fish0/repository.rb', line 64

def limit(value)
  @limit_quantity = value
  self
end

#order_by(query) ⇒ Object



59
60
61
62
# File 'lib/fish0/repository.rb', line 59

def order_by(query)
  order.merge!(query)
  self
end

#projection(values) ⇒ Object



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

def projection(values)
  @projection = values
  self
end

#search(string) ⇒ Object



54
55
56
57
# File 'lib/fish0/repository.rb', line 54

def search(string)
  where('$text' => { '$search' => string })
  self
end

#skip(value) ⇒ Object



69
70
71
72
# File 'lib/fish0/repository.rb', line 69

def skip(value)
  @skip_quantity = value
  self
end

#where(query) ⇒ Object



49
50
51
52
# File 'lib/fish0/repository.rb', line 49

def where(query)
  conditions.merge!(query)
  self
end