Class: Versionomy::Conversion::Base
- Inherits:
-
Object
- Object
- Versionomy::Conversion::Base
- Defined in:
- lib/versionomy/conversion/base.rb
Overview
The base conversion class.
This base class defines the API for a conversion. All conversions must define the method convert_value
documented here. Conversions need not actually extend this base class, as long as they duck-type this method. However, this base class does provide a few convenience methods such as a sane implementation of inspect.
Direct Known Subclasses
Instance Method Summary collapse
-
#convert_value(value_, format_, convert_params_ = nil) ⇒ Object
Convert the given value to the given format and return the converted value.
-
#initialize(&block_) ⇒ Base
constructor
Create a conversion using a simple DSL.
-
#inspect ⇒ Object
Inspect this conversion.
-
#to_s ⇒ Object
The default to_s implementation just calls inspect.
Constructor Details
#initialize(&block_) ⇒ Base
Create a conversion using a simple DSL. You can pass a block to the initializer that takes the same parameters as convert_value, and the conversion will use that block to perform the conversion.
58 59 60 |
# File 'lib/versionomy/conversion/base.rb', line 58 def initialize(&block_) @_converter = block_ end |
Instance Method Details
#convert_value(value_, format_, convert_params_ = nil) ⇒ Object
Convert the given value to the given format and return the converted value.
The convert_params may be interpreted however the particular conversion wishes.
Raises Versionomy::Errors::ConversionError if the conversion failed.
71 72 73 74 75 76 77 |
# File 'lib/versionomy/conversion/base.rb', line 71 def convert_value(value_, format_, convert_params_=nil) if @_converter @_converter.call(value_, format_, convert_params_) else raise Errors::ConversionError, "Conversion not implemented" end end |
#inspect ⇒ Object
Inspect this conversion.
82 83 84 |
# File 'lib/versionomy/conversion/base.rb', line 82 def inspect "#<#{self.class}:0x#{object_id.to_s(16)}>" end |
#to_s ⇒ Object
The default to_s implementation just calls inspect.
89 90 91 |
# File 'lib/versionomy/conversion/base.rb', line 89 def to_s inspect end |