Module: Vedeu::Coercions

Includes:
Common
Included in:
Colour, Style, Translator
Defined in:
lib/vedeu/support/coercions.rb

Overview

Provides means to convert attributes into the correct model.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#defined_value?, #to_sentence

Class Method Details

.included(receiver) ⇒ void

This method returns an undefined value.

Whenever this module Vedeu::Coercions is included in another class or module, make its methods into class methods, so they may be called directly.



54
55
56
# File 'lib/vedeu/support/coercions.rb', line 54

def self.included(receiver)
  receiver.extend(self)
end

Instance Method Details

#coerce(value) ⇒ Object

Produces new objects of the correct class from the value, ignores objects that have already been coerced.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vedeu/support/coercions.rb', line 16

def coerce(value)
  if value.nil?
    new

  elsif value.is_a?(self)
    value

  else
    new(value)

  end
end

#coercer(values) ⇒ Array

Produces new objects of the correct class from attributes hashes, ignores objects that have already been coerced.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vedeu/support/coercions.rb', line 34

def coercer(values)
  return [] unless defined_value?(values)

  [values].flatten.map do |value|
    if value.is_a?(self)
      value

    else
      new(value)

    end
  end
end