class Ordinary::Normalizer

Public Class Methods

new(options = {}, &process) click to toggle source
# File lib/ordinary/normalizer.rb, line 4
def initialize(options = {}, &process)
  @determine = extract_determiner(options)
  @context   = options[:on]
  @process   = process
end

Public Instance Methods

coming_under?(model) click to toggle source

Determine if a model coming under a target of the normalizer.

@param [ActiveModel::Model] model a model to determine if be a target of

the normalizer

@return whether the model is a target of the normalizer

# File lib/ordinary/normalizer.rb, line 23
def coming_under?(model)
  @determine.nil? or !!@determine.call(model)
end
normalize(value) click to toggle source

Normalize a value by the normalizer.

@param [Object] value a value to normalize @return [Object] a normalized value

# File lib/ordinary/normalizer.rb, line 14
def normalize(value)
  @process.call(value)
end
run_at?(context) click to toggle source

Determine if

@param [Symbol] context a context to determine @return whether

# File lib/ordinary/normalizer.rb, line 31
def run_at?(context)
  @context.nil? or (@context == context)
end

Private Instance Methods

extract_determiner(options) click to toggle source
# File lib/ordinary/normalizer.rb, line 37
def extract_determiner(options)
  if determine = options[:if]
    lambda { |model|  model.instance_eval(&determine) }
  elsif determine = options[:unless]
    lambda { |model| !model.instance_eval(&determine) }
  end
end