Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 643ad0fdf3a106b389d6506c8c3f082d9eb61a5b @btelles committed Jan 29, 2010
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,53 @@
+ValidatesAndFormatsPhones
+=========================
+
+Allows you to easily format and validate formats in any format you desire
+(sensible defaults provided).
+
+Just add 'validates_and_formats_phones' to your ActiveRecord model.
+
+The default format is a 10-digit american phone number on the column 'phone'.
+But you can change all that...just read on.
+
+
+Example
+=======
+
+
+Adding the validates_and_formats_phones method by itself gives you
+validation and formatting for the field :phone like so:
+
+
+ class Person
+ validates_and_formats_phones
+ end
+
+ > person = Person.new(:phone => '123 456 7890')
+ > person.save
+ > person.phone
+ > => '(123) 456-7890'
+ >
+ > person.phone = '123'
+ > person.save
+ > => false
+
+You can pass your own fields like so:
+
+ class Person
+ validates_and_formats_phones :fax, :mobile
+ end
+
+
+Or your own formats like so:
+
+ class Person
+ validates_and_formats_phones(8 => '####-####')
+ end
+
+or both, like so:
+
+ class Person
+ validates_and_formats_phones(:fax, mobile, 8 => '####-####')
+ end
+
+Copyright (c) 2010 [Bernie Telles], released under the MIT license
@@ -0,0 +1,44 @@
+require 'rubygems'
+require 'rake'
+require 'rake/gempackagetask'
+require 'spec/rake/spectask'
+
+desc 'Default: run specs.'
+task :default => :spec
+
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+PKG_FILES = FileList[
+ '[a-zA-Z]*',
+ 'generators/**/*',
+ 'lib/**/*',
+ 'rails/**/*',
+ 'tasks/**/*',
+ 'test/**/*'
+]
+
+spec = Gem::Specification.new do |s|
+ s.name = "validates_and_formats_phones"
+ s.version = "0.0.1"
+
+ s.author = "Bernie Telles"
+ s.email = "btelles@gmail.com"
+ s.homepage = "http://frontended.com/"
+ s.platform = Gem::Platform::RUBY
+ s.summary = "Allows you to easily format and validate formats in any format you desire (sensible defaults provided)."
+ s.files = PKG_FILES.to_a
+ s.require_path = "lib"
+
+ s.has_rdoc = false
+ s.extra_rdoc_files = ["README"]
+end
+
+desc 'Turn this plugin into a gem.'
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
@@ -0,0 +1,6 @@
+
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
+
+ Autotest.add_discovery do
+ "rspec"
+ end
@@ -0,0 +1,2 @@
+require 'validates_and_formats_phones/phone_formatter'
+require 'validates_and_formats_phones/validates_and_formats_phones'
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,3 @@
+require 'active_record'
+require 'validates_and_formats_phones/phone_formatter'
+require 'validates_and_formats_phones/validates_and_formats_phones'
@@ -0,0 +1,17 @@
+
+String.class_eval do
+ #Formats a phone according to the provided formats.
+ # _formats_ is a hash whose keys are the number of digits
+ # and values are the formats of the phone number.
+ # For example {10 => "(###) ###-####"}
+ def to_phone(formats = { 10 => "(###) ###-####"})
+ digits = scan(/\d/)
+ final_string = formats[scan(/\d/).size].each_char.inject('') do |result, character|
+ character == '#' ? result << digits.shift : result << character
+ result
+ end
+
+ final_string
+ end
+
+end
@@ -0,0 +1,48 @@
+ module ValidatesAndFormatsPhones
+ DEFAULT_FORMAT = { 10 => "(###) ###-####"}
+ def self.included(base)
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+
+
+ def validates_and_formats_phones(*args)
+ options = args.extract_options!
+ options = DEFAULT_FORMAT if options.empty?
+ args.empty? ? field_names = [:phone] : field_names = args
+
+ send :include, InstanceMethods
+
+ validates_each *field_names do |record, attr, value|
+ size_options = options.keys
+ unless value.blank? || size_options.include?(value.scan(/\d/).size)
+ if size_options.size > 1
+ last = size_options.pop
+ record.errors.add attr, "must have #{size_options.join(', ')} or #{last} digits."
+ else
+ record.errors.add attr, "must have #{size_options[0]} digits."
+ end
+
+ end
+ after_validation "format_phone_fields(#{field_names.inspect}, #{options.inspect})"
+ end
+ end
+ end
+
+ module InstanceMethods
+
+ def format_phone_fields(fields = [:phone], *args)
+ options = args.empty? ? DEFAULT_FORMAT : args.extract_options!
+ fields.each do |field_name|
+ format_phone_field(field_name, options) unless send(field_name).blank?
+ end
+ end
+ def format_phone_field(field_name, *args)
+ options = args.extract_options! || DEFAULT_FORMAT
+ self.send("#{field_name}=", self.send(field_name).to_s.to_phone(options))
+ end
+ end
+ end
+
+ ActiveRecord::Base.send :include, ValidatesAndFormatsPhones
Binary file not shown.
@@ -0,0 +1 @@
+require 'validates_and_formats_phones'
@@ -0,0 +1,3 @@
+sqlite3:
+ adapter: sqlite3
+ database: vendor/plugins/validates_and_formats_phones/spec/validates_phones_plugin.sqlite3
@@ -0,0 +1,3 @@
+sqlite3:
+ :adapter: sqlite3
+ :dbfile: vendor/plugins/validates_and_formats_phones/spec/validates_phones_plugin.sqlite3.db
@@ -0,0 +1,9 @@
+ActiveRecord::Schema.define(:version => 0) do
+ create_table :phones, :force => true do |t|
+ t.string :phone
+ end
+ create_table :options_phones, :force => true do |t|
+ t.string :other_phone
+ end
+
+end
Oops, something went wrong.

0 comments on commit 643ad0f

Please sign in to comment.