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

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

Parameters:

Returns:



76
77
78
79
80
81
# File 'lib/traitorous/convert.rb', line 76

def array(converter)
  Converter.new(
      StandardProcs.map { |entry| converter.importer.call(entry) },
      StandardProcs.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.

Parameters:

Returns:



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/traitorous/convert.rb', line 106

def array_to_hash(key_converter, value_converter)
  Converter.new(
      StandardProcs.inject({}) do |memo_obj, data|
        value = value_converter.importer.call(data)
        key = key_converter.importer.call(value, data)
        memo_obj[key] = value
        memo_obj
      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.

Parameters:

  • method_name (Symbol, String)

    method to send data

  • with: (Symbol, String) (defaults to: :new)

    method to send klass with data as params

  • export_with: (Symbol, String) (defaults to: :export)

    method to send klass with data as params

Returns:



34
35
36
37
38
39
# File 'lib/traitorous/convert.rb', line 34

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: :itself) ⇒ 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

Parameters:

  • with_method (Symbol, String)

    method to send data on import

  • export_with (Symbol, String) (defaults to: :itself)

    method to send data on export

Returns:



63
64
65
66
67
68
# File 'lib/traitorous/convert.rb', line 63

def call_on_self(with_method, export_with: :itself)
  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

Parameters:

  • default_value (Object) (defaults to: nil)

    value to set in cases that data is falsey

Returns:



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?

Parameters:

Returns:



87
88
89
90
91
92
# File 'lib/traitorous/convert.rb', line 87

def hash(value_converter)
  Converter.new(
      StandardProcs.inject({}) { |h, (k, v)| h[k] = value_converter.importer.call(v) },
      StandardProcs.inject({}) { |h, (k, v)| h[k] = value_converter.exporter.call(v) },
  )
end

.model(model_name, container = :scalar) ⇒ Traitorous::Converter

.model(model_name) instantiates a model_name using model_name.new(data)

Parameters:

  • model_name (Class, #new)

    the name of the model to be instantiated

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/traitorous/convert.rb', line 44

def model(model_name, container = :scalar)
  case container
    when :scalar
      call_on(model_name)
    when :array, []
      self.array(self.call_on(model_name))
    when :hash, {}
      self.hash(self.call_on(model_name))
    else
      raise ArgumentError.new("No container of type: #{container}")
  end
end

.noopTraitorous::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 noop
  Converter.new(
      StandardProcs.noop,
      StandardProcs.noop
  )
end