Sha256: 43d0eadb329390c97a20cfeb5524b99305c95588be6fe90be3ac6137870a1fef

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 KB

Contents

class RemoteTable
  class Transform
    attr_accessor :select, :reject, :transform_class, :transform_options, :transform, :raw_table
    attr_accessor :errata
    
    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
    
    # the null transformation
    def apply(raw_table)
      self.raw_table = raw_table
      self
    end
    
    # - convert OrderedHash to a Hash (otherwise field ordering will be saved)
    # - dump it
    # - digest it
    def self.row_hash(row)
      Digest::MD5.hexdigest Marshal.dump(Hash.new.replace(row))
    end
    
    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
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
remote_table-ruby19-0.2.30 lib/remote_table/transform.rb
remote_table-0.2.30 lib/remote_table/transform.rb
remote_table-0.2.29 lib/remote_table/transform.rb
remote_table-0.2.28 lib/remote_table/transform.rb
remote_table-0.2.27 lib/remote_table/transform.rb
remote_table-0.2.26 lib/remote_table/transform.rb
remote_table-0.2.24 lib/remote_table/transform.rb
remote_table-0.2.23 lib/remote_table/transform.rb
remote_table-0.2.22 lib/remote_table/transform.rb
remote_table-0.2.21 lib/remote_table/transform.rb
remote_table-0.2.20 lib/remote_table/transform.rb