Class: Daru::Index
Instance Attribute Summary collapse
-
#index_class ⇒ Object
readonly
Returns the value of attribute index_class.
-
#relation_hash ⇒ Object
readonly
Returns the value of attribute relation_hash.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #dup ⇒ Object
-
#each(&block) ⇒ Object
needs to iterate over keys sorted by their values.
- #include?(index) ⇒ Boolean
-
#initialize(index, values = nil) ⇒ Index
constructor
A new instance of Index.
- #key(value) ⇒ Object
- #re_index(new_index) ⇒ Object
- #to_a ⇒ Object
- #to_index ⇒ Object
Constructor Details
#initialize(index, values = nil) ⇒ Index
Returns a new instance of Index.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/daru/index.rb', line 17 def initialize index, values=nil @relation_hash = {} index = 0 if index.nil? index = Array.new(index) { |i| i} if index.is_a? Integer if values.nil? index.each_with_index do |n, idx| n = n.to_sym unless n.is_a?(Integer) @relation_hash[n] = idx end else raise IndexError, "Size of values : #{values.size} and index : #{index.size} do not match" if index.size != values.size values.each_with_index do |value,i| @relation_hash[index[i]] = value end end @relation_hash.freeze @size = @relation_hash.size if index[0].is_a?(Integer) @index_class = Integer else @index_class = Symbol end end |
Instance Attribute Details
#index_class ⇒ Object (readonly)
Returns the value of attribute index_class.
15 16 17 |
# File 'lib/daru/index.rb', line 15 def index_class @index_class end |
#relation_hash ⇒ Object (readonly)
Returns the value of attribute relation_hash.
11 12 13 |
# File 'lib/daru/index.rb', line 11 def relation_hash @relation_hash end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
13 14 15 |
# File 'lib/daru/index.rb', line 13 def size @size end |
Instance Method Details
#+(other) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/daru/index.rb', line 73 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 |
#==(other) ⇒ Object
49 50 51 52 53 |
# File 'lib/daru/index.rb', line 49 def ==(other) return false if other.size != @size @relation_hash.keys == other.to_a end |
#[](key) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/daru/index.rb', line 55 def [](key) case key when Range first = @relation_hash[key.first] last = @relation_hash[key.last] indexes = [] (first..last).each do |idx| indexes << @relation_hash.key(idx) end Daru::Index.new indexes, (first..last).to_a else @relation_hash[key] end end |
#dup ⇒ Object
99 100 101 |
# File 'lib/daru/index.rb', line 99 def dup Daru::Index.new @relation_hash.keys end |
#each(&block) ⇒ Object
needs to iterate over keys sorted by their values. Happens right now by virtue of ordered Hashes (ruby).
7 8 9 |
# File 'lib/daru/index.rb', line 7 def each(&block) @relation_hash.each_key(&block) end |
#include?(index) ⇒ Boolean
95 96 97 |
# File 'lib/daru/index.rb', line 95 def include? index @relation_hash.has_key? index end |
#key(value) ⇒ Object
87 88 89 |
# File 'lib/daru/index.rb', line 87 def key(value) @relation_hash.key value end |
#re_index(new_index) ⇒ Object
91 92 93 |
# File 'lib/daru/index.rb', line 91 def re_index new_index new_index.to_index end |
#to_a ⇒ Object
83 84 85 |
# File 'lib/daru/index.rb', line 83 def to_a @relation_hash.keys end |
#to_index ⇒ Object
103 104 105 |
# File 'lib/daru/index.rb', line 103 def to_index self end |