Class: RemoteTable::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_table/transform.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bus) ⇒ Transform

Returns a new instance of Transform.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/remote_table/transform.rb', line 6

def initialize(bus)
  if transform_params = bus.delete(:transform)
    @transform_class = transform_params.delete(:class)
    @transform_options = transform_params
    @transform = @transform_class.new(@transform_options)
    @transform.add_hints!(bus)
  end
  @select = bus[:select]
  @reject = bus[:reject]
  @errata = bus[:errata]
end

Instance Attribute Details

#errataObject

Returns the value of attribute errata.



4
5
6
# File 'lib/remote_table/transform.rb', line 4

def errata
  @errata
end

#raw_tableObject

Returns the value of attribute raw_table.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def raw_table
  @raw_table
end

#rejectObject

Returns the value of attribute reject.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def reject
  @reject
end

#selectObject

Returns the value of attribute select.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def select
  @select
end

#transformObject

Returns the value of attribute transform.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def transform
  @transform
end

#transform_classObject

Returns the value of attribute transform_class.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def transform_class
  @transform_class
end

#transform_optionsObject

Returns the value of attribute transform_options.



3
4
5
# File 'lib/remote_table/transform.rb', line 3

def transform_options
  @transform_options
end

Class Method Details

.row_hash(row) ⇒ Object

  • convert it to a plain hash for whatever ruby version you’re on

  • dump it

  • digest it



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/remote_table/transform.rb', line 27

def self.row_hash(row)
  plain_hsh = if RUBY_VERSION >= '1.9'
    row.keys.sort.inject(::Hash.new) do |memo, key|
      value = row[key]
      key = key.to_s.toutf8
      value = value.to_s.toutf8 if value.respond_to? :to_s
      memo[key] = value
      memo
    end
  else
    ::Hash.new.replace(row)
  end
  ::Digest::MD5.hexdigest ::Marshal.dump(plain_hsh)
end

Instance Method Details

#apply(raw_table) ⇒ Object

the null transformation



19
20
21
22
# File 'lib/remote_table/transform.rb', line 19

def apply(raw_table)
  self.raw_table = raw_table
  self
end

#each_row(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/remote_table/transform.rb', line 42

def each_row(&block)
  raw_table.each_row do |row|
    row['row_hash'] = self.class.row_hash(row)
    virtual_rows = transform ? transform.apply(row) : row # allow transform.apply(row) to return multiple rows
    Array.wrap(virtual_rows).each do |virtual_row|
      if errata
        next if errata.rejects? virtual_row
        errata.correct! virtual_row
      end
      next if select and !select.call(virtual_row)
      next if reject and reject.call(virtual_row)
      yield virtual_row
    end
  end
end