Class: Yaso::Service

Inherits:
Object
  • Object
show all
Extended by:
Stepable
Defined in:
lib/yaso/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Stepable

failure, steps

Constructor Details

#initialize(context) ⇒ Service

Returns a new instance of Service.



9
10
11
12
# File 'lib/yaso/service.rb', line 9

def initialize(context)
  @context = context
  @success = true
end

Instance Attribute Details

#success=(value) ⇒ Object (writeonly)

Sets the attribute success

Parameters:

  • value

    the value to set the attribute success to.



7
8
9
# File 'lib/yaso/service.rb', line 7

def success=(value)
  @success = value
end

Class Method Details

.call(context = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/yaso/service.rb', line 39

def call(context = {})
  @entry ||= flow.call(self, steps)
  step = @entry
  instance = new(context)
  step = step.call(context, instance) while step
  instance
end

.flow(name = nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/yaso/service.rb', line 47

def flow(name = nil)
  @flow ||= Logic::FLOWS[:classic] if self == Yaso::Service
  return @flow || Yaso::Service.flow if name.nil?

  @flow = Logic::FLOWS[name] || raise(UnknownFlowError.new(self, name))
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/yaso/service.rb', line 14

def [](key)
  @context[key]
end

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/yaso/service.rb', line 18

def []=(key, value)
  @context[key] = value
end

#failure?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/yaso/service.rb', line 26

def failure?
  !@success
end

#inspectObject



34
35
36
# File 'lib/yaso/service.rb', line 34

def inspect
  "Result:#{self.class} successful: #{@success}, context: #{@context}"
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/yaso/service.rb', line 22

def success?
  @success
end

#to_hObject



30
31
32
# File 'lib/yaso/service.rb', line 30

def to_h
  @context.dup
end