module Charty::VectorAdapters::IndexSupport

Attributes

index[R]

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/charty/vector_adapters.rb, line 146
def [](key)
  case key
  when Charty::Vector
    where(key)
  else
    super(key_to_loc(key))
  end
end
[]=(key, val) click to toggle source
Calls superclass method
# File lib/charty/vector_adapters.rb, line 155
def []=(key, val)
  super(key_to_loc(key), val)
end
index=(values) click to toggle source
# File lib/charty/vector_adapters.rb, line 172
def index=(values)
  @index = check_and_convert_index(values, :index, length)
end

Private Instance Methods

check_and_convert_index(values, name, expected_length) click to toggle source
# File lib/charty/vector_adapters.rb, line 176
        def check_and_convert_index(values, name, expected_length)
  case values
  when Index, Range
  else
    unless (ary = Array.try_convert(values))
      raise ArgumentError, "invalid object for %s: %p" % [name, values]
    end
    values = ary
  end
  if expected_length != values.size
    raise ArgumentError,
          "invalid length for %s (%d for %d)" % [name, values.size, expected_length]
  end
  case values
  when Index
    values
  when Range
    RangeIndex.new(values)
  when Array
    Index.new(values)
  end
end
key_to_loc(key) click to toggle source
# File lib/charty/vector_adapters.rb, line 159
        def key_to_loc(key)
  loc = self.index.loc(key)
  if loc.nil?
    if key.respond_to?(:to_int)
      loc = key.to_int
    else
      raise KeyError.new("key not found: %p" % key,
                         receiver: __method__, key: key)
    end
  end
  loc
end