Class: Munger::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/munger/data.rb

Overview

this class is a data munger

it takes raw data (arrays of hashes, basically) 
and can manipulate it in various interesting ways

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Data

will accept active record collection or array of hashes

Yields:

  • (_self)

Yield Parameters:

  • _self (Munger::Data)

    the object that the method was called on



11
12
13
14
# File 'lib/munger/data.rb', line 11

def initialize(options = {})
  @data = options[:data] if options[:data]
  yield self if block_given?
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/munger/data.rb', line 8

def data
  @data
end

Class Method Details

.array(string_or_array) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/munger/data.rb', line 193

def self.array(string_or_array)
  if string_or_array.is_a? Array
    return string_or_array
  else
    return [string_or_array]
  end
end

.load_data(data, options = {}) ⇒ Object

– NOTE: The name seems redundant; why:

Munger::Data.load_data(data)

and not:

Munger::Data.load(data)

++



37
38
39
# File 'lib/munger/data.rb', line 37

def self.load_data(data, options = {})
  Data.new(:data => data)
end

Instance Method Details

#<<(data) ⇒ Object



16
17
18
# File 'lib/munger/data.rb', line 16

def <<(data)
  add_data(data)
end

#add_column(names, options = {}) ⇒ Object Also known as: add_columns, transform_column, transform_columns

:default: The default value to use for the column in existing rows.

Set to nil if not specified.

if a block is passed, you can set the values manually



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/munger/data.rb', line 50

def add_column(names, options = {})
  default = options[:default] || nil
  @data.each_with_index do |row, index|
    if block_given?
      col_data = yield Item.ensure(row)
    else
      col_data = default
    end
    
    if names.is_a? Array
      names.each_with_index do |col, i|
        row[col] = col_data[i]
      end
    else
      row[names] = col_data
    end
    @data[index] = clean_data(row)
  end
end

#add_data(data) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/munger/data.rb', line 20

def add_data(data)
  if @data
    @data = @data + data 
  else
    @data = data
  end
  @data
end

#clean_data(hash_or_ar) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/munger/data.rb', line 73

def clean_data(hash_or_ar)
  if hash_or_ar.is_a? Hash
    return Item.ensure(hash_or_ar)
  elsif hash_or_ar.respond_to? :attributes
    return Item.ensure(hash_or_ar.attributes)
  end
  hash_or_ar
end

#columnsObject



41
42
43
44
45
# File 'lib/munger/data.rb', line 41

def columns
  @columns ||= clean_data(@data.first).to_hash.keys
rescue
  puts clean_data(@data.first).to_hash.inspect
end

#filter_rowsObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/munger/data.rb', line 82

def filter_rows
  new_data = []
  
  @data.each do |row|
    row = Item.ensure(row)
    if (yield row)
      new_data << row
    end
  end
  
  @data = new_data
end

#group(groups, agg_hash = {}) ⇒ Object

group the data like sql



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/munger/data.rb', line 96

def group(groups, agg_hash = {})
  data_hash = {}
  
  agg_columns = []
  agg_hash.each do |key, columns|
    Data.array(columns).each do |col|  # column name
      agg_columns << col
    end
  end
  agg_columns = agg_columns.uniq.compact
  
  @data.each do |row|
    row_key = Data.array(groups).map { |rk| row[rk] }
    data_hash[row_key] ||= {:cells => {}, :data => {}, :count => 0}
    focus = data_hash[row_key]
    focus[:data] = clean_data(row)
    
    agg_columns.each do |col|
      focus[:cells][col] ||= []
      focus[:cells][col] << row[col]
    end
    focus[:count] += 1
  end
        
  new_data = []
  new_keys = []
  
  data_hash.each do |row_key, data|
    new_row = data[:data]
    agg_hash.each do |key, columns|
      Data.array(columns).each do |col|  # column name
        newcol = ''
        if key.is_a?(Array) && key[1].is_a?(Proc)
          newcol = key[0].to_s + '_' + col.to_s
          new_row[newcol] = key[1].call(data[:cells][col])
        else  
          newcol = key.to_s + '_' + col.to_s
          case key
          when :average
            sum = data[:cells][col].inject { |sum, a| sum + a }
            new_row[newcol] = (sum / data[:count])  
          when :count
            new_row[newcol] = data[:count]  
          else            
            new_row[newcol] = data[:cells][col].inject { |sum, a| sum + a }
          end
        end
        new_keys << newcol
      end
    end
    new_data << Item.ensure(new_row)
  end
  
  @data = new_data
  new_keys.compact
end

#pivot(columns, rows, value, aggregation = :sum) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/munger/data.rb', line 153

def pivot(columns, rows, value, aggregation = :sum)
  data_hash = {}
  
  @data.each do |row|
    column_key = Data.array(columns).map { |rk| row[rk] }
    row_key = Data.array(rows).map { |rk| row[rk] }
    data_hash[row_key] ||= {}
    data_hash[row_key][column_key] ||= {:sum => 0, :data => {}, :count => 0}
    focus = data_hash[row_key][column_key]
    focus[:data] = clean_data(row)
    focus[:count] += 1
    focus[:sum] += row[value]
  end
  
  new_data = []
  new_keys = {}
  
  data_hash.each do |row_key, row_hash|
    new_row = {}
    row_hash.each do |column_key, row_data|
      column_key.each do |ckey|
        new_row.merge!(row_data[:data])
        case aggregation
        when :average
          new_row[ckey] = (row_data[:sum] / row_data[:count])
        when :count
          new_row[ckey] = row_data[:count]
        else            
          new_row[ckey] = row_data[:sum]
        end
        new_keys[ckey] = true
      end
    end
    new_data << Item.ensure(new_row)
  end
  
  @data = new_data
  new_keys.keys
end

#sizeObject Also known as: length



201
202
203
# File 'lib/munger/data.rb', line 201

def size
  @data.size
end

#to_a(cols = nil) ⇒ Object

cols is an array of column names, if given, the nested arrays are built in this order



222
223
224
225
226
227
228
229
# File 'lib/munger/data.rb', line 222

def to_a(cols=nil)
  array = []
  cols ||= self.columns
  @data.each do |row|
    array << cols.inject([]){ |a,col| a << row[col] }
  end
  array
end

#valid?Boolean

Returns:

  • (Boolean)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/munger/data.rb', line 206

def valid?
  if ((@data.size > 0) &&
    (@data.respond_to? :each_with_index) &&
    (@data.first.respond_to?(:keys) || 
     @data.first.respond_to?(:attributes) || 
     @data.first.is_a?(Munger::Item))) &&
    (!@data.first.is_a? String)
    return true
  else
    return false
  end
rescue
  false
end