Class: Stockboy::Configurator
- Inherits:
-
Object
- Object
- Stockboy::Configurator
- Defined in:
- lib/stockboy/configurator.rb
Overview
Context for evaluating DSL templates and capturing job options for initializing a job.
Wraps up the DSL methods called in job templates and handles the construction of the job’s provider
, reader
, attributes
, and filters
.
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
Captured job configuration options.
Instance Method Summary collapse
-
#attribute(key, opts = {}) ⇒ Object
Add individual attribute mapping rules.
-
#attributes(&block) ⇒ Object
Configure the attribute map for data records.
-
#filter(key, callable = nil, *args, &block) ⇒ Object
Add a filter to the processing filter chain.
-
#initialize(dsl = '', file = __FILE__, &block) ⇒ Configurator
constructor
Evaluate DSL and capture configuration for building a job.
-
#on(key) {|, Arguments| ... } ⇒ Object
Register a trigger to notify the job of external events.
-
#provider(key, opts = {}, &block) ⇒ Provider
(also: #connection)
Configure the provider for fetching data.
-
#reader(key, opts = {}, &block) ⇒ Reader
(also: #format)
Configure the reader for parsing data.
-
#repeat(&block) ⇒ Object
Configure repeating the provider for fetching multiple parts.
-
#to_job ⇒ Job
Initialize a new job with the captured options.
Constructor Details
#new(dsl, file = ) ⇒ Configurator #new(&block) ⇒ Configurator
Evaluate DSL and capture configuration for building a job
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/stockboy/configurator.rb', line 32 def initialize(dsl='', file=__FILE__, &block) @config = {} @config[:triggers] = Hash.new { |hash, key| hash[key] = [] } @config[:filters] = {} if block_given? instance_eval(&block) else instance_eval(dsl, file) end end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Captured job configuration options
21 22 23 |
# File 'lib/stockboy/configurator.rb', line 21 def config @config end |
Instance Method Details
#attribute(key, opts = {}) ⇒ Object
Add individual attribute mapping rules
144 145 146 147 |
# File 'lib/stockboy/configurator.rb', line 144 def attribute(key, opts={}) @config[:attributes] ||= AttributeMap.new @config[:attributes].insert(key, opts) end |
#attributes(&block) ⇒ Object
Configure the attribute map for data records
This will replace any existing attributes with a new set.
130 131 132 133 134 |
# File 'lib/stockboy/configurator.rb', line 130 def attributes(&block) raise ArgumentError unless block_given? @config[:attributes] = AttributeMap.new(&block) end |
#filter(key, callable = nil, *args, &block) ⇒ Object
Add a filter to the processing filter chain
-
Must be called with either a callable argument (proc) or a block.
-
Must be called in the order that filters should be applied.
164 165 166 167 168 |
# File 'lib/stockboy/configurator.rb', line 164 def filter(key, callable=nil, *args, &block) filter = Filters.build(callable, args, block) || block filter or raise ArgumentError, "Missing filter arguments for #{key}" @config[:filters][key] = filter end |
#on(key) {|, Arguments| ... } ⇒ Object
Register a trigger to notify the job of external events
Useful for adding generic control over the job’s resources from your app. For example, if you need to record stats or clean up data after your application has successfully processed the records, these actions can be defined within the context of each job template.
191 192 193 194 195 |
# File 'lib/stockboy/configurator.rb', line 191 def on(key, &block) raise(ArgumentError, "no block given") unless block_given? @config[:triggers][key] << block end |
#provider(key, opts = {}, &block) ⇒ Provider Also known as: connection
Configure the provider for fetching data
The optional block is evaluated in the provider’s own DSL context.
59 60 61 |
# File 'lib/stockboy/configurator.rb', line 59 def provider(key, opts={}, &block) @config[:provider] = Providers.build(key, opts, block) end |
#reader(key, opts = {}, &block) ⇒ Reader Also known as: format
Configure the reader for parsing data
114 115 116 |
# File 'lib/stockboy/configurator.rb', line 114 def reader(key, opts={}, &block) @config[:reader] = Readers.build(key, opts, block) end |
#repeat(&block) ⇒ Object
Configure repeating the provider for fetching multiple parts
If the provider needs to give us all the data as a series of requests, for example multiple HTTP pages or FTP files, the repeat block can be used to define the iteration for fetching each item.
The ‘<<` interface used here is defined by Ruby’s Enumerator.new block syntax. For each page that needs to be fetched, the provider options need to be altered and pushed on to the output. Control will be yielded to the reader at each iteration.
92 93 94 95 96 97 98 |
# File 'lib/stockboy/configurator.rb', line 92 def repeat(&block) unless block_given? && block.arity == 2 raise ArgumentError, "repeat block must accept |output, provider| arguments" end @config[:repeat] = block end |