Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: b79f3cf255
...
compare: 8324a17c28
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 04, 2011
@sarwagya added a separate Phone class to supply Phony options in runtime 610ea49
@sarwagya used space as default phone number separator
added dependencies required for rspec testing.
added readme and license files.
8324a17
View
20 LICENCE.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Sarwagya Pandey
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
9 README.rdoc
@@ -0,0 +1,9 @@
+= mongoid_phone_number
+
+Description goes here.
+
+== Copyright
+
+Copyright (c) 2011 Sarwagya Pandey. See LICENSE.txt for
+further details.
+
View
19 lib/mongoid/conversions.rb
@@ -13,8 +13,25 @@ def set(value)
end
def get(value)
- Phony.format(value.to_s, :format => :international, :spaces => :-)
+ Phone.new(value)
end
end
+
+ protected
+
+ # This class is used to set different Phony options in runtime because Phony doesn't allow creating a new object
+ class Phone
+ attr_accessor :spaces, :format, :value
+
+ def initialize (value = nil, format="international", spaces = " ")
+ @format = format
+ @spaces = spaces
+ @value = value
+ end
+
+ def to_s()
+ Phony.format(@value.to_s, :format => @format.to_sym, :spaces => @spaces.to_sym)
+ end
+ end
end
end
View
45 mongoid_phone_number.gemspec
@@ -1,6 +1,5 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "version"
Gem::Specification.new do |s|
s.name = "mongoid_phone_number"
@@ -8,7 +7,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.authors = ["TODO: Sarwagya"]
s.email = ["TODO: sarwagya@sprout-technology.com"]
- s.homepage = ""
+ s.homepage = %q{http://github.com/sarwagya/mongoid_phone_number}
s.summary = %q{Adds a custom field type 'PhoneNumber' in mongoid}
s.description = %q{Every non-numeric character is removed from the field and only the numeric value is stored.}
@@ -16,8 +15,46 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency("phony",'~>1.4.1')
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<mongoid>, ["= 2.0.0.rc.7"])
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
+ s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_development_dependency(%q<rcov>, [">= 0"])
+ s.add_development_dependency(%q<database_cleaner>, [">= 0"])
+ s.add_development_dependency(%q<bson_ext>, [">= 0"])
+ s.add_development_dependency(%q<fabrication>, ["~> 0.9.0"])
+ s.add_dependency(%q<phony>,["~>1.4.1"])
+ else
+ s.add_dependency(%q<mongoid>, ["= 2.0.0.rc.7"])
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
+ s.add_dependency(%q<fabrication>, ["~> 0.9.0"])
+ s.add_dependency(%q<phony>,["~>1.4.1"])
+ end
+ else
+ s.add_dependency(%q<mongoid>, ["= 2.0.0.rc.7"])
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
+ s.add_dependency(%q<bson_ext>, [">= 0"])
+ s.add_dependency(%q<fabrication>, ["~> 0.9.0"])
+ s.add_dependency(%q<phony>,["~>1.4.1"])
+ end
+
end
View
36 spec/mongoid/phone_number_spec.rb
@@ -0,0 +1,36 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+class Person
+ include Mongoid::Document
+
+ field :name
+ field :home_phone, :type => PhoneNumber
+end
+
+Fabricator(:person) do
+ name "Ben"
+ home_phone "1 809 123 1234"
+end
+
+describe Mongoid::Extensions::PhoneNumber::Conversions do
+ let(:person) { Fabricate :person }
+
+ context "a single instance" do
+
+ it "should store a normalized phone number value" do
+ person.attributes[:home_phone].should == '18091231234'
+ end
+
+ it "should return a default formatted phone number" do
+ phone = person.home_phone
+ phone.to_s.should == "+1 809 123 1234"
+ end
+
+ it "should allow formatting the phone number with other phony options" do
+ phone = person.home_phone
+ phone.format = "national"
+ phone.spaces = "."
+ phone.to_s.should == "0809.123.1234"
+ end
+ end
+end
View
32 spec/spec_helper.rb
@@ -0,0 +1,32 @@
+require 'bundler'
+Bundler.setup
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rspec'
+require 'mongoid'
+require 'database_cleaner'
+require 'mongoid_phone_number'
+require 'fabrication'
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+ config.mock_with :rspec
+
+ # http://mongoid.org/docs/integration/
+ config.before(:suite) do
+ DatabaseCleaner.strategy = :truncation
+ DatabaseCleaner.orm = 'mongoid'
+ end
+
+ config.before(:each) do
+ DatabaseCleaner.clean
+ end
+end
+
+Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db('mongoid_phone_number_test')
+end

No commit comments for this range

Something went wrong with that request. Please try again.