Class: Optimus::Data

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = [], options = {})
  @options = options || []
  @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

#columnsObject (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_rowObject



101
102
103
104
105
# File 'lib/optimus_data.rb', line 101

def add_row()
  row = Row.new(self)
  @rows << row
  return row
end

#dupObject 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