class Array
helper to check for method existance in ruby 1.8- and 1.9-compatible way because `methods`, `instance_methods` and others return strings in 1.8 and symbols in 1.9
['foo', 'bar'].include_method?(:foo) # => true
Public Instance Methods
include_method?(name)
click to toggle source
# File lib/leaf/core_ext.rb, line 9 def include_method?(name) name = name.to_sym !!(find { |item| item.to_sym == name }) end
paginate(options = {})
click to toggle source
Paginates a static array (extracting a subset of it). The result is a Leaf::Collection
instance, which is an array with a few more properties about its paginated state.
Parameters:
-
:page
- current page, defaults to 1 -
:per_page
- limit of items per page, defaults to 30 -
:total_entries
- total number of items in the array, defaults toarray.length
(obviously)
Example:
arr = ['a', 'b', 'c', 'd', 'e'] paged = arr.paginate(:per_page => 2) #-> ['a', 'b'] paged.total_entries #-> 5 arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd'] arr.paginate(:page => 3, :per_page => 2) #-> ['e']
This method was originally suggested by Desi McAdam
# File lib/leaf/array.rb, line 23 def paginate(options = {}) raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options Leaf::Collection.create options[:page] || 1, options[:per_page] || 30, options[:total_entries] || self.length do |pager| pager.replace self[pager.offset, pager.per_page].to_a end end