Class: Remi::Job::Parameters
- Inherits:
-
Object
- Object
- Remi::Job::Parameters
- 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.
Instance Attribute Summary collapse
-
#context ⇒ Object
The context in which parameter blocks will be evaluated.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get the value of a parameter.
-
#[]=(name, value) ⇒ Object
Set the value of a parameter.
- #__define__(name, &block) ⇒ Object
-
#clone ⇒ Job::Parameters
A clone of this parameter set.
-
#initialize(context = nil) ⇒ Parameters
constructor
A new instance of Parameters.
-
#to_h ⇒ Hash
The parameters as a hash.
Constructor Details
#initialize(context = nil) ⇒ Parameters
40 41 42 43 |
# File 'lib/remi/job/parameters.rb', line 40 def initialize(context=nil) @context = context @params = {} end |
Instance Attribute Details
#context ⇒ Object
46 47 48 |
# File 'lib/remi/job/parameters.rb', line 46 def context @context end |
Instance Method Details
#[](name) ⇒ Object
Get the value of a parameter
53 54 55 56 |
# File 'lib/remi/job/parameters.rb', line 53 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
65 66 67 68 |
# File 'lib/remi/job/parameters.rb', line 65 def []=(name, value) __define__(name) { value } unless respond_to? name @params[name] = value end |
#__define__(name, &block) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/remi/job/parameters.rb', line 82 def __define__(name, &block) @params[name] = nil define_singleton_method name do @params[name] ||= Remi::Dsl.dsl_return(self, @context, &block) end end |
#clone ⇒ Job::Parameters
76 77 78 79 80 |
# File 'lib/remi/job/parameters.rb', line 76 def clone the_clone = super the_clone.instance_variable_set(:@params, @params.dup) the_clone end |
#to_h ⇒ Hash
71 72 73 |
# File 'lib/remi/job/parameters.rb', line 71 def to_h @params end |