Module: Ordinary
- Defined in:
- lib/ordinary.rb,
lib/ordinary/unit.rb,
lib/ordinary/module.rb,
lib/ordinary/builder.rb,
lib/ordinary/version.rb,
lib/ordinary/normalizer.rb
Defined Under Namespace
Modules: ClassMethods, Composable, Module Classes: Builder, Normalizer, Unit, Units
Constant Summary collapse
- VERSION =
'0.0.1'
Class Method Summary collapse
- .included(klass) ⇒ Object
-
.register(*modules) ⇒ Object
Register modules to the context of building.
-
.unregister(modules) ⇒ Object
Unregister modules from the context of building.
Instance Method Summary collapse
-
#normalize(context = nil) ⇒ Object
Normalize all attributes and return the normalized object.
-
#normalize!(context = nil) ⇒ Object
Normalize all attributes distructively.
-
#normalize_attribute(attr_name, context = nil) ⇒ Object
Normalize an attribute.
-
#normalized?(context = nil) ⇒ Boolean
Determine if self is normalized.
Class Method Details
.included(klass) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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) ⇒ Object
Register modules to the context of building.
64 65 66 |
# File 'lib/ordinary.rb', line 64 def self.register(*modules) Builder::Context.register(*modules) end |
.unregister(modules) ⇒ Object
Unregister modules from the context of building.
72 73 74 |
# File 'lib/ordinary.rb', line 72 def self.unregister(modules) Builder::Context.unregister(*modules) end |
Instance Method Details
#normalize(context = nil) ⇒ Object
Normalize all attributes and return the normalized object.
31 32 33 |
# File 'lib/ordinary.rb', line 31 def normalize(context = nil) clone.normalize!(context) end |
#normalize!(context = nil) ⇒ Object
Normalize all attributes distructively.
39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
Normalize an attribute.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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) ⇒ Boolean
Determine if self is normalized.
55 56 57 58 |
# 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 |