Module: Traitorous::Convert
- Defined in:
- lib/traitorous/convert.rb
Overview
Convert provides a selection of helpers that will build converters for
common situations.
Class Method Summary collapse
-
.array(converter) ⇒ Traitorous::Converter
Creates an importer that runs the converter importer or exporter for each element of an array, return the resulting array.
-
.array_to_hash(key_converter, value_converter) ⇒ Traitorous::Converter
TODO better expecting key, value pairs as the output of the converter? Expects an incoming array, uses the param value_converter for each element, then uses the key_converter on the resulting value and stores the key, value pair in the resulting hash.
-
.call_on(klass, with: :new, export_with: :export) ⇒ Traitorous::Converter
.call_on creates a StandardProc.call_on proc for the importer, and a StandardProc.call_on_self proc for the exporter.
-
.call_on_self(with_method, export_with: :export) ⇒ Traitorous::Converter
.call_on_self creates a StandardProc.call_on_self proc for the importer with the with_method param, and a StandardProc.call_on_self with the export_with param.
-
.default(default_value = nil) ⇒ Traitorous::Converter
.default(default_value = nil) creates a StandardProc.default importer and a StandardProcs.noop exporter TODO add :export_with for consistency.
-
.hash(value_converter) ⇒ Traitorous::Converter
TODO better to extract the inject proc into it’s own StandardProc?.
-
.skip ⇒ Traitorous::Converter
.skip provides the simplest converter.
Class Method Details
.array(converter) ⇒ Traitorous::Converter
Creates an importer that runs the converter importer or exporter for
each element of an array, return the resulting array
61 62 63 64 65 66 |
# File 'lib/traitorous/convert.rb', line 61 def array(converter) Converter.new( proc { |data| Array(data).map { |entry| converter.importer.call(entry) } }, proc { |data| Array(data).map { |entry| converter.exporter.call(entry) } } ) end |
.array_to_hash(key_converter, value_converter) ⇒ Traitorous::Converter
TODO better expecting key, value pairs as the output of the converter? Expects an incoming array, uses the param value_converter for each
element, then uses the key_converter on the resulting value and stores
the key, value pair in the resulting hash.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/traitorous/convert.rb', line 91 def array_to_hash(key_converter, value_converter) Converter.new( proc do |data_arr| data_arr.inject({}) do |h, d| v = value_converter.importer.call(d) k = key_converter.importer.call(v) h[k] = v h end end, proc { |data_hsh| data_hsh.values.map { |value| value_converter.exporter.call(value) } } ) end |
.call_on(klass, with: :new, export_with: :export) ⇒ Traitorous::Converter
.call_on creates a StandardProc.call_on proc for the importer, and a
StandardProc.call_on_self proc for the exporter.
35 36 37 38 39 40 |
# File 'lib/traitorous/convert.rb', line 35 def call_on(klass, with: :new, export_with: :export) Converter.new( StandardProcs.call_on(klass, with_method: with), StandardProcs.call_on_self(export_with) ) end |
.call_on_self(with_method, export_with: :export) ⇒ Traitorous::Converter
.call_on_self creates a StandardProc.call_on_self proc for the importer
with the with_method param, and a StandardProc.call_on_self with the
export_with param
48 49 50 51 52 53 |
# File 'lib/traitorous/convert.rb', line 48 def call_on_self(with_method, export_with: :export) Converter.new( StandardProcs.call_on_self(with_method), StandardProcs.call_on_self(export_with) ) end |
.default(default_value = nil) ⇒ Traitorous::Converter
.default(default_value = nil) creates a StandardProc.default importer
and a StandardProcs.noop exporter
TODO add :export_with for consistency
21 22 23 24 25 26 |
# File 'lib/traitorous/convert.rb', line 21 def default(default_value = nil) Converter.new( StandardProcs.default(default_value), StandardProcs.noop ) end |
.hash(value_converter) ⇒ Traitorous::Converter
TODO better to extract the inject proc into it’s own StandardProc?
72 73 74 75 76 77 |
# File 'lib/traitorous/convert.rb', line 72 def hash(value_converter) Converter.new( proc { |data| data.inject({}) { |h, (k, v)| h[k] = value_converter.importer.call(v); h } }, proc { |data| data.inject({}) { |h, (k, v)| h[k] = value_converter.exporter.call(v); h } }, ) end |
.skip ⇒ Traitorous::Converter
.skip provides the simplest converter. It does nothing to the value
except pass it through unchanged on both import and export
9 10 11 12 13 14 |
# File 'lib/traitorous/convert.rb', line 9 def skip Converter.new( StandardProcs.noop, StandardProcs.noop ) end |