Class: Remi::Job::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/remi/job/parameters.rb

Overview

A job parameter adds flexiblity to defining job templates. An instance of Parameters contains a collection of parameters that are evaluatin in the context of a job. It functions very similarly to Rspec's #let, in that in can be defined using a block of code that is only evaluated the first time it is used, and cached for later use.

Parameters should only be used in the context of a job.

Examples:

class MyJob < Remi::Job
  param(:my_param) { 'some parameter' }
  param :my_calculated_param do
    1.upto(1000).size
  end

  transform :something do
    puts "my_param is #{job.params[:my_param]}"
    puts "my_calculated_param is #{job.params[:my_calculated_param]}"
  end
end

job1 = MyJob.new
job1.execute
#=> my_param is some parameter
#=> my_calculated_param is 1000

job2 = MyJob.new
job2.params[:my_param] = 'override'
job2.execute
#=> my_param is override
#=> my_calculated_param is 1000

job3 = MyJob.new(my_param: 'constructor override', my_calculated_param: 322)
job3.execute
#=> my_param is constructor override
#=> my_calculated_param is 322

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Parameters

Returns a new instance of Parameters.



40
41
42
43
44
# File 'lib/remi/job/parameters.rb', line 40

def initialize(context=nil)
  @context = context
  @params_methods = []
  @params = {}
end

Instance Attribute Details

#contextObject

Returns The context in which parameter blocks will be evaluated.

Returns:

  • (Object)

    The context in which parameter blocks will be evaluated



47
48
49
# File 'lib/remi/job/parameters.rb', line 47

def context
  @context
end

Instance Method Details

#[](name) ⇒ Object

Get the value of a parameter

Parameters:

  • name (Symbol)

    The name of the parameter

Returns:

  • (Object)

    The value of the parameter

Raises:

  • (ArgumentError)


54
55
56
57
# File 'lib/remi/job/parameters.rb', line 54

def [](name)
  return send(name) if respond_to?(name)
  raise ArgumentError, "Job parameter #{name} is not defined"
end

#[]=(name, value) ⇒ Object

Set the value of a parameter

Parameters:

  • name (Symbol)

    The name of the parameter

  • value (Object)

    The new value of the parameter

Returns:

  • (Object)

    The new value of the parameter



66
67
68
69
70
71
# File 'lib/remi/job/parameters.rb', line 66

def []=(name, value)
  __define__(name) { value } unless respond_to? name
  @params[name] = value

  value
end

#__define__(name, &block) ⇒ Object



87
88
89
90
91
92
# File 'lib/remi/job/parameters.rb', line 87

def __define__(name, &block)
  @params_methods << name unless @params_methods.include? name
  define_singleton_method name do
    @params.fetch(name) { |name| @params[name] = Remi::Dsl.dsl_return(self, @context, &block) }
  end
end

#cloneJob::Parameters

Returns A clone of this parameter set.

Returns:



80
81
82
83
84
85
# File 'lib/remi/job/parameters.rb', line 80

def clone
  the_clone = super
  the_clone.instance_variable_set(:@params, @params.dup)
  the_clone.instance_variable_set(:@params_methods, @params_methods.dup)
  the_clone
end

#to_hHash

Returns The evaluated parameters as a hash.

Returns:

  • (Hash)

    The evaluated parameters as a hash



74
75
76
77
# File 'lib/remi/job/parameters.rb', line 74

def to_h
  @params_methods.each { |p| self.send(p) }
  @params
end