Class: RStore::CSV
- Inherits:
-
Object
- Object
- RStore::CSV
- Defined in:
- lib/rstore/csv.rb
Instance Attribute Summary (collapse)
-
- (Array<Data>) data_array
readonly
Holds
RStore::Dataobjects that are used internally to store information from a data source. -
- (BaseDB) database
readonly
A subclass of BaseDB.
-
- (BaseTable) table
readonly
A sublcass of BaseTable.
Class Method Summary (collapse)
-
+ change_default_options(options)
Change default options recognized by #from The new option values apply to all following instances of
RStore::CSVOptions can be reset to their defaults by calling CSV.reset_default_options See #from for a list of all options and their default values. -
+ query(db_table) {|table| ... }
Easy querying by yielding a Sequel::Dataset instance of your table.
-
+ reset_default_options
Reset the options recognized by #from to their default values.
Instance Method Summary (collapse)
-
- from(source, options = {})
Specify the source of the csv file(s) There can be several calls to this method on given instance of
RStore::CSV. -
- (CSV) initialize(&block)
constructor
This constructor takes a block yielding an implicit instance of self.
-
- (Boolean) ran_once?
Test if the data has been inserted into the database table.
-
- run
Start processing the csv files, storing the data into a database table.
-
- to(db_table)
Choose the database table to store the csv data into.
Constructor Details
- (CSV) initialize(&block)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rstore/csv.rb', line 37 def initialize &block @data_hash = {} @data_array = [] @database = nil @table = nil # Tracking method calls to #from, #to, and #run. @from = false @to = false @run = false instance_eval(&block) if block_given? end |
Instance Attribute Details
- (Array<Data>) data_array (readonly)
Holds RStore::Data objects that are used internally to store information from a data source.
20 21 22 |
# File 'lib/rstore/csv.rb', line 20 def data_array @data_array end |
Class Method Details
+ change_default_options(options)
This method returns an undefined value.
Change default options recognized by #from
The new option values apply to all following instances of RStore::CSV
Options can be reset to their defaults by calling reset_default_options
See #from for a list of all options and their default values.
275 276 277 |
# File 'lib/rstore/csv.rb', line 275 def self. Configuration.() end |
+ query(db_table) {|table| ... }
This method returns an undefined value.
Easy querying by yielding a Sequel::Dataset instance of your table.
245 246 247 248 249 250 |
# File 'lib/rstore/csv.rb', line 245 def self.query db_table, &block database, table = database_table(db_table) database.connect do |db| block.call(db[table.name]) if block_given? # Sequel::Dataset end end |
+ reset_default_options
This method returns an undefined value.
Reset the options recognized by #from to their default values.
285 286 287 |
# File 'lib/rstore/csv.rb', line 285 def self. Configuration. end |
Instance Method Details
- from(source, options) - from(source)
This method returns an undefined value.
Specify the source of the csv file(s)
There can be several calls to this method on given instance of RStore::CSV.
This method has to be called before #run.
89 90 91 92 93 |
# File 'lib/rstore/csv.rb', line 89 def from source, ={} crawler = FileCrawler.new(source, :csv, ) @data_hash.merge!(crawler.data_hash) @from = true end |
- (Boolean) ran_once?
Test if the data has been inserted into the database table.
261 262 263 |
# File 'lib/rstore/csv.rb', line 261 def ran_once? @run == true end |
- run
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rstore/csv.rb', line 132 def run return if ran_once? # Ignore subsequent calls to #run raise Exception, "At least one method 'from' has to be called before method 'run'" unless @from == true raise Exception, "Method 'to' has to be called before method 'run'" unless @to == true @data_hash.each do |path, data| content = read_data(data) @data_array << Data.new(path, content, :raw, data.) end @database.connect do |db| create_table(db) name = @table.name prepared_data_array = @data_array.map do |data| data.parse_csv.convert_fields(db, name) end insert_all(prepared_data_array, db, name) @run = true = <<-TEXT.gsub(/^\s+/, '') =============================== All data has been successfully inserted into table '#{database.name}.#{table.name}'" ------------------------------- You can retrieve all table data with the following code: ------------------------------- #{self.class}.query('#{database.name}.#{table.name}') do |table| table.all end =============================== TEXT puts end end |
- to(db_table)
This method returns an undefined value.
Choose the database table to store the csv data into. This method has to be called before #run.
107 108 109 110 |
# File 'lib/rstore/csv.rb', line 107 def to db_table @database, @table = CSV.database_table(db_table) @to = true end |