Class: Avm::Result

Inherits:
SimpleDelegator
  • Object
show all
Includes:
EacRubyUtils::Listable
Defined in:
lib/avm/result.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

TYPE_SUCCESS_COLOR =
'green'
TYPE_ERROR_COLOR =
'red'
TYPE_NEUTRAL_COLOR =
'light_black'
TYPE_PENDING_COLOR =
'yellow'
TYPE_OUTDATED_COLOR =
'blue'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type) ⇒ Result

Returns a new instance of Result.



42
43
44
45
46
# File 'lib/avm/result.rb', line 42

def initialize(value, type)
  super(value)
  validate_type(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



40
41
42
# File 'lib/avm/result.rb', line 40

def type
  @type
end

Class Method Details

.success_or_error(success, success_value, error_value = nil) ⇒ Avm::Result

Return Avm::Result.success(success_value) if success is truthy. Return Avm::Result.error(error_value || success_value) if success is falsely.

Returns:



31
32
33
34
35
36
37
# File 'lib/avm/result.rb', line 31

def success_or_error(success, success_value, error_value = nil)
  if success
    self.success(success_value)
  else
    error(error_value.presence || success_value)
  end
end

Instance Method Details

#labelObject



48
49
50
# File 'lib/avm/result.rb', line 48

def label
  label_fg
end

#label_bgObject



56
57
58
# File 'lib/avm/result.rb', line 56

def label_bg
  ColorizedString.new(to_s).light_white.send("on_#{type_color}")
end

#label_fgObject



52
53
54
# File 'lib/avm/result.rb', line 52

def label_fg
  ColorizedString.new(to_s).send(type_color)
end

#type_colorObject



60
61
62
# File 'lib/avm/result.rb', line 60

def type_color
  self.class.const_get("type_#{type}_color".upcase)
end