attribute-normalizer-1.2.0/ 0000755 0001750 0001751 00000000000 14123637622 014520 5 ustar rmb571 rmb571 attribute-normalizer-1.2.0/attribute_normalizer.gemspec 0000644 0001750 0001751 00000002573 14123637622 022341 0 ustar rmb571 rmb571 ######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: attribute_normalizer 1.2.0 ruby lib Gem::Specification.new do |s| s.name = "attribute_normalizer".freeze s.version = "1.2.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Michael Deering".freeze] s.date = "2014-07-10" s.description = "".freeze s.email = ["mdeering@mdeering.com".freeze] s.files = ["README.textile".freeze, "lib/attribute_normalizer.rb".freeze, "lib/attribute_normalizer/model_inclusions.rb".freeze, "lib/attribute_normalizer/normalizers/blank_normalizer.rb".freeze, "lib/attribute_normalizer/normalizers/boolean_normalizer.rb".freeze, "lib/attribute_normalizer/normalizers/phone_normalizer.rb".freeze, "lib/attribute_normalizer/normalizers/squish_normalizer.rb".freeze, "lib/attribute_normalizer/normalizers/strip_normalizer.rb".freeze, "lib/attribute_normalizer/normalizers/whitespace_normalizer.rb".freeze, "lib/attribute_normalizer/rspec_matcher.rb".freeze, "lib/attribute_normalizer/version.rb".freeze] s.homepage = "https://github.com/mdeering/attribute_normalizer".freeze s.rubygems_version = "3.2.5".freeze s.summary = "".freeze end attribute-normalizer-1.2.0/lib/ 0000755 0001750 0001751 00000000000 14123637622 015266 5 ustar rmb571 rmb571 attribute-normalizer-1.2.0/lib/attribute_normalizer/ 0000755 0001750 0001751 00000000000 14123637622 021533 5 ustar rmb571 rmb571 attribute-normalizer-1.2.0/lib/attribute_normalizer/rspec_matcher.rb 0000644 0001750 0001751 00000002155 14123637622 024702 0 ustar rmb571 rmb571 module AttributeNormalizer module RSpecMatcher def normalize_attribute(attribute) NormalizeAttribute.new(attribute) end class NormalizeAttribute def initialize(attribute) @attribute = attribute @from = '' end def description "normalize #{@attribute} from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}" end def failure_message "#{@attribute} did not normalize as expected! \"#{@subject.send(@attribute)}\" != #{@to.nil? ? 'nil' : "\"#{@to}\""}" end def failure_message_when_negated "expected #{@attribute} to not be normalized from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}" end alias negative_failure_message failure_message_when_negated def from(value) @from = value self end def to(value) @to = value self end def matches?(subject) @subject = subject @subject.send("#{@attribute}=", @from) @subject.send(@attribute) == @to end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/version.rb 0000644 0001750 0001751 00000000306 14123637622 023544 0 ustar rmb571 rmb571 module AttributeNormalizer module Version MAJOR = 1 MINOR = 2 PATCH = 0 BUILD = nil def self.to_s [ MAJOR, MINOR, PATCH, BUILD ].compact.join('.') end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/model_inclusions.rb 0000644 0001750 0001751 00000006177 14123637622 025441 0 ustar rmb571 rmb571 module AttributeNormalizer def self.included(base) base.extend ClassMethods end module ClassMethods def normalize_attributes(*attributes, &block) options = attributes.last.is_a?(::Hash) ? attributes.pop : {} normalizers = [ options[:with] ].flatten.compact normalizers = [ options[:before] ].flatten.compact if block_given? && normalizers.empty? post_normalizers = [ options[:after] ].flatten.compact if block_given? if normalizers.empty? && !block_given? normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers end attributes.each do |attribute| define_method "normalize_#{attribute}" do |value| normalized = value normalizers.each do |normalizer_name| unless normalizer_name.kind_of?(Symbol) normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ] end normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name] raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options) end normalized = block_given? ? yield(normalized) : normalized if block_given? post_normalizers.each do |normalizer_name| unless normalizer_name.kind_of?(Symbol) normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ] end normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name] raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options) end end normalized end self.send :private, "normalize_#{attribute}" if method_defined?(:"#{attribute}=") alias_method "old_#{attribute}=", "#{attribute}=" define_method "#{attribute}=" do |value| normalized_value = self.send(:"normalize_#{attribute}", value) self.send("old_#{attribute}=", normalized_value) end else define_method "#{attribute}=" do |value| super(self.send(:"normalize_#{attribute}", value)) end end end end alias :normalize_attribute :normalize_attributes def normalize_default_attributes AttributeNormalizer.configuration.default_attributes.each do |attribute_name, options| normalize_attribute(attribute_name, options) if self.column_names.include?(attribute_name) end end # def inherited(subclass) # super # if subclass.name.present? && subclass.respond_to?(:table_exists?) && (subclass.table_exists? rescue false) # subclass.normalize_default_attributes # end # end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/ 0000755 0001750 0001751 00000000000 14123637622 024100 5 ustar rmb571 rmb571 attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/squish_normalizer.rb 0000644 0001750 0001751 00000000330 14123637622 030177 0 ustar rmb571 rmb571 module AttributeNormalizer module Normalizers module SquishNormalizer def self.normalize(value, options = {}) value.is_a?(String) ? value.strip.gsub(/\s+/, ' ') : value end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/boolean_normalizer.rb 0000644 0001750 0001751 00000000655 14123637622 030314 0 ustar rmb571 rmb571 # Extracted from ActiveRecord::ConnectionAdapters::Column require 'set' module AttributeNormalizer module Normalizers module BooleanNormalizer TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set def self.normalize(value, options = {}) if value.is_a?(String) && value.blank? nil else TRUE_VALUES.include?(value) end end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/strip_normalizer.rb 0000644 0001750 0001751 00000000306 14123637622 030027 0 ustar rmb571 rmb571 module AttributeNormalizer module Normalizers module StripNormalizer def self.normalize(value, options = {}) value.is_a?(String) ? value.strip : value end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/blank_normalizer.rb 0000644 0001750 0001751 00000000337 14123637622 027761 0 ustar rmb571 rmb571 module AttributeNormalizer module Normalizers module BlankNormalizer def self.normalize(value, options = {}) value.nil? || (value.is_a?(String) && value !~ /\S/) ? nil : value end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/phone_normalizer.rb 0000644 0001750 0001751 00000000426 14123637622 030002 0 ustar rmb571 rmb571 module AttributeNormalizer module Normalizers module PhoneNormalizer def self.normalize(value, options = {}) value = value.is_a?(String) ? value.gsub(/[^0-9]+/, '') : value value.is_a?(String) && value.empty? ? nil : value end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer/normalizers/whitespace_normalizer.rb 0000644 0001750 0001751 00000000371 14123637622 031024 0 ustar rmb571 rmb571 module AttributeNormalizer module Normalizers module WhitespaceNormalizer def self.normalize(value, options = {}) value.is_a?(String) ? value.gsub(/[^\S\n]+/, ' ').gsub(/\s?\n\s?/, "\n").strip : value end end end end attribute-normalizer-1.2.0/lib/attribute_normalizer.rb 0000644 0001750 0001751 00000003675 14123637622 022073 0 ustar rmb571 rmb571 require 'attribute_normalizer/normalizers/blank_normalizer' require 'attribute_normalizer/normalizers/phone_normalizer' require 'attribute_normalizer/normalizers/strip_normalizer' require 'attribute_normalizer/normalizers/squish_normalizer' require 'attribute_normalizer/normalizers/whitespace_normalizer' require 'attribute_normalizer/normalizers/boolean_normalizer' module AttributeNormalizer class MissingNormalizer < ArgumentError; end class << self attr_accessor :configuration end def self.configuration @configuration ||= Configuration.new end def self.configure yield(configuration) end class Configuration attr_accessor :default_normalizers, :normalizers, :default_attributes def default_normalizers=(normalizers) @default_normalizers = normalizers.is_a?(Array) ? normalizers : [ normalizers ] end def initialize @normalizers = { :blank => AttributeNormalizer::Normalizers::BlankNormalizer, :phone => AttributeNormalizer::Normalizers::PhoneNormalizer, :squish => AttributeNormalizer::Normalizers::SquishNormalizer, :strip => AttributeNormalizer::Normalizers::StripNormalizer, :whitespace => AttributeNormalizer::Normalizers::WhitespaceNormalizer, :boolean => AttributeNormalizer::Normalizers::BooleanNormalizer } @default_normalizers = [ :strip, :blank ] end end end require 'attribute_normalizer/model_inclusions' require 'attribute_normalizer/rspec_matcher' def include_attribute_normalizer(class_or_module) return if class_or_module.include?(AttributeNormalizer) class_or_module.class_eval do extend AttributeNormalizer::ClassMethods end end include_attribute_normalizer(ActiveModel::Base) if defined?(ActiveModel::Base) include_attribute_normalizer(ActiveRecord::Base) if defined?(ActiveRecord::Base) include_attribute_normalizer(CassandraObject::Base) if defined?(CassandraObject::Base) attribute-normalizer-1.2.0/README.textile 0000644 0001750 0001751 00000016566 14123637622 017073 0 ustar rmb571 rmb571 h1. Attribute Normalizer !https://secure.travis-ci.org/mdeering/attribute_normalizer.png?branch=master(Build Status)!:http://travis-ci.org/mdeering/attribute_normalizer p. A little normalization goes a long way in helping maintain data integrity. h2. Change History * 1.1.0 ** Allow the use of default normalizers before and after the evaluation of a given block * 1.0.0 ** -DSL Changes- ** Default attributes to normalize ** mongid support * 0.3.0 ** Normalizer Chaining ** Built-in common normalizers ** Ability to change the default attribute normalization. * 0.2.1 ** ActiveModel Support Built-in ** Fix for :with option getting dropped when normalizing several attributes * 0.2.0 ** Removed the normalization on reads. ** Added out of the box support for CassandraObjects ** Included RSpec matcher _normalizer_attribute_ ** Added the ability to define global normalizers ** *Strings no longer get 'strip' called on them before getting passed on to your defined normalization blocks* h2. Supported ORMs * _Active Model_ * Active Record * CassandraObjects p. _I will gladly take pull requests to automatically load Attribute Normalizer into your ORM of choice if requested. To test it out on your ORM just include the AttributeNormalizer module after requiring it._
# your_initializer.rb
require 'attribute_normalizer'
YourORM::Base.send :include, AttributeNormalizer
h2. Install
sudo gem install attribute_normalizer
p. Then just required it. Rails usages is as follows.
h3. Rails 2
# config/environment.rb
config.gem 'attribute_normalizer'
h3. Rails 3
# Gemfile
gem 'attribute_normalizer'
p. It also still works as a traditional Rails plugin.
./script/plugin install git://github.com/mdeering/attribute_normalizer.git
h2. Usage
p. Lets create a quick test/spec for what we want to accomplish as far as normalization using the built in RSpec matcher.
# spec/models/book_spec.rb
describe Book do
it { should normalize_attribute(:author) }
it { should normalize_attribute(:price).from('$3,450.98').to(3450.98) }
it { should normalize_attribute(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
it { should normalize_attribute(:title).from(' pick up chicks with magic tricks ').to('Pick Up Chicks With Magic Tricks') }
it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos ').to('social-life-at-the-edge-of-chaos') }
it { should normalize_attribute(:limited_slug).from(' Social Life at the Edge of Chaos ').to('social-life') }
end
p. The following normalizers are already included with the +0.3 version of the gem.
* _:blank_ Will return _nil_ on empty strings
* _:phone_ Will strip out all non-digit characters and return nil on empty strings
* _:strip_ Will strip leading and trailing whitespace.
* _:squish_ Will strip leading and trailing whitespace and convert any consecutive spaces to one space each
p. And lets predefine some normalizers that we may use in other classes/models or that we don't want to clutter up our class/model's readability with.
# config/initializers/attribute_normalizer.rb
AttributeNormalizer.configure do |config|
config.normalizers[:currency] = lambda do |value, options|
value.is_a?(String) ? value.gsub(/[^0-9\.]+/, '') : value
end
config.normalizers[:truncate] = lambda do |text, options|
if text.is_a?(String)
options.reverse_merge!(:length => 30, :omission => "...")
l = options[:length] - options[:omission].mb_chars.length
chars = text.mb_chars
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
else
text
end
end
# The default normalizers if no :with option or block is given is to apply the :strip and :blank normalizers (in that order).
# You can change this if you would like as follows:
# config.default_normalizers = :strip, :blank
# You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
# the default normalizers for each attribute (e.g. config.default_normalizers)
# config.default_attributes = :name, :title
# Also, You can add a specific attribute to default_attributes using one or more normalizers:
# config.add_default_attribute :name, :with => :truncate
end
The _normalize_attributes_ method is eager loaded into your ORM. _normalize_attribute_ is aliased to _normalized_attributes_ and both can take in a single attribute or an array of attributes.
class Book < ActiveRecord::Base
# By default it will strip leading and trailing whitespace
# and set to nil if blank.
normalize_attributes :author, :publisher
# Using one of our predefined normalizers.
normalize_attribute :price, :with => :currency
# Using more then one of our predefined normalizers including one with options
normalize_attribute :summary, :with => [ :strip, { :truncate => { :length => 12 } } ]
# You can also define your normalization block inline.
normalize_attribute :title do |value|
value.is_a?(String) ? value.titleize.strip : value
end
# Or use a combination of normalizers plus an inline block.
# the normalizers in the :with option will each be evalulated
# in order and the result will be given to the block.
# You could also use option :before in place of :with
normalize_attribute :slug, :with => [ :strip, :blank ] do |value|
value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
end
# Use builtin normalizers before and after the evaluation of your inline
# block
normalize_attribute :limited_slug, :before => [ :strip, :blank ], :after => [ { :truncate => { :length => 11, :omission => '' } } ] do |value|
value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
end
end
p. All the specs will pass now. Here is quick look at the behaviour from a console.
summary = 'Here is my summary that is a little to long'
title = 'pick up chicks with magic tricks'
book = Book.create!(:author => '', :price => '$3,450.89', :summary => summary, :title => title, :slug => title, :limited_slug => title)
book.author # => nil
book.price # => 3450.89
book.summary # => 'Here is m...'
book.title # => 'Pick Up Chicks With Magic Tricks'
book.slug # => 'pick-up-chicks-with-magic-tricks'
book.limited_slug # => 'pick-up-chi'
h2. Test Helpers
p. If you are running RSpec there is a matcher available for use. Usage can been seen above. Include it as follows.
h3. Rails 2
# spec/spec_helper.rb
RSpec.configure do |config|
config.include AttributeNormalizer::RSpecMatcher, :type => :models
end
h3. Rails 3
# spec/spec_helper.rb
RSpec.configure do |config|
config.include AttributeNormalizer::RSpecMatcher, :type => :model
end
p. _I will gladly take a patch to add a macro to Test::Unit if someone submits it._
h2. Credits
Original module code and concept was taken from "Dan Kubb":http://github.com/dkubb during a project we worked on together. I found that I was consistently using this across all my projects so I wanted to plugin-er-size and gem this up for easy reuse.
h2. Copyright
Copyright (c) 2009-2010 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.