Class: ServiceActor::Result

Inherits:
BasicObject
Defined in:
lib/service_actor/result.rb

Overview

Represents the context of an actor, holding the data from both its inputs and outputs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Result

Returns a new instance of Result.



31
32
33
# File 'lib/service_actor/result.rb', line 31

def initialize(data = {})
  @data = data.to_h
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

rubocop:disable Metrics/AbcSize



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/service_actor/result.rb', line 118

def method_missing(method_name, *args) # rubocop:disable Metrics/AbcSize
  if method_name.end_with?("?") &&
      data.key?(key = method_name.to_s.chomp("?").to_sym)
    value = data[key]
    value.respond_to?(:empty?) ? !value.empty? : !!value
  elsif method_name.end_with?("=")
    data[method_name.to_s.chomp("=").to_sym] = args.first
  elsif data.key?(method_name)
    data[method_name]
  else
    warn_on_undefined_method_invocation(method_name)
  end
end

Class Method Details

.to_result(data) ⇒ Object



7
8
9
10
11
# File 'lib/service_actor/result.rb', line 7

def to_result(data)
  return data if data.is_a?(self)

  new(data.to_h)
end

Instance Method Details

#[](name) ⇒ Object



79
80
81
# File 'lib/service_actor/result.rb', line 79

def [](name)
  data[name]
end

#[]=(key, value) ⇒ Object



83
84
85
# File 'lib/service_actor/result.rb', line 83

def []=(key, value)
  data[key] = value
end

#deconstruct_keys(keys) ⇒ Object



96
97
98
99
100
# File 'lib/service_actor/result.rb', line 96

def deconstruct_keys(keys)
  deconstructed_keys = to_h.merge(success: success?, failure: failure?)

  keys ? deconstructed_keys.slice(*keys) : deconstructed_keys
end

#delete!(key) ⇒ Object



87
88
89
# File 'lib/service_actor/result.rb', line 87

def delete!(key)
  data.delete(key)
end

#errorObject



65
66
67
# File 'lib/service_actor/result.rb', line 65

def error
  data[:error] || nil
end

#fail!(failure_class = nil, result = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/service_actor/result.rb', line 45

def fail!(failure_class = nil, result = {})
  if failure_class.nil? || failure_class.is_a?(::Hash)
    result = failure_class.to_h
    failure_class = ::ServiceActor::Failure
  end

  data.merge!(result)
  data[:failure] = true

  ::Kernel.raise failure_class, self
end

#failure?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/service_actor/result.rb', line 61

def failure?
  data[:failure] || false
end

#inspectObject Also known as: pretty_print



39
40
41
# File 'lib/service_actor/result.rb', line 39

def inspect
  "<#{self.class.name} #{to_h}>"
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/service_actor/result.rb', line 75

def key?(name)
  to_h.key?(name)
end

#merge!(result) ⇒ Object



69
70
71
72
73
# File 'lib/service_actor/result.rb', line 69

def merge!(result)
  data.merge!(result)

  self
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/service_actor/result.rb', line 91

def respond_to?(method_name, include_private = false)
  self.class.instance_methods.include?(method_name) ||
    respond_to_missing?(method_name, include_private)
end

#success?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/service_actor/result.rb', line 57

def success?
  !failure?
end

#to_hObject



35
36
37
# File 'lib/service_actor/result.rb', line 35

def to_h
  data
end