module Ordinary

Constants

VERSION

Public Class Methods

included(klass) click to toggle source
# File lib/ordinary.rb, line 76
def self.included(klass)
  klass.extend(ClassMethods)

  if defined?(ActiveModel::Validations) and klass.include?(ActiveModel::Validations)
    method = klass.instance_method(:run_validations!)

    klass.__send__(:define_method, :run_validations!) do
      context = validation_context
      normalized?(context) ? method.bind(self).call : normalize(context).valid?(context)
    end
  end

  if defined?(ActiveRecord::Base) and klass.include?(ActiveRecord::Base)
  end
end
register(*modules) click to toggle source

Register modules to the context of building.

@scope class @param [Array<Ordinary::Module>] modules modules to register

# File lib/ordinary.rb, line 64
def self.register(*modules)
  Builder::Context.register(*modules)
end
unregister(modules) click to toggle source

Unregister modules from the context of building.

@scope class @param [Array<Ordinary::Module>] modules modules to unregister

# File lib/ordinary.rb, line 72
def self.unregister(modules)
  Builder::Context.unregister(*modules)
end

Public Instance Methods

normalize(context = nil) click to toggle source

Normalize all attributes and return the normalized object.

@param [Symbol] context normalization context. defaults to nil @return [Object] the normalized object

# File lib/ordinary.rb, line 31
def normalize(context = nil)
  clone.normalize!(context)
end
normalize!(context = nil) click to toggle source

Normalize all attributes distructively.

@param [Symbol] context normalization context. defaults to nil @return [Object] self

# File lib/ordinary.rb, line 39
def normalize!(context = nil)
  unless normalized?(context)
    self.class.normalizers.keys.each do |attr_name|
      __send__(:"#{attr_name}=", normalize_attribute(attr_name, context))
    end

    @normalization_context = context
  end

  self
end
normalize_attribute(attr_name, context = nil) click to toggle source

Normalize an attribute.

@param [Symbol] attr_name an attribute to normalize @param [Symbol] context normalization context. defaults to nil @return [Object] the normalized attribute

# File lib/ordinary.rb, line 13
def normalize_attribute(attr_name, context = nil)
  value = __send__(attr_name)

  unless value.nil?
    self.class.normalizers[attr_name.to_sym].each do |normalizer|
      next unless normalizer.coming_under?(self)
      next unless normalizer.run_at?(context)
      value = normalizer.normalize(value)
    end
  end

  value
end
normalized?(context = nil) click to toggle source

Determine if self is normalized.

@param [Symbol] context normalization context. defaults to nil @return whether self is normalized

# File lib/ordinary.rb, line 55
def normalized?(context = nil)
  return false unless instance_variable_defined?(:@normalization_context)
  @normalization_context.nil? or (@normalization_context == context)
end