Class: LamyResult::Lamy

Inherits:
Object
  • Object
show all
Defined in:
lib/lamy_result/lamy.rb

Overview

Creates and checks result instances.

A quick reference for class and instance methods.

Lamy.ok is a class method to be called on the `Lamy` class.

Lamy#ok? is an instance method called on a Lamy instance, which is
returned by Lamy.ok.
  result = Lamy.ok(Excon.get('https://example.com'))
  result.ok? => true

See .define_status_tags for the method defined at runtime.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, value: nil) ⇒ Lamy

Returns a new instance of Lamy.



17
18
19
20
21
22
23
24
# File 'lib/lamy_result/lamy.rb', line 17

def initialize(status:, value: nil)
  @status = status
            .to_s
            .downcase
            .to_sym

  @value = value
end

Instance Attribute Details

#statusObject (readonly)

The raw attributes may be accessed from outside the class, but shouldn't be changed once set.



28
29
30
# File 'lib/lamy_result/lamy.rb', line 28

def status
  @status
end

#valueObject (readonly)

The raw attributes may be accessed from outside the class, but shouldn't be changed once set.



28
29
30
# File 'lib/lamy_result/lamy.rb', line 28

def value
  @value
end

Class Method Details

.assert_new_instance_method!(method_symbol) ⇒ Object

Checks if the method is an instance method, otherwise raises a StandardError.



160
161
162
163
164
165
166
# File 'lib/lamy_result/lamy.rb', line 160

def self.assert_new_instance_method!(method_symbol)
  unless self.instance_methods.include?(method_symbol)
    raise StandardError.new "##{method_symbol} is not an instance method."
  end

  true
end

.define_aliases_for(method_symbol, instance_alias, *method_aliases) ⇒ Object

Defines a class or instance alias for the supplied method with each of the supplied array items.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lamy_result/lamy.rb', line 170

def self.define_aliases_for(
  method_symbol,
  instance_alias,
  *method_aliases
)
  method_aliases.each do |method_alias|
    if instance_alias
      alias_method(method_alias, method_symbol)
    else
      self.class.alias_method(method_alias, method_symbol)
    end
  end

  true
end

.define_class_aliases_for(method_symbol, *method_aliases) ⇒ Object



186
187
188
# File 'lib/lamy_result/lamy.rb', line 186

def self.define_class_aliases_for(method_symbol, *method_aliases)
  define_aliases_for(method_symbol, false, *method_aliases)
end

.define_conditional_then_method(status, *aliases) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lamy_result/lamy.rb', line 140

def self.define_conditional_then_method(status, *aliases)
  method_symbol = format_conditional_method(status)
  status_conditional_aliases = aliases.map do |s|
    format_conditional_method(s)
  end

  define_method(method_symbol) do |&method_block|
    assert_status_then(status_to_check: status, &method_block)
  end

  assert_new_instance_method! method_symbol

  define_instance_aliases_for(
    method_symbol,
    *status_conditional_aliases
  )
end

.define_instance_aliases_for(method_symbol, *method_aliases) ⇒ Object

Defines an instance alias for the supplied method with each of the supplied array items.



192
193
194
195
196
197
198
# File 'lib/lamy_result/lamy.rb', line 192

def self.define_instance_aliases_for(method_symbol, *method_aliases)
  define_aliases_for(
    method_symbol,
    true,
    *method_aliases
  )
end

.define_status_check_method(status, *aliases) ⇒ Object

Defines an instance method to check the status label. E.g. #ok?



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lamy_result/lamy.rb', line 123

def self.define_status_check_method(status, *aliases)
  method_symbol = format_status_check_method(status)
  status_check_aliases = aliases.map do |s|
    format_status_check_method(s)
  end

  define_method(method_symbol) do
    # Get the status that we need to check. It should have
    # been captured.
    status_is?(status.to_sym)
  end

  assert_new_instance_method! method_symbol

  self.define_instance_aliases_for(method_symbol, *status_check_aliases)
end

.define_status_method(status, *aliases) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/lamy_result/lamy.rb', line 111

def self.define_status_method(status, *aliases)
  self
  .class
  .send(:define_method, status) do |value|
    Lamy.new(status: status, value: value)
  end

  # Define aliases for the new status if we got any.
  define_class_aliases_for(status, *aliases)
end

.define_status_tags(*status_tags) ⇒ Object Also known as: define_status_tag, add_status_tags, add_status_tag

Adds one or more new status tags to be used by the



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lamy_result/lamy.rb', line 84

def self.define_status_tags(*status_tags)
  # Use Array() over [] to create an array if status_tags is not an array,
  # but not create an array within an array.
  Array(
    status_tags
  ).each do |status|
    new_status, *new_status_aliases = Array(status)

    # Defines the short-hand class method like .ok(input)
    self.define_status_method(new_status, *new_status_aliases)

    # Define status check instance methods like #ok?
    self.define_status_check_method(new_status, *new_status_aliases)

    # Define conditional instance methods like #ok_then
    self.define_conditional_then_method(new_status, *new_status_aliases)
  end
end

.format_conditional_method(status) ⇒ Object



107
108
109
# File 'lib/lamy_result/lamy.rb', line 107

def self.format_conditional_method(status)
  "#{status.to_s}_then".to_sym
end

.format_status_check_method(status) ⇒ Object



103
104
105
# File 'lib/lamy_result/lamy.rb', line 103

def self.format_status_check_method(status)
  "#{status.to_s}?".to_sym
end

Instance Method Details

#assert_status_then(status_to_check:) ⇒ Object

Calls #status_is? on the `status_to_check` and if true, yields the value. Otherwise, returns the instance. This is a low-level method meant to be used by #ok_then and #success_then methods.



40
41
42
43
44
45
46
47
48
# File 'lib/lamy_result/lamy.rb', line 40

def assert_status_then(status_to_check:)
  if status_is?(status_to_check)
    return yield @value if block_given?

    return @value
  end

  self
end

#export_statusObject

Returns the status attribute as a symbol or a TrueClass/FalseClass. This is a low-level method meant to be used by #to_a and #to_h.



52
53
54
55
56
57
58
59
60
# File 'lib/lamy_result/lamy.rb', line 52

def export_status
  # If the status is a boolean, return the status as a boolean.
  # Otherwise, return it as a symbol, which it should already be.
  if true? || false?
    @status.to_s.to_sym == true.to_s.to_sym
  else
    @status.to_s.to_sym
  end
end

#status_is?(value) ⇒ Boolean Also known as: ==, eql?

Compares the current instance's status to the input value. This is a lov-level method meant to be used by the #ok? and #success? methods.

Returns:

  • (Boolean)


33
34
35
# File 'lib/lamy_result/lamy.rb', line 33

def status_is?(value)
  @status == value.to_sym
end

#to_aObject

Returns an array of the instance. The status instance attribute is first and the value attribute is second. The status will be returned as a symbol unless it is true or false. At which point, it will be converted.



66
67
68
69
70
71
# File 'lib/lamy_result/lamy.rb', line 66

def to_a
  [
    export_status,
    @value
  ]
end

#to_hObject

Returns a hash of the instance with @status under the `status` key and @value under the `value` key. The status will be returned as a symbol unless it is true or false. At which point, it will be converted.



76
77
78
79
80
81
# File 'lib/lamy_result/lamy.rb', line 76

def to_h
  {
    status: export_status,
    value: @value
  }
end