Class: Smelter::ScriptRunner
- Inherits:
-
Object
- Object
- Smelter::ScriptRunner
- Defined in:
- lib/smelter/script_runner.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize ⇒ ScriptRunner
constructor
A new instance of ScriptRunner.
- #load_registration(opts) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #run(instance = {}) ⇒ Object
- #set_context(context) ⇒ Object
Constructor Details
#initialize ⇒ ScriptRunner
Returns a new instance of ScriptRunner.
7 8 9 |
# File 'lib/smelter/script_runner.rb', line 7 def initialize @attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/smelter/script_runner.rb', line 33 def method_missing(name, *args, &block) if block_given? attributes[name.to_s] = block else attributes[name.to_s] = args[0] end rescue RuntimeError => e if !!e.[/add a new key into hash during iteration/] super else raise e end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/smelter/script_runner.rb', line 4 def attributes @attributes end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/smelter/script_runner.rb', line 4 def context @context end |
#user ⇒ Object
Returns the value of attribute user.
5 6 7 |
# File 'lib/smelter/script_runner.rb', line 5 def user @user end |
Instance Method Details
#load_registration(opts) ⇒ Object
29 30 31 |
# File 'lib/smelter/script_runner.rb', line 29 def load_registration(opts) opts[:klass].find_by(name: opts[:name]) end |
#run(instance = {}) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/smelter/script_runner.rb', line 21 def run(instance={}) attributes.each do |attribute_name, value| result = value.is_a?(Proc) ? value.call(instance) : value instance[attribute_name.to_s] = result end instance end |
#set_context(context) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/smelter/script_runner.rb', line 11 def set_context(context) @context = context attrs = @context.is_a?(Hash) ? @context.keys : @context.members attrs.each do |attr| self.define_singleton_method(attr) do @context[attr] end end end |