Module: PowerConverter
- Defined in:
- lib/power_converter.rb,
lib/power_converter/version.rb
Overview
:nodoc:
Defined Under Namespace
Classes: ConversionError, ConverterNotFoundError
Constant Summary collapse
- CONVERSION_METHOD_PREFIX =
When building a dynamic conversion method this is its prefix.
"convert_to_".freeze
- CONVERSION_METHOD_REGEXP =
Useful for when you want to know if a method name is a valid conversion method name.
/\A#{CONVERSION_METHOD_PREFIX}(.+)\Z/
- VERSION =
:nodoc:
"0.1.2".freeze
Class Method Summary collapse
-
.convert(value, options = {}) { ... } ⇒ Object
Convert the given ‘value` via the named `:to` converter.
-
.converter_for(named_conversion) ⇒ #call
Given the ‘named_conversion` find and retrieve the defined converter.
-
.define_alias(aliased_named_conversion, options = {}) ⇒ Object
You may define an alias of a defined (or to be defined) power converter.
-
.define_conversion_for(named_conversion, &converter) {|value| ... } ⇒ Object
Responsible for defining a conversion method and a “shovel-ready” conversion module; because maybe you want a mixin for convenience reasons.
-
.defined_converter_names ⇒ Array
A convenience method for seeing the names of all converters that have been defined.
-
.module_for(named_conversion) ⇒ Module
The means for mixing in a private conversion method; Perhaps as policy you don’t want to expose the public conversion method but instead prefer to leverage private methods.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Handle attempts to call module level conversions directly off of the PowerConverter module.
256 257 258 259 260 261 262 263 |
# File 'lib/power_converter.rb', line 256 def method_missing(method_name, *args, **kwargs, &block) named_converter = extract_named_converter_from(method_name) if named_converter convert(*args, kwargs.merge(to: named_converter), &block) else super end end |
Class Method Details
.convert(value, options = {}) { ... } ⇒ Object
Convert the given ‘value` via the named `:to` converter. As a short-circuit if the given `value` publicly responds to a `to_<named_converter>` it will use that.
155 156 157 158 159 160 161 162 163 |
# File 'lib/power_converter.rb', line 155 def convert(value, = {}) named_converter = .fetch(:to) scope = .fetch(:scope, nil) return value.public_send("to_#{named_converter}") if value.respond_to?("to_#{named_converter}", false) returning_value = converter_for(named_converter).call(value, *scope) return returning_value unless returning_value.nil? return yield if block_given? raise ConversionError.new(value, ) end |
.converter_for(named_conversion) ⇒ #call
There are no protections for infinite alias loops.
Given the ‘named_conversion` find and retrieve the defined converter.
218 219 220 221 222 223 |
# File 'lib/power_converter.rb', line 218 def converter_for(named_conversion) key = named_conversion.to_s return defined_conversions[key] if defined_conversions.key?(key) return converter_for(aliased_conversions[key]) if aliased_conversions.key?(key) raise ConverterNotFoundError.new(named_conversion, defined_converter_names) end |
.define_alias(aliased_named_conversion, options = {}) ⇒ Object
You may define an alias of a defined (or to be defined) power converter.
117 118 119 120 |
# File 'lib/power_converter.rb', line 117 def define_alias(aliased_named_conversion, = {}) @aliased_conversions ||= {} @aliased_conversions[aliased_named_conversion.to_s] = .fetch(:is_alias_of) end |
.define_conversion_for(named_conversion, &converter) {|value| ... } ⇒ Object
Make sure that the converter requires at least one parameter.
If your defined converter returns ‘nil`, it is assumed that the conversion failed and a [PowerConverter::ConversionError] exception will be thrown.
The conversion module/method that is created may not adhear to the exact idiom (a method defined in CamelCase)
Responsible for defining a conversion method and a “shovel-ready” conversion module; because maybe you want a mixin for convenience reasons.
97 98 99 100 |
# File 'lib/power_converter.rb', line 97 def define_conversion_for(named_conversion, &converter) @defined_conversions ||= {} @defined_conversions[named_conversion.to_s] = converter end |
.defined_converter_names ⇒ Array
A convenience method for seeing the names of all converters that have been defined.
232 233 234 |
# File 'lib/power_converter.rb', line 232 def defined_converter_names defined_conversions.keys + aliased_conversions.keys end |
.module_for(named_conversion) ⇒ Module
Allow for the inclusion of multiple power converter named types.
This does not allow for the robustness of the .convert method. I’m wondering about its ongoing value.
The means for mixing in a private conversion method; Perhaps as policy you don’t want to expose the public conversion method but instead prefer to leverage private methods.
193 194 195 196 197 198 199 |
# File 'lib/power_converter.rb', line 193 def module_for(named_conversion) Module.new do extend Forwardable def_delegator PowerConverter, "#{CONVERSION_METHOD_PREFIX}#{named_conversion}" private "#{CONVERSION_METHOD_PREFIX}#{named_conversion}" end end |