Class: Stockboy::Configurator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#new(dsl, file = ) ⇒ Configurator #new(&block) ⇒ Configurator

Evaluate DSL and capture configuration for building a job

Overloads:

  • #new(dsl, file = ) ⇒ Configurator

    Evaluate DSL from a string

    Parameters:

    • dsl (String)

      job template language for evaluation

    • file (String) (defaults to: )

      path to original file for reporting errors

  • #new(&block) ⇒ Configurator

    Evaluate DSL in a block



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

#configHash (readonly)

Captured job configuration options

Returns:

  • (Hash)


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

Examples:

attributes do
  first_name as: ->(raw){ raw["FullName"].split(" ").first }
  email      from: "RawEmail", as: [:string]
  check_in   from: "RawCheckIn", as: [:date]
end

Raises:

  • (ArgumentError)


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.

Examples:

filter :missing_email do |raw, out|
  raw["RawEmail"].empty?
end
filter :past_due do |raw, out|
  out.check_in < Date.today
end
filter :under_age, :check_id
filter :update, proc{ true } # capture all remaining items

Raises:

  • (ArgumentError)


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.

Examples:

trigger :cleanup do |job, *args|
  job.provider.delete_data
end

# elsewhere:
if MyProjects.find(123).import_records(job.records[:valid])
  job.cleanup
end

Parameters:

  • key (Symbol)

    Name of the trigger

Yield Parameters:

  • (Stockboy::Job)
  • Arguments (Array)

    passed to the action when called

Raises:

  • (ArgumentError)


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.

Examples:

provider :file, file_dir: "/downloads/@client" do
  file_name "example.csv"
end

Parameters:

  • provider_class (Symbol, Class, Provider)

    The registered symbol name for the provider, or actual provider

  • opts (Hash) (defaults to: {})

    Provider-specific options passed to the provider initializer

Returns:

Raises:

  • (ArgumentError)


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

Examples:

reader :csv do
  col_sep "|"
end

Parameters:

  • reader_class (Symbol, Class, Reader)

    The registered symbol name for the reader, or actual reader

  • opts (Hash) (defaults to: {})

    Provider-specific options passed to the provider initializer

Returns:

Raises:

  • (ArgumentError)


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_jobJob

Initialize a new job with the captured options

Returns:



172
173
174
# File 'lib/stockboy/configurator.rb', line 172

def to_job
  Job.new(@config)
end