Class: Mailroute::Relation

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/mailroute/extensions/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, search_options = {}) ⇒ Relation

Returns a new instance of Relation.



9
10
11
12
# File 'lib/mailroute/extensions/relation.rb', line 9

def initialize(klass, search_options = {})
  @klass = klass
  @search_options = search_options
end

Instance Attribute Details

#search_optionsObject (readonly)

Returns the value of attribute search_options.



7
8
9
# File 'lib/mailroute/extensions/relation.rb', line 7

def search_options
  @search_options
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/mailroute/extensions/relation.rb', line 38

def ==(other)
  case other
  when Relation
    search_options == other.search_options
  when Array
    to_a == other
  else
    false
  end
end

#filter(options) ⇒ Object



26
27
28
# File 'lib/mailroute/extensions/relation.rb', line 26

def filter(options)
  new_relation(:params => options)
end

#limit(n) ⇒ Object



18
19
20
# File 'lib/mailroute/extensions/relation.rb', line 18

def limit(n)
  new_relation(:params => { :limit => n })
end

#offset(n) ⇒ Object



22
23
24
# File 'lib/mailroute/extensions/relation.rb', line 22

def offset(n)
  new_relation(:params => { :offset => n })
end

#order_by(attribute) ⇒ Object



30
31
32
# File 'lib/mailroute/extensions/relation.rb', line 30

def order_by(attribute)
  new_relation(:params => { :order_by => attribute })
end

#search(term) ⇒ Object



34
35
36
# File 'lib/mailroute/extensions/relation.rb', line 34

def search(term)
  new_relation(:params => { :q => term })
end

#to_aObject



14
15
16
# File 'lib/mailroute/extensions/relation.rb', line 14

def to_a
  @records ||= @klass.all(search_options)
end

#total_countObject



49
50
51
52
# File 'lib/mailroute/extensions/relation.rb', line 49

def total_count
  meta = to_a.instance_variable_get(:@_meta)
  meta && meta['total_count']
end