Class: Versionomy::Conversion::Base

Inherits:
Object
  • Object
show all
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

Parsing

Instance Method Summary collapse

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.



59
60
61
# File 'lib/versionomy/conversion/base.rb', line 59

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.



72
73
74
75
76
77
78
# File 'lib/versionomy/conversion/base.rb', line 72

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

#inspectObject

Inspect this conversion.



83
84
85
# File 'lib/versionomy/conversion/base.rb', line 83

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)}>"
end

#to_sObject

The default to_s implementation just calls inspect.



90
91
92
# File 'lib/versionomy/conversion/base.rb', line 90

def to_s
  inspect
end