Class: Smelter::ScriptRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/smelter/script_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScriptRunner

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.message[/add a new key into hash during iteration/]
    super
  else
    raise e
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/smelter/script_runner.rb', line 4

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/smelter/script_runner.rb', line 4

def context
  @context
end

#userObject

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