Class: Service::Base::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/service/base.rb

Overview

Simple structure to hold the context of the service during its whole lifecycle.

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Context

Returns a new instance of Context.



23
24
25
# File 'lib/service/base.rb', line 23

def initialize(context = {})
  @store = context.symbolize_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



84
85
86
87
# File 'lib/service/base.rb', line 84

def method_missing(method_name, *args, &block)
  return super if args.present?
  store[method_name]
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/service/base.rb', line 27

def [](key)
  store[key.to_sym]
end

#[]=(key, value) ⇒ Object



31
32
33
# File 'lib/service/base.rb', line 31

def []=(key, value)
  store[key.to_sym] = value
end

#fail(context = {}) ⇒ Context

Marks the context as failed without raising an exception.

Examples:

context.fail("failure": "something went wrong")

Parameters:

  • context (Hash, Context) (defaults to: {})

    the context to merge into the current one

Returns:



66
67
68
69
70
# File 'lib/service/base.rb', line 66

def fail(context = {})
  store.merge!(context.symbolize_keys)
  @failure = true
  self
end

#fail!(context = {}) ⇒ Context

Marks the context as failed.

Examples:

context.fail!("failure": "something went wrong")

Parameters:

  • context (Hash, Context) (defaults to: {})

    the context to merge into the current one

Returns:

Raises:



56
57
58
59
# File 'lib/service/base.rb', line 56

def fail!(context = {})
  self.fail(context)
  raise Failure, self
end

#failure?Boolean

Returns true if the context is set as failed

Returns:

  • (Boolean)

    returns true if the context is set as failed

See Also:



47
48
49
# File 'lib/service/base.rb', line 47

def failure?
  @failure || false
end

#inspect_stepsObject



72
73
74
# File 'lib/service/base.rb', line 72

def inspect_steps
  Service::StepsInspector.new(self).inspect
end

#success?Boolean

Returns true if the context is set as successful (default)

Returns:

  • (Boolean)

    returns true if the context is set as successful (default)



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

def success?
  !failure?
end

#to_hObject



35
36
37
# File 'lib/service/base.rb', line 35

def to_h
  store.dup
end