Class: DataMiner::Script
- Inherits:
-
Object
- Object
- DataMiner::Script
- Defined in:
- lib/data_miner/script.rb
Overview
The container that holds each step in the script.
Constant Summary collapse
- UNIQ_THREAD_VAR =
'DataMiner::Script.current_uniq'
- STACK_THREAD_VAR =
'DataMiner::Script.current_stack'
Instance Attribute Summary collapse
-
#steps ⇒ Array<DataMiner::Step>
readonly
The steps in the script.
Instance Method Summary collapse
-
#append(*args, &blk) ⇒ nil
Append a step to a script.
-
#append_once(*args, &blk) ⇒ nil
Append a step to a script unless it’s already there.
-
#import(description, settings) { ... } ⇒ nil
Import rows into your model.
-
#prepend(*args, &blk) ⇒ nil
Prepend a step to a script.
-
#prepend_once(*args, &blk) ⇒ nil
Prepend a step to a script unless it’s already there.
-
#process(method_id_or_description, &blk) ⇒ nil
Identify a single method or a define block of arbitrary code to be executed.
-
#sql(description, url_or_statement) ⇒ Object
Execute SQL, provided either as a string or a URL.
-
#start ⇒ DataMiner::Run
Run the script for this model.
Instance Attribute Details
#steps ⇒ Array<DataMiner::Step> (readonly)
The steps in the script.
46 47 48 |
# File 'lib/data_miner/script.rb', line 46 def steps @steps end |
Instance Method Details
#append(*args, &blk) ⇒ nil
Append a step to a script. Mostly for internal use.
195 196 197 198 |
# File 'lib/data_miner/script.rb', line 195 def append(*args, &blk) steps << make(*args, &blk) nil end |
#append_once(*args, &blk) ⇒ nil
Append a step to a script unless it’s already there. Mostly for internal use.
184 185 186 187 188 189 190 |
# File 'lib/data_miner/script.rb', line 184 def append_once(*args, &blk) step = make(*args, &blk) unless steps.include? step steps << step end nil end |
#import(description, settings) { ... } ⇒ nil
Be sure to check out github.com/seamusabshere/remote_table and github.com/seamusabshere/errata for available settings
.
There are hundreds of import
examples in github.com/brighterplanet/earth. The README points to a few (at the bottom.)
We often use string primary keys to make idempotency easier. github.com/seamusabshere/active_record_inline_schema supports defining these inline.
Enabling :validate
may slow down importing large files because it precludes bulk loading using github.com/seamusabshere/upsert.
Import rows into your model.
As long as…
-
you
key
on the primary key, or -
the table has an auto-increment primary key, or
-
you DON’T enable
:validate
… then things will be sped up using the upsert library in streaming mode.
Otherwise, native ActiveRecord
constuctors and validations will be used.
140 141 142 |
# File 'lib/data_miner/script.rb', line 140 def import(description, settings, &blk) append(:import, description, settings, &blk) end |
#prepend(*args, &blk) ⇒ nil
Prepend a step to a script. Mostly for internal use.
176 177 178 179 |
# File 'lib/data_miner/script.rb', line 176 def prepend(*args, &blk) steps.unshift make(*args, &blk) nil end |
#prepend_once(*args, &blk) ⇒ nil
Prepend a step to a script unless it’s already there. Mostly for internal use.
165 166 167 168 169 170 171 |
# File 'lib/data_miner/script.rb', line 165 def prepend_once(*args, &blk) step = make(*args, &blk) unless steps.include? step steps.unshift step end nil end |
#process(method_id) ⇒ nil #process(description) { ... } ⇒ nil
Identify a single method or a define block of arbitrary code to be executed.
91 92 93 |
# File 'lib/data_miner/script.rb', line 91 def process(method_id_or_description, &blk) append(:process, method_id_or_description, &blk) end |
#sql(description, url_or_statement) ⇒ Object
url_or_statement
is auto-detected by looking for %r{^[^s]/[^]} (non-spaces followed by a slash followed by non-asterisk). Therefore if you’re passing a local file path and want it to be treated like a URL, make it absolute.
Execute SQL, provided either as a string or a URL.
158 159 160 |
# File 'lib/data_miner/script.rb', line 158 def sql(description, url_or_statement) append(:sql, description, url_or_statement) end |
#start ⇒ DataMiner::Run
Normally you should use Country.run_data_miner!
A primitive “call stack” is kept that will prevent infinite loops. So, if Country’s data miner script calls Province’s AND vice-versa, each one will only be run once.
Run the script for this model. Mostly for internal use.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/data_miner/script.rb', line 206 def start model_name = model.name # $stderr.write "0 - #{model_name}\n" # $stderr.write "A - current_uniq - #{Script.current_uniq ? 'true' : 'false'}\n" # $stderr.write "B - #{Script.current_stack.join(',')}\n" if Script.current_uniq and Script.current_stack.include?(model_name) # we've already done this in the current stack, so skip it return end if not Script.current_uniq # since we're not trying to uniq, ignore the current contents of the stack Script.current_stack.clear end Script.current_stack << model_name unless Run.table_exists? Run.auto_upgrade! end run = Run.new run.model_name = model_name run.start do steps.each do |step| step.start model.reset_column_information end end end |