Class: Remi::SourceToTargetMap::Row
- Inherits:
-
Object
- Object
- Remi::SourceToTargetMap::Row
- Defined in:
- lib/remi/source_to_target_map/row.rb
Overview
Public: A row is composed of an array and an index hash. The index hash converts a key into a number representing the position in the array. Functionally, it’s very similar to how a hash works. However, we need to create a lot of Row objects that all have the same index hash. All of those row objects can reference the same index hash object and thus dramatically reduce the amount of memory needed store a lot of rows.
Examples
row = Row.new({ a: 1, b: 2}, ['alpha', 'beta'])
row[:a] #=> 'alpha'
row[:b] #=> 'beta'
Class Method Summary collapse
-
.[](arg) ⇒ Object
Public: Converts hash-like objects into rows, array-like objects into rows, or just returns a row if one is provied.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Public: Returns the value of the row array for the given key.
-
#[]=(key, value) ⇒ Object
Public: Sets the value of the row array for the given key.
-
#each(&block) ⇒ Object
Public: Makes Row enumerable, acts like a hash.
-
#each_source(&block) ⇒ Object
Public: Enumerates over each source value.
-
#each_target(&block) ⇒ Object
Public: Enumerates over each target value.
-
#initialize(index, values, source_keys: nil) ⇒ Row
constructor
Public: Initializes a row object.
-
#keys ⇒ Object
Public: Returns the keys of the index.
-
#source_keys ⇒ Object
Public: Returns all source keys.
-
#target_keys ⇒ Object
Public: Returns all target keys.
-
#to_a ⇒ Object
Public: Returns the values stored in the row.
Constructor Details
#initialize(index, values, source_keys: nil) ⇒ Row
Public: Initializes a row object.
index - A hash containing keys that are usually symbols and values that
represent a position in the values array.
values - An array of values. source_keys - Array of keys that should be treated as data
sources for a row transformation
46 47 48 49 50 |
# File 'lib/remi/source_to_target_map/row.rb', line 46 def initialize(index, values, source_keys: nil) @index = index @values = values @source_keys = source_keys end |
Class Method Details
.[](arg) ⇒ Object
Public: Converts hash-like objects into rows, array-like objects into rows, or just returns a row if one is provied.
arg - A Row, array-like object, or hash-like object.
Examples:
Row[{ a: 'one', b: 'two' }] #=> #<Row @index={:a=>0, :b=>1} @values=["one", "two"]>
Returns a Row
28 29 30 31 32 33 34 35 36 |
# File 'lib/remi/source_to_target_map/row.rb', line 28 def self.[](arg) return arg if arg.is_a? Row if arg.respond_to? :keys Row.new(arg.keys.each_with_index.to_h, arg.values) else Row.new(0.upto(arg.size).each_with_index.to_h, arg) end end |
Instance Method Details
#[](key) ⇒ Object
Public: Returns the value of the row array for the given key
53 54 55 |
# File 'lib/remi/source_to_target_map/row.rb', line 53 def [](key) @values[@index[key]] end |
#[]=(key, value) ⇒ Object
Public: Sets the value of the row array for the given key
58 59 60 |
# File 'lib/remi/source_to_target_map/row.rb', line 58 def []=(key, value) @values[@index[key]] = value end |
#each(&block) ⇒ Object
Public: Makes Row enumerable, acts like a hash
63 64 65 66 |
# File 'lib/remi/source_to_target_map/row.rb', line 63 def each &block return enumerate_row_variables unless block_given? enumerate_row_variables.each { |k,v| block.call(k,v) } end |
#each_source(&block) ⇒ Object
Public: Enumerates over each source value
70 71 72 73 74 |
# File 'lib/remi/source_to_target_map/row.rb', line 70 def each_source &block Enumerator.new do |y| source_keys.each { |key| y << [key, self[key]] } end end |
#each_target(&block) ⇒ Object
Public: Enumerates over each target value
77 78 79 80 81 |
# File 'lib/remi/source_to_target_map/row.rb', line 77 def each_target &block Enumerator.new do |y| target_keys.each { |key| y << [key, self[key]] } end end |
#keys ⇒ Object
Public: Returns the keys of the index.
89 90 91 |
# File 'lib/remi/source_to_target_map/row.rb', line 89 def keys @index.keys end |
#source_keys ⇒ Object
Public: Returns all source keys
94 95 96 |
# File 'lib/remi/source_to_target_map/row.rb', line 94 def source_keys @source_keys ||= @index.keys end |
#target_keys ⇒ Object
Public: Returns all target keys
99 100 101 |
# File 'lib/remi/source_to_target_map/row.rb', line 99 def target_keys @target_keys ||= keys - source_keys end |
#to_a ⇒ Object
Public: Returns the values stored in the row.
84 85 86 |
# File 'lib/remi/source_to_target_map/row.rb', line 84 def to_a @values end |