Class: Optimus::Data
- Inherits:
-
Object
- Object
- Optimus::Data
- Defined in:
- lib/optimus_data.rb
Overview
A generalized data structure for optimus files – essentially just a table structure. I should be able to say: e_data = Optimus::Data.new e_data[0] for the first row / col e_data[‘ExperimentName’] for the experiment name e_data[0] = “foo” e_data.add_row e_data[‘kitteh’] = “cheezburger” For querying: Indexing numerically out of bounds should raise an exception Indexing textwise out of bounds should raise an exception For setting: Indexing numerically out of bounds should raise an exception Indexing textwise out of bounds should add a column So… you might reasonably do r = e_data.new_row() r = ‘3521’ One last thing: if you care about column ordering, but may be adding data in an arbitrary order (example: reading E-Prime log files), you can force a column order by passing an array of strings to Optimus::Data.new
Defined Under Namespace
Classes: Row
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
Instance Method Summary collapse
- #add_row ⇒ Object
- #dup ⇒ Object (also: #to_optimus_data)
- #find_column_index(col_id) ⇒ Object
- #find_or_add_column_index(col_id) ⇒ Object
-
#initialize(columns = [], options = {}) ⇒ Data
constructor
A new instance of Data.
-
#merge(*datasets) ⇒ Object
Returns a new Optimus::Data object containing the data from this and all other data sets.
-
#merge!(*datasets) ⇒ Object
Combine more Optimus::Data objects into this one, in-place.
-
#method_missing(method, *args, &block) ⇒ Object
We mostly delegate to our rows array.
- #sort!(&block) ⇒ Object
- #sort_by!(&block) ⇒ Object
Constructor Details
#initialize(columns = [], options = {}) ⇒ Data
Returns a new instance of Data.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/optimus_data.rb', line 37 def initialize(columns = [], = {}) = || [] @rows = [] @columns = [] @column_hash = {} @columns_set_in_initialize = false if (columns && columns.length > 0) add_columns!(columns) @columns_set_in_initialize = true end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
We mostly delegate to our rows array
97 98 99 |
# File 'lib/optimus_data.rb', line 97 def method_missing(method, *args, &block) @rows.send method, *args, &block end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
35 36 37 |
# File 'lib/optimus_data.rb', line 35 def columns @columns end |
Instance Method Details
#add_row ⇒ Object
101 102 103 104 105 |
# File 'lib/optimus_data.rb', line 101 def add_row() row = Row.new(self) @rows << row return row end |
#dup ⇒ Object Also known as: to_optimus_data
90 91 92 |
# File 'lib/optimus_data.rb', line 90 def dup Optimus::Data.new().merge!(self) end |
#find_column_index(col_id) ⇒ Object
107 108 109 |
# File 'lib/optimus_data.rb', line 107 def find_column_index(col_id) @column_hash[col_id] end |
#find_or_add_column_index(col_id) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/optimus_data.rb', line 111 def find_or_add_column_index(col_id) index_id = find_column_index(col_id) # If index_id was a string, nil means we may want to add it. If it's a # numeric index, we want to return nil from here -- we're not gonna add # unnamed indexes. return index_id if index_id or col_id.is_a?(Fixnum) # In this case, we're adding a column... index = @columns.size @columns << col_id @column_hash[col_id] = index @column_hash[index] = index index end |
#merge(*datasets) ⇒ Object
Returns a new Optimus::Data object containing the data from this and all other data sets
51 52 53 54 55 |
# File 'lib/optimus_data.rb', line 51 def merge(*datasets) cols = [self, *datasets].map { |d| d.columns }.flatten.uniq d = Optimus::Data.new(cols) return d.merge!(self, *datasets) end |
#merge!(*datasets) ⇒ Object
Combine more Optimus::Data objects into this one, in-place
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/optimus_data.rb', line 58 def merge!(*datasets) datasets.each do |source| add_columns!(source.columns) if source.columns == self.columns # The fast option source.each do |row| r = self.add_row r.sort_value = row.sort_value r.values = row.values end else # The general case option source.each do |row| r = self.add_row row.columns.each do |col| r[col] = row[col] end r.sort_value = row.sort_value end end end return self end |
#sort!(&block) ⇒ Object
82 83 84 |
# File 'lib/optimus_data.rb', line 82 def sort!(&block) @rows = @rows.sort(&block) end |
#sort_by!(&block) ⇒ Object
86 87 88 |
# File 'lib/optimus_data.rb', line 86 def sort_by!(&block) @rows = @rows.sort_by(&block) end |