Sha256: b2233468e9b2ab06046dd4f7265a672c5e456987bcc242c44030c86047038b6d
Contents?: true
Size: 1.28 KB
Versions: 13
Compression:
Stored size: 1.28 KB
Contents
require 'set' unless Hash.instance_methods.include? 'except' Hash.class_eval do # Returns a new hash without the given keys. def except(*keys) rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) reject { |key,| rejected.include?(key) } end # Replaces the hash without only the given keys. def except!(*keys) replace(except(*keys)) end end end unless Hash.instance_methods.include? 'slice' Hash.class_eval do # Returns a new hash with only the given keys. def slice(*keys) allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) reject { |key,| !allowed.include?(key) } end # Replaces the hash with only the given keys. def slice!(*keys) replace(slice(*keys)) end end end unless Array.instance_methods.include? 'paginate' # http://www.desimcadam.com/archives/8 Array.class_eval do def paginate(page = 1, per_page = 15) pagination_array = WillPaginate::Collection.new(page, per_page, size) start_index = pagination_array.offset end_index = start_index + (per_page - 1) array_to_concat = self[start_index..end_index] array_to_concat.nil? ? [] : pagination_array.concat(array_to_concat) end end end
Version data entries
13 entries across 13 versions & 4 rubygems