dallas-has_phone_number 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dallas Reedy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ == HasPhoneNumber
2
+
3
+ A simple plugin which currently just attaches a validation and setter method for each attribute argument passed to it.
4
+
5
+
6
+ == Example
7
+
8
+ has_phone_number :phone
9
+
10
+
11
+ ---
12
+ Copyright (c) 2009 Dallas Reedy, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the has_phone_number plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the has_phone_number plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'HasPhoneNumber'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "has_phone_number"
29
+ gemspec.summary = (summary = "HasPhoneNumber is a Ruby on Rails gem for setting up phone number attributes.")
30
+ gemspec.email = "code@dallasreedy.com"
31
+ gemspec.homepage = "http://github.com/dallas/has_phone_number"
32
+ gemspec.description = summary
33
+ gemspec.authors = ["Dallas Reedy"]
34
+ end
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{has_phone_number}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Dallas Reedy"]
9
+ s.date = %q{2009-06-10}
10
+ s.description = %q{HasPhoneNumber is a Ruby on Rails gem for setting up phone number attributes.}
11
+ s.email = %q{code@dallasreedy.com}
12
+ s.extra_rdoc_files = [
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ "MIT-LICENSE",
17
+ "README.rdoc",
18
+ "Rakefile",
19
+ "VERSION",
20
+ "has_phone_number.gemspec",
21
+ "init.rb",
22
+ "lib/has_phone_number.rb"
23
+ ]
24
+ s.homepage = %q{http://github.com/dallas/has_phone_number}
25
+ s.rdoc_options = ["--charset=UTF-8"]
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.3.4}
28
+ s.summary = %q{HasPhoneNumber is a Ruby on Rails gem for setting up phone number attributes.}
29
+
30
+ if s.respond_to? :specification_version then
31
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
32
+ s.specification_version = 3
33
+
34
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
35
+ else
36
+ end
37
+ else
38
+ end
39
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_phone_number'
@@ -0,0 +1,57 @@
1
+ module Dallas #:nodoc: all
2
+ module HasPhoneNumber
3
+ include ActionView::Helpers::NumberHelper
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_phone_number(*attributes)
11
+ @phone_numbers = []
12
+
13
+ attributes.each do |attribute|
14
+ # simple length and format validation
15
+ # using validates_each to bypass ActiveRecord's odd propensity to use record.attribute
16
+ # rather than record.read_attribute(attribute) or record[attribute]
17
+ validates_each attribute do |record, attr_name, value|
18
+ record.errors.add attr_name, 'should be a 10-digit phone number' unless record[attr_name] =~ /\d{10}/
19
+ end
20
+
21
+ # setter method to filter non-digit characters
22
+ define_method "#{attribute}=" do |value|
23
+ write_attribute(attribute, value.to_s.gsub(/\D+/, ''))
24
+ end
25
+
26
+ # getter method which automatically applies number_to_phone helper
27
+ define_method attribute do |*from_database|
28
+ return nil if self[attribute].nil?
29
+ !! from_database.first ? self[attribute] : number_to_phone(self[attribute], :area_code => true)
30
+ end
31
+
32
+ # override the default _before_type_cast so that it is
33
+ # formatted using number_to_phone for form fields also
34
+ define_method "#{attribute}_before_type_cast" do |*from_database|
35
+ __send__(attribute, from_database.first)
36
+ end
37
+
38
+ @phone_numbers << attribute
39
+ end
40
+
41
+ include Dallas::HasPhoneNumber::InstanceMethods
42
+ end
43
+
44
+ def phone_numbers
45
+ Array(@phone_numbers)
46
+ end
47
+ end # ClassMethods
48
+
49
+ module InstanceMethods
50
+ def phone_numbers
51
+ self.class.phone_numbers.map {|a| [a, read_attribute(a)]}
52
+ end
53
+ end # InstanceMethods
54
+ end # HasPhoneNumber
55
+ end # Dallas
56
+
57
+ ActiveRecord::Base.send(:include, Dallas::HasPhoneNumber)
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dallas-has_phone_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dallas Reedy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: HasPhoneNumber is a Ruby on Rails gem for setting up phone number attributes.
17
+ email: code@dallasreedy.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.rdoc
27
+ - Rakefile
28
+ - VERSION
29
+ - has_phone_number.gemspec
30
+ - init.rb
31
+ - lib/has_phone_number.rb
32
+ has_rdoc: false
33
+ homepage: http://github.com/dallas/has_phone_number
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: HasPhoneNumber is a Ruby on Rails gem for setting up phone number attributes.
58
+ test_files: []
59
+