Class: ATypes::NumericWrap
- Defined in:
- lib/a_types/decorators/numeric_wrap.rb
Overview
Adds numeric methods to any wrapped object.
Class Method Summary collapse
-
.try_convert(source) ⇒ Fixnum, ...
Tries to convert the given object to a numerical value, according to the rules of #to_num.
Instance Method Summary collapse
-
#to_num ⇒ Fixnum, ...
Returns the numeric value of this wrapped object.
Methods inherited from Wrapper
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.
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_num ⇒ Fixnum, ...
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
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 |