Module: Ordinary::ClassMethods
- Defined in:
- lib/ordinary.rb
Instance Attribute Summary collapse
-
#normalizers ⇒ Hash<Symbol, Array<Ordinary::Normalizer>>
readonly
Normalizers for each attribute.
Instance Method Summary collapse
-
#normalizes(*attr_names) {|value| ... } ⇒ Object
Define normalization for attributes.
Instance Attribute Details
#normalizers ⇒ Hash<Symbol, Array<Ordinary::Normalizer>> (readonly)
Returns normalizers for each attribute.
97 98 99 |
# File 'lib/ordinary.rb', line 97 def normalizers @normalizers ||= {} end |
Instance Method Details
#normalizes(*attr_names) {|value| ... } ⇒ Object
Define normalization for attributes.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ordinary.rb', line 123 def normalizes(*attr_names, &block) attr_names = attr_names.dup buil = nil = {} case attr_names.last when Proc build = attr_names.pop when Hash = attr_names.pop.dup build = .delete(:with) end unless build or block raise ArgumentError, 'process for building a normalizer' \ '(with the last argument or :with option) or ' \ 'an unit of a normalizer ' \ '(with block) are not given' end build ||= lambda { block } unit = Builder.new(&block).build(&build) normalizer = Normalizer.new(, &unit) attr_names.each do |attr_name| raise ArgumentError, "##{attr_name} is not defined" unless method_defined?(attr_name) raise ArgumentError, "##{attr_name}= is not defined" unless method_defined?(:"#{attr_name}=") (normalizers[attr_name.to_sym] ||= []) << normalizer define_method :"normalized_#{attr_name}" do |context = nil| normalize_attribute(attr_name, context) end end end |