Class: PublishMyData::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/publish_my_data/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(criteria, pagination_params, opts = {}) ⇒ Paginator

criteria can be a Tripod::Criteria or a sparql string. pagination_params should be an instance pagination params.  if criteria is a sparql string, optionally pass options to dictate what type of objects to return (else it will return Resources)



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/publish_my_data/paginator.rb', line 58

def initialize(criteria, pagination_params, opts={})

  if criteria.class == String
    self.sparql_query = PublishMyData::SparqlQuery.new(criteria)
    self.resource_class = opts[:resource_class] || PublishMyData::Resource
  elsif criteria.class == Tripod::Criteria
    # Note that this uses the :return_graph => false option for criteria execution to avoid duplicate graphs in the results
    self.sparql_query = PublishMyData::SparqlQuery.new(criteria.as_query(:return_graph => false))
    self.resource_class = criteria.resource_class
  end

  self.pagination_params = pagination_params
end

Instance Attribute Details

#pagination_paramsObject

Returns the value of attribute pagination_params.



51
52
53
# File 'lib/publish_my_data/paginator.rb', line 51

def pagination_params
  @pagination_params
end

#resource_classObject

Returns the value of attribute resource_class.



52
53
54
# File 'lib/publish_my_data/paginator.rb', line 52

def resource_class
  @resource_class
end

#sparql_queryObject

PublishMyData::SparqlQuery



53
54
55
# File 'lib/publish_my_data/paginator.rb', line 53

def sparql_query
  @sparql_query
end

Instance Method Details

#==(other) ⇒ Object



97
98
99
100
101
# File 'lib/publish_my_data/paginator.rb', line 97

def ==(other)
  self.pagination_params == other.pagination_params &&
    self.sparql_query == other.sparql_query  &&
    self.resource_class == self.resource_class
end

#paginate(force_total_count = nil) ⇒ Object

returns a Kaminari paginatable array (for html), or a plain old array (for data formats)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/publish_my_data/paginator.rb', line 73

def paginate(force_total_count=nil)

  page = self.pagination_params.page
  per_page = self.pagination_params.per_page
  pagination_query_str = self.sparql_query.as_pagination_query(page, per_page).query

  if self.pagination_params.format == :html
    total_count = force_total_count || self.sparql_query.count
    page_of_results = resource_class.find_by_sparql(pagination_query_str)
    Kaminari.paginate_array(page_of_results, total_count: total_count).page(page).per(per_page)
  else
    page_of_results = resource_class.find_by_sparql(pagination_query_str)

    Tripod::ResourceCollection.new(
      page_of_results,
      :return_graph => false,
      :sparql_query_str => pagination_query_str,
      :resource_class => self.resource_class
    )

  end

end