module Rootage::Normalizer

‘Normalizer` is a utility module that normalizes values into normalization types. If values cannot normalize, this method raises `NormalizerValueError`. Normalization types are followings:

- boolean
- string
- integer
- positive_integer
- float
- date

And array is treated as special type, this type means the value should be included in the array.

Public Class Methods

normalize(type, val) click to toggle source

Normalize the value as the normalization type.

@param type [Symbol]

normalization type

@param val [Object]

source value

@return [Object]

the normalized value
# File lib/rootage/normalizer.rb, line 25
def normalize(type, val)
  if type.nil?
    raise ArgumentError.new("Normalization type should not be nil.")
  end

  if respond_to?(type, true)
    send(type, val)
  else
    raise NormalizerTypeError.new(type)
  end
end
set(name, &block) click to toggle source

Set normalization function.

@param name [Symbol]

normalization type name

@yieldparam val [Object]

source object
# File lib/rootage/normalizer.rb, line 43
def set(name, &block)
  singleton_class.instance_eval do
    define_method(name, &block)
  end
end

Private Class Methods

boolean(val) click to toggle source
# File lib/rootage/normalizer.rb, line 51
def boolean(val)
  BooleanValue.of(val)
rescue => e
  raise NormalizerValueError.new(:boolean, val, e.message)
end
date(val) click to toggle source
# File lib/rootage/normalizer.rb, line 112
def date(val)
  if val.is_a?(Date)
    val
  else
    Date.iso8601(val)
  end
rescue => e
  raise NormalizerValueError.new(:date, val, e.message)
end
float(val) click to toggle source
# File lib/rootage/normalizer.rb, line 96
def float(val)
  val.to_f
rescue => e
  raise NormalizerValueError.new(:float, val, e.message)
end
integer(val) click to toggle source
# File lib/rootage/normalizer.rb, line 81
def integer(val)
  val.to_i
rescue => e
  raise NormalizerValueError.new(:integer, val, e.message)
end
path(val) click to toggle source
# File lib/rootage/normalizer.rb, line 102
def path(val)
  if val.is_a?(Pathname)
    val
  else
    Pathname.new(val)
  end
rescue => e
  raise NormalizerValueError.new(:path, val, e.message)
end
positive_integer(val) click to toggle source
# File lib/rootage/normalizer.rb, line 87
def positive_integer(val)
  n = integer(val)
  if n > 0
    n
  else
    raise NormalizerValueError.new(:positive_integer, val, "It should be a positive integer.")
  end
end
string(val) click to toggle source
# File lib/rootage/normalizer.rb, line 57
def string(val)
  val.to_s
rescue => e
  raise NormalizerValueError.new(:string, val, e.message)
end
symbol(val) click to toggle source
# File lib/rootage/normalizer.rb, line 63
def symbol(val)
  val.to_sym
rescue => e
  raise NormalizerValueError.new(:symbol, val, e.message)
end
symbol_downcase(val) click to toggle source
# File lib/rootage/normalizer.rb, line 69
def symbol_downcase(val)
  val.to_s.downcase.to_sym
rescue => e
  raise NormalizerValueError.new(:symbol_downcase, val, e.message)
end
symbol_uppercase(val) click to toggle source
# File lib/rootage/normalizer.rb, line 75
def symbol_uppercase(val)
  val.to_s.uppercase.to_sym
rescue => e
  raise NormalizerValueError.new(:symbol_uppercase, val, e.message)
end
uri(val) click to toggle source
# File lib/rootage/normalizer.rb, line 122
def uri(val)
  URI.parse(val)
rescue => e
  raise NormalizerValueError.new(:uri, val, e.message)
end