Class: Antimony::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/antimony/formula.rb

Constant Summary collapse

LOG_PARENT_PATH =
Dir.pwd + '/log'
LOG_PATH =
"#{LOG_PARENT_PATH}/formulas".freeze
FORMULAS_PATH =
Dir.pwd + '/formulas'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, inputs = {}) ⇒ Formula

Returns a new instance of Formula.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/antimony/formula.rb', line 10

def initialize(name, inputs = {})
  ($LOAD_PATH.unshift FORMULAS_PATH) unless $LOAD_PATH.include? FORMULAS_PATH

  @inputs = indifferent_hash(inputs)
  @outputs = {}

  init_log(name)

  helper_path = "#{FORMULAS_PATH}/formula_helper.rb"

  eval File.read(helper_path) if File.exist?(helper_path) # rubocop:disable Lint/Eval

  @formula = File.read("#{FORMULAS_PATH}/#{name}.rb")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



48
49
50
51
# File 'lib/antimony/formula.rb', line 48

def method_missing(name, *args, &block)
  raise 'No active connection!' unless @connection
  @connection.send(name, *args, &block)
end

Instance Attribute Details

#formula_logObject

Returns the value of attribute formula_log.



8
9
10
# File 'lib/antimony/formula.rb', line 8

def formula_log
  @formula_log
end

#inputsObject

Returns the value of attribute inputs.



8
9
10
# File 'lib/antimony/formula.rb', line 8

def inputs
  @inputs
end

#outputsObject

Returns the value of attribute outputs.



8
9
10
# File 'lib/antimony/formula.rb', line 8

def outputs
  @outputs
end

Instance Method Details

#runObject



25
26
27
# File 'lib/antimony/formula.rb', line 25

def run
  eval(@formula) # rubocop:disable Lint/Eval
end

#session(host, username, password) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/antimony/formula.rb', line 29

def session(host, username, password)
  @connection = Antimony::Session.new(host, username, password)
  yield if block_given?
  @log.info @connection.session_log
  @connection.close
  @connection = nil
end