Class: Opera::Operation::Base

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/opera/operation/base.rb

Constant Summary

Constants included from Builder

Opera::Operation::Builder::INSTRUCTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

included

Constructor Details

#initialize(params: {}, dependencies: {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
# File 'lib/opera/operation/base.rb', line 11

def initialize(params: {}, dependencies: {})
  @context = {}
  @finished = false
  @result = Result.new
  @params = params.freeze
  @dependencies = dependencies.freeze
  config
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/opera/operation/base.rb', line 8

def context
  @context
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



9
10
11
# File 'lib/opera/operation/base.rb', line 9

def dependencies
  @dependencies
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/opera/operation/base.rb', line 9

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/opera/operation/base.rb', line 9

def result
  @result
end

Class Method Details

.call(args = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/opera/operation/base.rb', line 33

def call(args = {})
  operation = new(params: args.fetch(:params, {}), dependencies: args.fetch(:dependencies, {}))
  executor = Executor.new(operation)
  executor.evaluate_instructions(instructions)
  executor.result
end

.check_method_availability!(method) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
# File 'lib/opera/operation/base.rb', line 52

def check_method_availability!(method)
  return if instance_methods(false).none?(method)

  raise(ArgumentError, "Method #{method} is already defined")
end

.configObject



40
41
42
# File 'lib/opera/operation/base.rb', line 40

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



44
45
46
# File 'lib/opera/operation/base.rb', line 44

def configure
  yield config
end

.context(&blk) ⇒ Object



58
59
60
# File 'lib/opera/operation/base.rb', line 58

def context(&blk)
  AttributesDSL.new(klass: self, block_name: :context, allowed: [:attr_reader, :attr_accessor]).instance_exec(&blk)
end

.dependencies(&blk) ⇒ Object



66
67
68
# File 'lib/opera/operation/base.rb', line 66

def dependencies(&blk)
  AttributesDSL.new(klass: self, block_name: :dependencies).instance_exec(&blk)
end

.params(&blk) ⇒ Object



62
63
64
# File 'lib/opera/operation/base.rb', line 62

def params(&blk)
  AttributesDSL.new(klass: self, block_name: :params).instance_exec(&blk)
end

.reporterObject



48
49
50
# File 'lib/opera/operation/base.rb', line 48

def reporter
  config.reporter
end

Instance Method Details

#configObject



20
21
22
# File 'lib/opera/operation/base.rb', line 20

def config
  self.class.config
end

#finish!Object



24
25
26
# File 'lib/opera/operation/base.rb', line 24

def finish!
  @finished = true
end

#finished?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/opera/operation/base.rb', line 28

def finished?
  @finished
end