module Ordinary
Constants
- VERSION
Public Class Methods
# 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 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 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 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 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 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
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