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.



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

def initialize(data = {})
  @data = data.to_h.transform_keys(&:to_sym)
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



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/service_actor/result.rb', line 143

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



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

def [](name)
  name = name.to_sym

  data[name]
end

#[]=(key, value) ⇒ Object



98
99
100
101
102
# File 'lib/service_actor/result.rb', line 98

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

  data[key] = value
end

#deconstruct_keys(keys) ⇒ Object



115
116
117
118
119
# File 'lib/service_actor/result.rb', line 115

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

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

#delete!(key) ⇒ Object



104
105
106
107
108
# File 'lib/service_actor/result.rb', line 104

def delete!(key)
  key = key.to_sym

  data.delete(key)
end

#errorObject



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

def error
  data[:error] || nil
end

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/service_actor/result.rb', line 56

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)


72
73
74
# File 'lib/service_actor/result.rb', line 72

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

#inspectObject



46
47
48
# File 'lib/service_actor/result.rb', line 46

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(name)
  name = name.to_sym

  to_h.key?(name)
end

#merge!(result) ⇒ Object



80
81
82
83
84
# File 'lib/service_actor/result.rb', line 80

def merge!(result)
  data.merge!(result.transform_keys(&:to_sym))

  self
end

#pretty_print(pp) ⇒ Object



50
51
52
53
54
# File 'lib/service_actor/result.rb', line 50

def pretty_print(pp)
  pp.text "#<#{self.class.name} "
  pp.pp to_h
  pp.text ">"
end

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

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/service_actor/result.rb', line 110

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)


68
69
70
# File 'lib/service_actor/result.rb', line 68

def success?
  !failure?
end

#to_hObject



42
43
44
# File 'lib/service_actor/result.rb', line 42

def to_h
  filter_default_output(data)
end