class Spicerack::ArrayIndex

Attributes

array[R]

Public Class Methods

new(*array) click to toggle source
# File lib/spicerack/array_index.rb, line 19
def initialize(*array)
  if array.length == 1 && array[0].respond_to?(:to_ary)
    @array = array[0].to_a
  else
    @array = array
  end
end

Public Instance Methods

freeze() click to toggle source
Calls superclass method
# File lib/spicerack/array_index.rb, line 34
def freeze
  @array = _deep_freeze_and_dup_object(array).freeze
  index.freeze

  super
end
index() click to toggle source
# File lib/spicerack/array_index.rb, line 27
def index
  array.each_with_index.each_with_object({}) do |(element, index), hash|
    hash[element] = index unless hash.key?(element)
  end
end

Private Instance Methods

_deep_freeze_and_dup_object(obj) click to toggle source
# File lib/spicerack/array_index.rb, line 43
def _deep_freeze_and_dup_object(obj)
  if obj.is_a?(Module)
    obj
  elsif obj.respond_to?(:transform_values)
    obj.transform_values(&method(:_deep_freeze_and_dup_object)).freeze
  elsif obj.respond_to?(:map)
    obj.map(&method(:_deep_freeze_and_dup_object)).freeze
  else
    obj.dup.freeze
  end
end