Sha256: bd8e83b328dc2bdccfd5235f9f55375f07f4f25479f8f51cc47ae958a1eaa6ab

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module Daru
  class Index
    include Enumerable

    # needs to iterate over keys sorted by their values. Happens right now by 
    # virtue of ordered Hashes (ruby).
    def each(&block)
      @relation_hash.each_key(&block)
    end

    attr_reader :relation_hash

    attr_reader :size

    attr_reader :index_class

    def initialize index
      @relation_hash = {}

      index = 0                         if index.nil?
      index = Array.new(index) { |i| i} if index.is_a? Integer

      index.each_with_index do |n, idx|
        n = n.to_sym unless n.is_a?(Integer)

        @relation_hash[n] = idx 
      end
      @relation_hash.freeze

      @size = @relation_hash.size

      if index[0].is_a?(Integer)
        @index_class = Integer
      else
        @index_class = Symbol
      end
    end

    def ==(other)
      return false if other.size != @size

      @relation_hash.keys == other.to_a
    end

    def [](key)
      @relation_hash[key]
    end

    def +(other)
      if other.respond_to? :relation_hash #another index object
        (@relation_hash.keys + other.relation_hash.keys).uniq.to_index
      elsif other.is_a?(Symbol) or other.is_a?(Integer)
        (@relation_hash.keys << other).uniq.to_index
      else
        (@relation_hash.keys + other).uniq.to_index
      end
    end

    def to_a
      @relation_hash.keys
    end

    def key(value)
      @relation_hash.key value
    end

    def re_index new_index
      new_index.to_index
    end

    def include? index
      @relation_hash.has_key? index
    end

    def dup
      Daru::Index.new @relation_hash.keys
    end

    def to_index
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
daru-0.0.3.1 lib/daru/index.rb
daru-0.0.3 lib/daru/index.rb