class PaginationSearch::HashPaginate

Public Class Methods

new(keys = nil) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 4
def initialize(keys = nil)
  @keys = keys
end

Public Instance Methods

page(items, params) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 8
def page(items, params)
  return [] unless items
  return items unless params[:limit] && params[:offset]

  length = params[:limit].to_i
  start = params[:offset].to_i * length

  search = params[:search]
  sort_by = params[:sort_by]
  sort_descending = params[:sort_descending]

  processed = process(items, search, sort_by, sort_descending)[start, length]
  processed ? processed : []
end

Protected Instance Methods

accept?(item, search_conditions) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 47
def accept?(item, search_conditions)
  search_conditions.trait_term_hash.all? do |search_key, terms|
    if search_key == PaginationSearch::SearchConditions::ALL_TRAIT_SEARCH_KEY
      search_keys = @keys || item.keys
      terms.all? do |term|
        search_keys.any? do |k|
          match(item[k], term, search_conditions.match_exact?)
        end
      end
    elsif item.key?(search_key)
      val = item[search_key]
      terms.all? do |term|
        match(val, term, search_conditions.match_exact?)
      end
    else
      true
    end
  end
end
match(field, term, match_exact) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 41
def match(field, term, match_exact)
  f = field.to_s.downcase
  t = term.downcase
  match_exact ? f == t : f.include?(t)
end
ordered(items, sort_descending) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 71
def ordered(items, sort_descending)
  sort_descending.to_bool ? items.reverse : items
end
process(items, search, sort_by, sort_descending) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 25
def process(items, search, sort_by, sort_descending)
  searched_items = searched(items, search)
  sorted_items = sorted(searched_items, sort_by)
  ordered(sorted_items, sort_descending)
end
searched(items, search) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 31
def searched(items, search)
  search_conditions = PaginationSearch::SearchConditions.process(search) if search.present?

  if search_conditions
    items.select { |i| accept?(i, search_conditions) }
  else
    items
  end
end
sorted(items, sort_by) click to toggle source
# File lib/pagination_search/hash_paginate.rb, line 67
def sorted(items, sort_by)
  sort_by.present? ? items.sort_by { |i| i[sort_by] } : items
end