phony_rails 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phony_number.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Joost Hietbrink
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # PhonyNumber
2
+
3
+ This Gem adds useful methods to your Rails app to validate, display and save phone numbers.
4
+ It uses the super awesome Phony gem (https://github.com/floere/phony).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'phony_number'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install phony_number
19
+
20
+ ## Usage
21
+
22
+ In your model add:
23
+
24
+ class SomeModel < ActiveRecord::Base
25
+ phony_normalize :phone_number, :default_country_code => 'US' # Normalizes the attribute itself
26
+ phony_normalized_method :fax_number # Creates method normalized_fax_number that returns the normalized version of fax_number
27
+ end
28
+
29
+ Use the Phony.plausible method to validate an attribute:
30
+
31
+ validate :phone_number, :phony_number => true
32
+
33
+ In your views use:
34
+
35
+ <%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,99 @@
1
+ require 'phony'
2
+ require "phony_rails/string_extensions"
3
+ require "phony_rails/version"
4
+
5
+ module PhonyRails
6
+
7
+ # Quick fix to get country_phone_number (phone number) for all relevant countries.
8
+ # TODO: Replace with some gem or something.
9
+ COUNTRY_NUMBER = {
10
+ 'NL' => '31',
11
+ 'BE' => '32',
12
+ 'DE' => '49',
13
+ 'GB' => '44',
14
+ 'FR' => '33',
15
+ 'ES' => '34',
16
+ 'IT' => '39',
17
+ 'US' => '1',
18
+ 'AU' => '61',
19
+ 'LU' => '352'
20
+ }
21
+
22
+ # This method requires a country_code attribute (eg. NL) and phone_number to be set.
23
+ # Options:
24
+ # :country_code => The country code we should use.
25
+ # :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
26
+ def self.normalize_number(number, options = {})
27
+ return if number.blank?
28
+ number = Phony.normalize(number) # TODO: Catch errors
29
+ if country_number = COUNTRY_NUMBER[options[:country_code] || options[:default_country_code]]
30
+ # Add country_number if missing
31
+ number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
32
+ end
33
+ number = Phony.normalize(number)
34
+ rescue
35
+ number # If all goes wrong .. we still return the original input.
36
+ end
37
+
38
+ # This module is added to AR.
39
+ module ActiveRecordExtension
40
+
41
+ def self.extended(base)
42
+ base.send :include, InstanceMethods
43
+ base.extend ClassMethods
44
+ end
45
+
46
+ module InstanceMethods
47
+
48
+ private
49
+
50
+ # This methods sets the attribute to the normalized version.
51
+ # It also adds the country_code (number), eg. 31 for NL numbers.
52
+ def set_phony_normalized_numbers(attributes, options = {})
53
+ options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
54
+ attributes.each do |attribute|
55
+ write_attribute(attribute, PhonyRails.normalize_number(read_attribute(attribute), options))
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ module ClassMethods
62
+
63
+ # Use this method on the class level like:
64
+ # phony_normalize :phone_number, :fax_number, :default_country_code => 'NL'
65
+ #
66
+ # It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure
67
+ # you've geocoded before calling this method!
68
+ def phony_normalize(*attributes)
69
+ options = attributes.last.is_a?(Hash) ? attributes.pop : {}
70
+ options.assert_valid_keys :country_code, :default_country_code
71
+ attributes.each do |attribute|
72
+ # Add before validation that saves a normalized version of the phone number
73
+ self.before_validation do
74
+ set_phony_normalized_numbers(attributes, options)
75
+ end
76
+ end
77
+ end
78
+
79
+ # Usage:
80
+ # phony_normalized_method :fax_number, :default_country_code => 'US'
81
+ # Creates a normalized_fax_number method.
82
+ def phony_normalized_method(*attributes)
83
+ options = attributes.last.is_a?(Hash) ? attributes.pop : {}
84
+ options.assert_valid_keys :country_code, :default_country_code
85
+ attributes.each do |attribute|
86
+ raise ArgumentError, "Attribute #{attribute} was not found on #{self.name} (PhonyRails)" unless self.attribute_method?(attribute)
87
+ define_method :"normalized_#{attribute}" do
88
+ options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
89
+ PhonyRails.normalize_number(self[attribute], options)
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+ ActiveRecord::Base.extend PhonyRails::ActiveRecordExtension
@@ -0,0 +1,12 @@
1
+ class String
2
+
3
+ # Add a method to the String class so we can easily format phone numbers.
4
+ # This enables:
5
+ # "31612341234".phony_formatted # => '06 12341234'
6
+ # "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
7
+ def phony_formatted(options = {})
8
+ options[:format] ||= :national
9
+ Phony.formatted(self, options)
10
+ end
11
+
12
+ end
@@ -0,0 +1,3 @@
1
+ module PhonyRails
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,12 @@
1
+ # Uses the Phony.plausible method to validate an attribute.
2
+ # Usage:
3
+ # validate :phone_number, :phony => true
4
+ class PhonyValidator < ActiveModel::EachValidator
5
+
6
+ # Validates a String using Phony.plausible? method.
7
+ def validate_each(record, attribute, value)
8
+ return if value.blank?
9
+ record.errors[attribute] << (options[:message] || "is an invalid number") if not Phony.plausible?(value)
10
+ end
11
+
12
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/phony_rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Joost Hietbrink"]
6
+ gem.email = ["joost@joopp.com"]
7
+ gem.description = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
8
+ gem.summary = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "phony_rails"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = PhonyRails::VERSION
17
+
18
+ gem.add_dependency "phony", "~> 1.6.7"
19
+ gem.add_dependency "activerecord", "~> 3.0"
20
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phony_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joost Hietbrink
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: phony
16
+ requirement: &70205545340880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70205545340880
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70205545340380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70205545340380
36
+ description: This Gem adds useful methods to your Rails app to validate, display and
37
+ save phone numbers.
38
+ email:
39
+ - joost@joopp.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - lib/phony_rails.rb
49
+ - lib/phony_rails/string_extensions.rb
50
+ - lib/phony_rails/version.rb
51
+ - lib/validators/phony_validator.rb
52
+ - phony_rails.gemspec
53
+ homepage: ''
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.15
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: This Gem adds useful methods to your Rails app to validate, display and save
77
+ phone numbers.
78
+ test_files: []