dallas-acts-as-phone 0.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,16 @@
1
+ == ActsAsPhone
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
+ acts_as_phone :phone
9
+
10
+ #=> does validates_format_of :phone, :with => /^\d{10}$/
11
+
12
+ #=> and adds def phone=(value); write_attribute(:phone, value.to_s.gsub(/\D+/, '')); end
13
+
14
+
15
+ ---
16
+ 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 acts_as_phone 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 acts_as_phone plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActsAsPhone'
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 = "acts-as-phone"
29
+ gemspec.summary = (summ = "ActsAsPhone is a Ruby on Rails gem for setting up phone-number-like model attributes.")
30
+ gemspec.email = "code@dallasreedy.com"
31
+ gemspec.homepage = "http://github.com/dallas/acts-as-phone"
32
+ gemspec.description = summ
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
+ 0.0.0
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{acts-as-phone}
5
+ s.version = "0.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-02}
10
+ s.description = %q{ActsAsPhone is a Ruby on Rails gem for setting up phone-number-like model 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
+ "acts-as-phone.gemspec",
21
+ "init.rb",
22
+ "lib/acts_as_phone.rb"
23
+ ]
24
+ s.has_rdoc = true
25
+ s.homepage = %q{http://github.com/dallas/acts-as-phone}
26
+ s.rdoc_options = ["--charset=UTF-8"]
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = %q{1.3.1}
29
+ s.summary = %q{ActsAsPhone is a Ruby on Rails gem for setting up phone-number-like model attributes.}
30
+
31
+ if s.respond_to? :specification_version then
32
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
33
+ s.specification_version = 2
34
+
35
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
36
+ else
37
+ end
38
+ else
39
+ end
40
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'acts_as_phone'
2
+ ActiveRecord::Base.send(:include, Dallas::Acts::Phone)
@@ -0,0 +1,57 @@
1
+ module Dallas #:nodoc: all
2
+ module Acts
3
+ module Phone
4
+ include ActionView::Helpers::NumberHelper
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def acts_as_phone(*attributes)
12
+ @phone_like_attributes = []
13
+
14
+ attributes.each do |attribute|
15
+ # simple length and format validation
16
+ # using validates_each to bypass ActiveRecord's odd propensity to use record.attribute
17
+ # rather than record.read_attribute(attribute) or record[attribute]
18
+ validates_each attribute do |record, attr_name, value|
19
+ record.errors.add attr_name, 'should be a 10-digit phone number' unless record[attr_name] =~ /\d{10}/
20
+ end
21
+
22
+ # setter method to filter non-digit characters
23
+ define_method "#{attribute}=" do |value|
24
+ write_attribute(attribute, value.to_s.gsub(/\D+/, ''))
25
+ end
26
+
27
+ # getter method which automatically applies number_to_phone helper
28
+ define_method attribute do |*from_database|
29
+ return nil if self[attribute].nil?
30
+ !! from_database.first ? self[attribute] : number_to_phone(self[attribute], :area_code => true)
31
+ end
32
+
33
+ # override the default _before_type_cast so that it is
34
+ # formatted using number_to_phone for form fields also
35
+ define_method "#{attribute}_before_type_cast" do |*from_database|
36
+ __send__(attribute, from_database.first)
37
+ end
38
+
39
+ @phone_like_attributes << attribute
40
+ end
41
+
42
+ include Dallas::Acts::Phone::InstanceMethods
43
+ end
44
+
45
+ def phone_like_attributes
46
+ Array(@phone_like_attributes)
47
+ end
48
+ end # ClassMethods
49
+
50
+ module InstanceMethods
51
+ def phone_like_attributes
52
+ self.class.phone_like_attributes.map {|a| [a, read_attribute(a)]}
53
+ end
54
+ end # InstanceMethods
55
+ end # Phone
56
+ end # Acts
57
+ end # Dallas
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dallas-acts-as-phone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dallas Reedy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ActsAsPhone is a Ruby on Rails gem for setting up phone-number-like model 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
+ - acts-as-phone.gemspec
30
+ - init.rb
31
+ - lib/acts_as_phone.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/dallas/acts-as-phone
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: 2
57
+ summary: ActsAsPhone is a Ruby on Rails gem for setting up phone-number-like model attributes.
58
+ test_files: []
59
+