Class: Murlsh::UrlResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/murlsh/url_result_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, page, per_page, filters = {}) ⇒ UrlResultSet

Returns a new instance of UrlResultSet.



7
8
9
10
# File 'lib/murlsh/url_result_set.rb', line 7

def initialize(query, page, per_page, filters={})
  @query, @page, @per_page, @filters = query, page, per_page, filters
  @order = 'time DESC'
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



66
67
68
# File 'lib/murlsh/url_result_set.rb', line 66

def filters
  @filters
end

#orderObject (readonly)

Returns the value of attribute order.



67
68
69
# File 'lib/murlsh/url_result_set.rb', line 67

def order
  @order
end

#pageObject (readonly)

Returns the value of attribute page.



64
65
66
# File 'lib/murlsh/url_result_set.rb', line 64

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



65
66
67
# File 'lib/murlsh/url_result_set.rb', line 65

def per_page
  @per_page
end

#queryObject (readonly)

Returns the value of attribute query.



63
64
65
# File 'lib/murlsh/url_result_set.rb', line 63

def query
  @query
end

Instance Method Details

#conditionsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/murlsh/url_result_set.rb', line 16

def conditions
  @conditions ||= if filters[:content_type]
    result = if filters[:content_type].is_a?(String)
      ['content_type = ?', filters[:content_type]]
    else
      ['content_type IN (?)', filters[:content_type]]
    end

    unless search_conditions.empty?
      result[0] << " AND (#{search_conditions[0]})"
      result.push(*search_conditions[1..-1])
    end
    result
  else
    search_conditions
  end
end

#next_pageObject



56
57
58
59
60
61
# File 'lib/murlsh/url_result_set.rb', line 56

def next_page
  unless instance_variable_defined?(:@next_page)
    @next_page = page + 1  if page < total_pages
  end
  @next_page
end

#offsetObject



42
# File 'lib/murlsh/url_result_set.rb', line 42

def offset; @offset ||= (page - 1) * per_page; end

#prev_pageObject



49
50
51
52
53
54
# File 'lib/murlsh/url_result_set.rb', line 49

def prev_page
  unless instance_variable_defined?(:@prev_page)
    @prev_page = page - 1  if (2..total_pages) === page
  end
  @prev_page
end

#resultsObject



44
45
46
47
# File 'lib/murlsh/url_result_set.rb', line 44

def results
  Murlsh::Url.all(:conditions => conditions, :order => order,
    :limit => per_page, :offset => offset)
end

#search_conditionsObject



12
13
14
# File 'lib/murlsh/url_result_set.rb', line 12

def search_conditions
  @search_conditions ||= Murlsh::SearchConditions.new(query).conditions
end

#total_entriesObject



34
35
36
# File 'lib/murlsh/url_result_set.rb', line 34

def total_entries
  @total_entries ||= Murlsh::Url.count(:conditions => conditions)
end

#total_pagesObject



38
39
40
# File 'lib/murlsh/url_result_set.rb', line 38

def total_pages
  @total_pages ||= [(total_entries / per_page.to_f).ceil, 1].max
end