Skip to content

Commit

Permalink
Merge pull request #134 from joost/rubocop
Browse files Browse the repository at this point in the history
Added first RuboCop stuff.
  • Loading branch information
Joost Hietbrink committed Mar 12, 2016
2 parents dbefb3a + e6438dd commit 38b25a0
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 221 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
63 changes: 63 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-03-12 20:44:43 +0100 using RuboCop version 0.38.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/phony_rails.rb'

# Offense count: 2
Lint/UnderscorePrefixedVariableName:
Exclude:
- 'lib/phony_rails.rb'

# Offense count: 4
Metrics/AbcSize:
Max: 41

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 21

# Offense count: 162
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Max: 166

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 25

# Offense count: 2
Metrics/PerceivedComplexity:
Max: 23

# Offense count: 1
# Cop supports --auto-correct.
Performance/StartWith:
Exclude:
- 'lib/phony_rails.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'lib/phony_rails.rb'

# Offense count: 7
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/phony_rails.rb'
- 'lib/phony_rails/string_extensions.rb'
- 'lib/validators/phony_validator.rb'
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: ruby
rvm:
- "1.9.3"
- "2.1.3"
- "2.2.1"
- rbx
script: bundle exec rspec spec
- '1.9.3'
- '2.1.3'
- '2.2.1'
script:
- bundle exec rspec spec
- bundle exec rubocop
11 changes: 6 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ gemspec # Specify your gem's dependencies in phony_number.gemspec
gem 'sqlite3'

gem 'rake'
gem 'rspec'#, '~> 2.14.0'
gem 'guard'#, '~> 2.10.1'
gem 'guard-bundler'#, '~> 2.0.0'
gem 'guard-rspec'#, '~> 4.2.0'
gem 'coveralls', require: false
gem 'rspec' # , '~> 2.14.0'
gem 'guard' # , '~> 2.10.1'
gem 'guard-bundler' # , '~> 2.0.0'
gem 'guard-rspec' # , '~> 4.2.0'
gem 'coveralls', require: false
gem 'rubocop'
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
ast (2.2.0)
bson (4.0.4)
builder (3.2.2)
coderay (1.1.1)
Expand Down Expand Up @@ -73,11 +74,15 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
origin (2.2.0)
parser (2.3.0.6)
ast (~> 2.2)
phony (2.15.17)
powerpack (0.1.1)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rainbow (2.1.0)
rake (11.1.0)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
Expand All @@ -95,6 +100,13 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
rubocop (0.38.0)
parser (>= 2.3.0.6, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.7.5)
shellany (0.0.1)
simplecov (0.11.2)
docile (~> 1.1.0)
Expand All @@ -110,6 +122,7 @@ GEM
tins (1.6.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unicode-display_width (1.0.2)

PLATFORMS
ruby
Expand All @@ -124,4 +137,5 @@ DEPENDENCIES
phony_rails!
rake
rspec
rubocop
sqlite3
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ end
guard 'rspec', cmd: 'bundle exec rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch('spec/spec_helper.rb') { 'spec' }
end
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)
task :default => :spec
task default: :spec
14 changes: 7 additions & 7 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ module Extension
# It also adds the country_code (number), eg. 31 for NL numbers.
def set_phony_normalized_numbers(attributes, options = {})
options = options.clone
if self.respond_to?(:country_code)
if respond_to?(:country_code)
set_country_as = options[:enforce_record_country] ? :country_code : :default_country_code
options[set_country_as] ||= self.country_code
options[set_country_as] ||= country_code
end
attributes.each do |attribute|
attribute_name = options[:as] || attribute
fail(RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
new_value= PhonyRails.normalize_number(send(attribute), options)
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
new_value = PhonyRails.normalize_number(send(attribute), options)
send("#{attribute_name}=", new_value) if new_value
end
end
Expand Down Expand Up @@ -128,11 +128,11 @@ def phony_normalized_method(*attributes)
main_options = attributes.last.is_a?(Hash) ? attributes.pop : {}
main_options.assert_valid_keys :country_code, :default_country_code
attributes.each do |attribute|
fail(StandardError, "Instance method normalized_#{attribute} already exists on #{self.name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
raise(StandardError, "Instance method normalized_#{attribute} already exists on #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")
define_method :"normalized_#{attribute}" do |*args|
options = args.first || {}
fail(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless self.respond_to?(attribute)
options[:country_code] ||= country_code if self.respond_to?(:country_code)
raise(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless respond_to?(attribute)
options[:country_code] ||= country_code if respond_to?(:country_code)
PhonyRails.normalize_number(send(attribute), main_options.merge(options))
end
end
Expand Down
81 changes: 38 additions & 43 deletions lib/phony_rails/string_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
class String

# Usage:
# "+31 (0)30 1234 123".phony_normalized # => '+31301234123'
# "(0)30 1234 123".phony_normalized # => '301234123'
# "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
def phony_normalized(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" if not options.is_a?(Hash)
options = options.dup
PhonyRails.normalize_number(self, options)
end

# Add a method to the String class so we can easily format phone numbers.
# This enables:
# "31612341234".phony_formatted # => '06 12341234'
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
# To first normalize a String use:
# "010-12341234".phony_formatted(:normalize => :NL)
# To return nil when a number is not correct (checked using Phony.plausible?) use
# "010-12341234".phony_formatted(strict: true)
# When an error occurs during conversion it will return the original String.
# To raise an error use:
# "somestring".phone_formatted(raise: true)
def phony_formatted(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" if not options.is_a?(Hash)
options = options.dup
normalize_country_code = options.delete(:normalize)
s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s, :add_plus => false) : self.gsub(/\D/, ''))
return if s.blank?
return if options[:strict] && !Phony.plausible?(s)
Phony.format(s, options.reverse_merge(:format => :national))
rescue
if options[:raise]
raise
else
s
end
end
class String
# Usage:
# "+31 (0)30 1234 123".phony_normalized # => '+31301234123'
# "(0)30 1234 123".phony_normalized # => '301234123'
# "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
def phony_normalized(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
options = options.dup
PhonyRails.normalize_number(self, options)
end

# The bang method
def phony_formatted!(options = {})
raise ArgumentError, "The :strict options is only supported in the phony_formatted (non bang) method." if options[:strict]
replace(self.phony_formatted(options))
end
# Add a method to the String class so we can easily format phone numbers.
# This enables:
# "31612341234".phony_formatted # => '06 12341234'
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
# To first normalize a String use:
# "010-12341234".phony_formatted(:normalize => :NL)
# To return nil when a number is not correct (checked using Phony.plausible?) use
# "010-12341234".phony_formatted(strict: true)
# When an error occurs during conversion it will return the original String.
# To raise an error use:
# "somestring".phone_formatted(raise: true)
def phony_formatted(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
options = options.dup
normalize_country_code = options.delete(:normalize)
s = (normalize_country_code ? PhonyRails.normalize_number(self, default_country_code: normalize_country_code.to_s, add_plus: false) : gsub(/\D/, ''))
return if s.blank?
return if options[:strict] && !Phony.plausible?(s)
Phony.format(s, options.reverse_merge(format: :national))
rescue
raise if options[:raise]
s
end

# The bang method
def phony_formatted!(options = {})
raise ArgumentError, 'The :strict options is only supported in the phony_formatted (non bang) method.' if options[:strict]
replace(phony_formatted(options))
end
end
2 changes: 1 addition & 1 deletion lib/phony_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module PhonyRails
VERSION = '0.13.0'
VERSION = '0.13.0'.freeze
end
10 changes: 3 additions & 7 deletions lib/validators/phony_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
# validate :phone_number, :phony_plausible => true
require 'active_model'
class PhonyPlausibleValidator < ActiveModel::EachValidator

# Validates a String using Phony.plausible? method.
def validate_each(record, attribute, value)
return if value.blank?

@record = record

value = PhonyRails.normalize_number value, default_country_code: normalized_country_code if normalized_country_code
@record.errors.add(attribute, error_message) if not Phony.plausible?(value, cc: country_number)
value = PhonyRails.normalize_number(value.dup, default_country_code: normalized_country_code) if normalized_country_code
@record.errors.add(attribute, error_message) unless Phony.plausible?(value, cc: country_number)
end

private
Expand Down Expand Up @@ -43,22 +42,19 @@ def record_country_code
def normalized_country_code
options[:normalized_country_code]
end

end

module ActiveModel
module Validations
module HelperMethods

def validates_plausible_phone(*attr_names)
# merged attributes are modified somewhere, so we are cloning them for each validator
merged_attributes = _merge_attributes(attr_names)

validates_with PresenceValidator, merged_attributes.clone if merged_attributes[:presence]
validates_with FormatValidator, merged_attributes.clone if (merged_attributes[:with] or merged_attributes[:without])
validates_with FormatValidator, merged_attributes.clone if merged_attributes[:with] || merged_attributes[:without]
validates_with PhonyPlausibleValidator, merged_attributes.clone
end

end
end
end
28 changes: 14 additions & 14 deletions phony_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
require File.expand_path('../lib/phony_rails/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Joost Hietbrink"]
gem.email = ["[email protected]"]
gem.description = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
gem.summary = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
gem.homepage = "https://github.com/joost/phony_rails"
gem.authors = ['Joost Hietbrink']
gem.email = ['[email protected]']
gem.description = 'This Gem adds useful methods to your Rails app to validate, display and save phone numbers.'
gem.summary = 'This Gem adds useful methods to your Rails app to validate, display and save phone numbers.'
gem.homepage = 'https://github.com/joost/phony_rails'
gem.license = 'MIT'

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "phony_rails"
gem.require_paths = ["lib"]
gem.name = 'phony_rails'
gem.require_paths = ['lib']
gem.version = PhonyRails::VERSION

gem.post_install_message = "PhonyRails v0.10.0 changes the way numbers are stored!"
gem.post_install_message = 'PhonyRails v0.10.0 changes the way numbers are stored!'
gem.post_install_message = "It now adds a '+' to the normalized number when it starts with a country number!"

gem.add_dependency "phony", '~> 2.12'
gem.add_dependency "activesupport", ">= 3.0"
gem.add_development_dependency "activerecord", ">= 3.0"
gem.add_development_dependency "mongoid", ">= 3.0"
gem.add_dependency 'phony', '~> 2.12'
gem.add_dependency 'activesupport', '>= 3.0'
gem.add_development_dependency 'activerecord', '>= 3.0'
gem.add_development_dependency 'mongoid', '>= 3.0'
end
Loading

0 comments on commit 38b25a0

Please sign in to comment.