Class: RemoteTable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/remote_table.rb,
lib/remote_table/format.rb,
lib/remote_table/hasher.rb,
lib/remote_table/version.rb,
lib/remote_table/executor.rb,
lib/remote_table/local_file.rb,
lib/remote_table/properties.rb,
lib/remote_table/format/html.rb,
lib/remote_table/transformer.rb,
lib/remote_table/format/excel.rb,
lib/remote_table/format/excelx.rb,
lib/remote_table/format/delimited.rb,
lib/remote_table/format/fixed_width.rb,
lib/remote_table/format/open_office.rb,
lib/remote_table/format/mixins/rooable.rb,
lib/remote_table/format/mixins/textual.rb

Defined Under Namespace

Classes: Executor, Format, Hasher, LocalFile, Properties, Transform, Transformer

Constant Summary collapse

VERSION =
"1.1.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RemoteTable

Create a new RemoteTable.

RemoteTable.new(url, options = {})

New syntax:

RemoteTable.new('www.customerreferenceprogram.org/uploads/CRP_RFP_template.xlsx', 'foo' => 'bar')

Old syntax:

RemoteTable.new(:url => 'www.customerreferenceprogram.org/uploads/CRP_RFP_template.xlsx', :foo => 'bar')

See the Properties object for the sorts of options you can pass.



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

def initialize(*args)
  @options = args.last.is_a?(::Hash) ? args.last.dup : {}
  @options.stringify_keys!
  @url = if args.first.is_a? ::String
    args.first.dup
  else
    @options['url'].dup
  end
  @url.freeze
  @options.freeze
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/remote_table.rb', line 32

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



31
32
33
# File 'lib/remote_table.rb', line 31

def url
  @url
end

Class Method Details

.executorObject

Used internally to execute stuff in shells.



95
96
97
# File 'lib/remote_table.rb', line 95

def self.executor
  Executor.instance
end

.hasherObject

Used internally to create unique hashes of rows.



100
101
102
# File 'lib/remote_table.rb', line 100

def self.hasher
  Hasher.instance
end

Instance Method Details

#[](row_number) ⇒ Object

Get a row by row number



82
83
84
# File 'lib/remote_table.rb', line 82

def [](row_number)
  to_a[row_number]
end

#each(&blk) ⇒ Object Also known as: each_row



56
57
58
# File 'lib/remote_table.rb', line 56

def each(&blk)
  to_a.each { |row| yield row }
end

#formatObject

Used internally to access to the driver that reads the format



115
116
117
# File 'lib/remote_table.rb', line 115

def format
  @format ||= properties.format.new self
end

#freeObject

clear the row cache to save memory



87
88
89
90
91
92
# File 'lib/remote_table.rb', line 87

def free
  @to_a.clear if @to_a.is_a?(::Array)
  @to_a = nil
  ::GC.start
  nil
end

#local_fileObject

Used internally to access to a downloaded copy of the file



105
106
107
# File 'lib/remote_table.rb', line 105

def local_file
  @local_file ||= LocalFile.new self
end

#propertiesObject

Used internally to access to the properties of the table, either set by the user or implied



110
111
112
# File 'lib/remote_table.rb', line 110

def properties
  @properties ||= Properties.new self
end

#to_aObject Also known as: rows



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/remote_table.rb', line 61

def to_a
  return @to_a if @to_a.is_a? ::Array
  @to_a = []
  format.each do |row|
    row['row_hash'] = ::RemoteTable.hasher.hash row
    # allow the transformer to return multiple "virtual rows" for every real row
    ::Array.wrap(transformer.transform(row)).each do |virtual_row|
      if properties.errata
        next if properties.errata.rejects? virtual_row
        properties.errata.correct! virtual_row
      end
      next if properties.select and !properties.select.call(virtual_row)
      next if properties.reject and properties.reject.call(virtual_row)
      @to_a.push virtual_row
    end
  end
  @to_a
end

#transformerObject

Used internally to acess the transformer (aka parser).



120
121
122
# File 'lib/remote_table.rb', line 120

def transformer
  @transformer ||= Transformer.new self
end