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
-
#attributes(&block) ⇒ Object
DSL method for configuring the attribute map in a block.
-
#filter(key, callable = nil, *args, &block) ⇒ Object
DSL method to add a filter to the filter chain.
-
#initialize(dsl = '', file = __FILE__, &block) ⇒ Configurator
constructor
Evaluate DSL and capture configuration for building a job.
-
#on(key) {|, Arguments| ... } ⇒ Object
DSL method to register a trigger to notify the job of an event.
-
#provider(provider_class, opts = {}, &block) ⇒ Provider
(also: #connection)
DSL method for configuring the provider.
-
#reader(reader_class, opts = {}, &block) ⇒ Reader
(also: #format)
DSL method for configuring the reader.
-
#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
#attributes(&block) ⇒ Object
DSL method for configuring the attribute map in a block
110 111 112 113 114 |
# File 'lib/stockboy/configurator.rb', line 110 def attributes(&block) raise ArgumentError unless block_given? @config[:attributes] = AttributeMap.new(&block) end |
#filter(key, callable = nil, *args, &block) ⇒ Object
DSL method to add a filter to the 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.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/stockboy/configurator.rb', line 131 def filter(key, callable=nil, *args, &block) raise ArgumentError unless key if callable.is_a?(Symbol) callable = Filters.find(callable) callable = callable.new(*args) if callable.is_a? Class end raise ArgumentError unless callable.respond_to?(:call) ^ block_given? @config[:filters][key] = block || callable end |
#on(key) {|, Arguments| ... } ⇒ Object
DSL method to register a trigger to notify the job of an event
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.
163 164 165 166 |
# File 'lib/stockboy/configurator.rb', line 163 def on(key, &block) raise(ArgumentError, "no block given") unless block_given? @config[:triggers][key] << block end |
#provider(provider_class, opts = {}, &block) ⇒ Provider Also known as: connection
DSL method for configuring the provider
The optional block is evaluated in the provider’s own DSL context.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stockboy/configurator.rb', line 59 def provider(provider_class, opts={}, &block) raise ArgumentError unless provider_class @config[:provider] = case provider_class when Symbol Providers.find(provider_class).new(opts, &block) when Class provider_class.new(opts, &block) else provider_class end end |
#reader(reader_class, opts = {}, &block) ⇒ Reader Also known as: format
DSL method for configuring the reader
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/stockboy/configurator.rb', line 87 def reader(reader_class, opts={}, &block) raise ArgumentError unless reader_class @config[:reader] = case reader_class when Symbol Readers.find(reader_class).new(opts, &block) when Class reader_class.new(opts, &block) else reader_class end end |
#to_job ⇒ Job
Initialize a new job with the captured options
172 173 174 |
# File 'lib/stockboy/configurator.rb', line 172 def to_job Job.new(@config) end |