module Ordinary::ClassMethods
Public Instance Methods
normalizers()
click to toggle source
@attribute [r] normalizers @return [Hash<Symbol, Array<Ordinary::Normalizer>>] normalizers for each
attribute
# File lib/ordinary.rb, line 97 def normalizers @normalizers ||= {} end
normalizes(*attr_names, &block)
click to toggle source
Define normalization for attributes.
@example define normalization with a builder for the normalizer
normalizes :name, lambda { lstrip } normalizes :name, lambda { lstrip | rstrip }
@example define normalization with a block
normalizes :name do |value| value.squeeze(' ') end
@example define normalization with a builder for the normalizer and a block
normalizes :name, -> { lstrip | block | rstrip } do |value| value.squeeze(' ') end
@param [Array<Symbol>] attr_names attirubte names to normalize @yield [value] normalize the attribute @yieldparam [Object] value value of the attribute to normalize
# File lib/ordinary.rb, line 123 def normalizes(*attr_names, &block) attr_names = attr_names.dup buil = nil options = {} case attr_names.last when Proc build = attr_names.pop when Hash options = attr_names.pop.dup build = options.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(options, &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