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 evaluated parameters as a hash.
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
#context ⇒ Object
Returns 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
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
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 |
#clone ⇒ Job::Parameters
Returns A clone of this parameter set.
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_h ⇒ Hash
Returns 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 |