Class: SimpleTools::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_tools/operation.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Operation

Returns a new instance of Operation.



23
24
25
26
27
# File 'lib/simple_tools/operation.rb', line 23

def initialize(params)
  @errors = {}
  @context = {}
  @params = params
end

Class Attribute Details

.stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/simple_tools/operation.rb', line 5

def context
  @context
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/simple_tools/operation.rb', line 5

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/simple_tools/operation.rb', line 5

def params
  @params
end

Class Method Details

.call(params = {}) ⇒ Object



10
11
12
13
14
# File 'lib/simple_tools/operation.rb', line 10

def call(params = {})
  instance = new(params)
  instance.call
  instance
end

.step(key) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/simple_tools/operation.rb', line 16

def step(key)
  raise(ArgumentError, 'symbol should be given') unless key.is_a?(Symbol)

  (@steps ||= []).push(key.to_sym)
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
# File 'lib/simple_tools/operation.rb', line 29

def call
  raise(ArgumentError, 'steps are not defined') unless self.class.steps

  self.class.steps.each do |step|
    public_send(step)
    break unless success?
  end
end

#failure?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/simple_tools/operation.rb', line 43

def failure?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/simple_tools/operation.rb', line 38

def success?
  errors.empty?
  # force developer to write descriptions of errors
end