Class: Upmin::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/upmin/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, search_options = {}, options = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/upmin/query.rb', line 11

def initialize(klass, search_options = {}, options = {})
  if klass.active_record?
    extend Upmin::ActiveRecord::Query
  elsif klass.data_mapper?
    extend Upmin::DataMapper::Query
  else
    raise ArgumentError.new(klass)
  end

  @klass = klass
  @search_options = search_options
  @page = options[:page]
  @per_page = options[:per_page]
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/upmin/query.rb', line 4

def klass
  @klass
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'lib/upmin/query.rb', line 6

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



7
8
9
# File 'lib/upmin/query.rb', line 7

def per_page
  @per_page
end

#search_optionsObject (readonly)

Returns the value of attribute search_options.



5
6
7
# File 'lib/upmin/query.rb', line 5

def search_options
  @search_options
end

Instance Method Details

#paginated_resultsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/upmin/query.rb', line 30

def paginated_results
  return @paginated_results if defined?(@paginated_results)
  if page && per_page
    pr = Upmin::Paginator.paginate(results, page, per_page)
  else
    pr = Upmin::Paginator.paginate(results)
  end
  @paginated_results = pr
  return @paginated_results
end

#resultsObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/upmin/query.rb', line 26

def results
  raise NotImplementedError
end

#upmin_resultsObject



41
42
43
44
45
# File 'lib/upmin/query.rb', line 41

def upmin_results
  return @upmin_results if defined?(@upmin_results)
  @upmin_results = paginated_results.map{ |r| r.upmin_model }
  return @upmin_results
end