Class: Operatic::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/operatic/result.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



22
23
24
25
# File 'lib/operatic/result.rb', line 22

def initialize
  @data = {}
  @success = true
end

Class Method Details

.generate(*attrs) ⇒ Object

Generate a subclass of Operatic::Result with named attrs accessors. This wouldn't normally be called directly, see ClassMethods#result_attr for example usage.

Parameters:

  • attrs (Array<Symbol>)

    a list of convenience data accessors.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/operatic/result.rb', line 8

def self.generate(*attrs)
  Class.new(self) do
    attrs.each do |name|
      define_method name do
        @data[name]
      end

      define_method "#{name}=" do |value|
        @data[name] = value
      end
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Read data that's attached to the result.



28
29
30
# File 'lib/operatic/result.rb', line 28

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

#[]=(key, value) ⇒ Object

Set data on the result.



33
34
35
# File 'lib/operatic/result.rb', line 33

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

#deconstructArray(Boolean, Hash<Symbol, anything>)

Returns an array of success and data.

Examples:

result = Result.new.success!(message: 'Hello world')

case result
in [true, { message: }]
  # Result is a success, do something with the `message` variable.
in [false, _]
  # Result is a failure, do something else.
end

Returns:

  • (Array(Boolean, Hash<Symbol, anything>))


50
51
52
# File 'lib/operatic/result.rb', line 50

def deconstruct
  [@success, @data]
end

#failure!(**data) ⇒ Object

Mark the result as a failure, optionally attach data via kwargs, and freeze the object so it cannot be modified further.

Note: Calling #success! or #failure! more than once will raise a FrozenError.



59
60
61
62
63
# File 'lib/operatic/result.rb', line 59

def failure!(**data)
  @success = false
  set_data(data)
  freeze
end

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  !@success
end

#freezeObject



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

def freeze
  @data.freeze
  super
end

#success!(**data) ⇒ Object

Mark the result as a success, optionally attach data via kwargs, and freeze the object so it cannot be modified further.

Calling this is not strictly necessary as a Result defaults to being a success, but it's a convenient means of attaching data and of indicating intent in the consuming code.

Note: Calling #success! or #failure! more than once will raise a FrozenError.



83
84
85
86
87
# File 'lib/operatic/result.rb', line 83

def success!(**data)
  @success = true
  set_data(data)
  freeze
end

#success?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/operatic/result.rb', line 89

def success?
  @success
end

#to_hHash<Symbol, anything>

Returns the full (frozen) hash of data attached to the result via #success!, #failure!, or convenience accessors added with generate.

Returns:

  • (Hash<Symbol, anything>)


97
98
99
# File 'lib/operatic/result.rb', line 97

def to_h
  @data
end