Class: Her::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/her/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Relation

Returns a new instance of Relation.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/her/relation.rb', line 3

def initialize(model)
  @model = model
  @conditions = []
  @group_conditions = []
  @having_conditions = []
  @order_conditions = []
  @limit_value = nil
  @offset_value = nil
  @do_paginate = false
  @do_count = false
end

Instance Method Details

#all(params = {}) ⇒ Object



46
47
48
49
50
# File 'lib/her/relation.rb', line 46

def all(params = {})
  with_response(params) do |data|
    @model.new_collection(data)
  end
end

#countObject



52
53
54
55
56
57
# File 'lib/her/relation.rb', line 52

def count
  @do_count = true
  with_response do |data|
    data.first
  end
end

#firstObject



59
60
61
62
# File 'lib/her/relation.rb', line 59

def first
  order('id asc').limit(1)
  all.first if all.respond_to?(:first)
end

#group(*args) ⇒ Object



21
22
23
24
# File 'lib/her/relation.rb', line 21

def group(*args)
  @group_conditions += args
  self
end

#having(*args) ⇒ Object



26
27
28
29
# File 'lib/her/relation.rb', line 26

def having(*args)
  @having_conditions += args
  self
end

#lastObject



64
65
66
67
# File 'lib/her/relation.rb', line 64

def last
  order('id desc').limit(1)
  all.first if all.respond_to?(:first)
end

#limit(value) ⇒ Object



36
37
38
39
# File 'lib/her/relation.rb', line 36

def limit(value)
  @limit_value = value
  self
end

#offset(value) ⇒ Object



41
42
43
44
# File 'lib/her/relation.rb', line 41

def offset(value)
  @offset_value = value
  self
end

#order(*args) ⇒ Object



31
32
33
34
# File 'lib/her/relation.rb', line 31

def order(*args)
  @order_conditions += args
  self
end

#paginate(page = 1, per_page = 20) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/her/relation.rb', line 69

def paginate(page = 1, per_page = 20)
  page = page.to_i < 1 ? 1 : page.to_i
  per_page = per_page.to_i < 1 ? 20 : per_page.to_i

  @do_paginate = true
  offset((page - 1) * per_page).limit(per_page)
end

#where(*args) ⇒ Object



15
16
17
18
19
# File 'lib/her/relation.rb', line 15

def where(*args)
  args = args.first if args.first.is_a?(Hash)
  @conditions.push(args)
  self
end