Class: ATypes::NumericWrap

Inherits:
Wrapper show all
Defined in:
lib/a_types/decorators/numeric_wrap.rb

Overview

Adds numeric methods to any wrapped object.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#content

Class Method Details

.try_convert(source) ⇒ Fixnum, ...

Tries to convert the given object to a numerical value, according to the rules of #to_num. Returns nil if no conversion is possible.

Parameters:

  • source (Object)

    to be converted

Returns:

  • (Fixnum, Float, nil)

    the result of the conversion to a numeric value.


28
29
30
# File 'lib/a_types/decorators/numeric_wrap.rb', line 28

def self.try_convert(source)
  new(source).to_num
end

Instance Method Details

#to_numFixnum, ...

Returns the numeric value of this wrapped object. Depending on its class, different strategies will be applied in the course:

  • Strings will be parsed for their content and return nil if it does not match any number

  • true/false will return 1 / -1

  • Numerics will return themselves

  • Other objects will simply return nil

Returns:

  • (Fixnum, Float, nil)

    the number that could be interpreted from the wrapped object.


16
17
18
19
# File 'lib/a_types/decorators/numeric_wrap.rb', line 16

def to_num
  strategy = "convert_#{content.class.name.downcase}"
  respond_to?(strategy, true) ? send(strategy, content) : nil
end