module Enumerable

Constants

EACH_WITH_FLAG_FLAGS
EACH_WITH_FLAG_VERSION

The version of the each-with-flag library.

Private Class Methods

all_user_flags() click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 25
def self.all_user_flags
  [EACH_WITH_FLAG_FLAGS.values].flatten
end

Public Instance Methods

each_with_flag(*params) { |data, *flags(index, params)| ... } click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 13
def each_with_flag(*params)
  self.each_with_index do |data, index|
    yield data, *flags(index, params)
  end
end
each_with_index_with_flag(*params) { |data, index, *flags(index, params)| ... } click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 19
def each_with_index_with_flag(*params)
  self.each_with_index do |data, index|
    yield data, index, *flags(index, params)
  end
end

Private Instance Methods

define_flag(user_flags, flag_name, value) click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 57
def define_flag(user_flags, flag_name, value)
  user_flags_for(flag_name).each_with_object(user_flags) do |user_flag, flags|
    flags[user_flag] = value
    flags
  end
end
flags(index, params) click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 68
def flags(index, params)
  flags = {}

  define_flag(flags, :first, index == 0)
  define_flag(flags, :has_previous, 0 < index)
  define_flag(flags, :surrounded, 0 < index && index < last_index)
  define_flag(flags, :has_next, index < last_index)
  define_flag(flags, :last, index == last_index)
  define_flag(flags, :first, index == 0)

  slice_hash(flags, params).values
end
last_index() click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 64
def last_index
  self.size - 1
end
slice_hash(hash, keys) click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 51
def slice_hash(hash, keys)
  keys.each_with_object({}) do |k, h|
    h[k] = hash[k] if hash.has_key?(k)
  end
end
user_flags_for(name) click to toggle source
# File lib/each_with_flag/enumerable/each_with_flag.rb, line 47
def user_flags_for(name)
  [EACH_WITH_FLAG_FLAGS[name]].flatten
end