Permalink
Browse files

Merge pull request #3 from indrekj/master

Refresh library style
  • Loading branch information...
2 parents 366533c + 225d054 commit 80fbcf6f05b5a79377cb3dcc28d8d98412f52a53 @mbj committed May 28, 2013
Showing with 637 additions and 307 deletions.
  1. +0 −4 .travis.yml
  2. +11 −14 Gemfile.devtools
  3. +2 −2 config/flay.yml
  4. +1 −1 config/flog.yml
  5. +72 −57 config/reek.yml
  6. +40 −34 lib/inflecto.rb
  7. +38 −36 lib/inflecto/inflections.rb
  8. +20 −0 lib/inflecto/rules_collection.rb
  9. +0 −9 spec/shared/invertible_method_behaviour.rb
  10. +0 −44 spec/shared/mutator_behavior.rb
  11. +18 −1 spec/spec_helper.rb
  12. +19 −0 spec/unit/inflecto/class_methods/camelize_spec.rb
  13. +19 −0 spec/unit/inflecto/class_methods/classify_spec.rb
  14. +35 −0 spec/unit/inflecto/class_methods/constantize_spec.rb
  15. +7 −0 spec/unit/inflecto/class_methods/dasherize_spec.rb
  16. +11 −0 spec/unit/inflecto/class_methods/demodulize_spec.rb
  17. +11 −0 spec/unit/inflecto/class_methods/foreign_key_spec.rb
  18. +20 −0 spec/unit/inflecto/class_methods/humanize_spec.rb
  19. +21 −0 spec/unit/inflecto/class_methods/inflections_spec.rb
  20. +51 −0 spec/unit/inflecto/class_methods/ordinalize_spec.rb
  21. +1 −1 spec/unit/{inflector → inflecto}/class_methods/pluralize_spec.rb
  22. +2 −2 spec/unit/{inflector → inflecto}/class_methods/singularize_spec.rb
  23. +6 −6 spec/unit/{inflector/class_methods/tabelize_spec.rb → inflecto/class_methods/tableize_spec.rb}
  24. +35 −0 spec/unit/inflecto/class_methods/underscore_spec.rb
  25. +11 −0 spec/unit/inflecto/inflections/class_methods/instance_spec.rb
  26. +21 −0 spec/unit/inflecto/inflections/clear_spec.rb
  27. +22 −0 spec/unit/inflecto/inflections/human_spec.rb
  28. +30 −0 spec/unit/inflecto/inflections/irregular_spec.rb
  29. +38 −0 spec/unit/inflecto/inflections/plural_spec.rb
  30. +38 −0 spec/unit/inflecto/inflections/singular_spec.rb
  31. +20 −0 spec/unit/inflecto/inflections/uncountable_spec.rb
  32. +17 −0 spec/unit/inflecto/rules_collection/apply_to_spec.rb
  33. +0 −21 spec/unit/inflector/class_methods/camelize_spec.rb
  34. +0 −15 spec/unit/inflector/class_methods/classify_spec.rb
  35. +0 −13 spec/unit/inflector/class_methods/demodulize_spec.rb
  36. +0 −13 spec/unit/inflector/class_methods/foreign_key_spec.rb
  37. +0 −13 spec/unit/inflector/class_methods/humanize_spec.rb
  38. +0 −21 spec/unit/inflector/class_methods/underscore_spec.rb
View
@@ -29,7 +29,3 @@ matrix:
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
- rvm: jruby-head
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
- allow_failures:
- # mutant fails
- - rvm: 1.9.3
- - rvm: rbx-19mode
View
@@ -1,45 +1,42 @@
# encoding: utf-8
group :development do
- gem 'rake', '~> 10.0.3'
+ gem 'rake', '~> 10.0.4'
gem 'rspec', '~> 2.13.0'
- gem 'yard', '~> 0.8.5'
+ gem 'yard', '~> 0.8.6.1'
end
group :yard do
gem 'kramdown', '~> 1.0.1'
end
group :guard do
- gem 'guard', '~> 1.6.2'
+ gem 'guard', '~> 1.8.0'
gem 'guard-bundler', '~> 1.0.0'
- gem 'guard-rspec', '~> 2.5.1'
+ gem 'guard-rspec', '~> 2.5.4'
# file system change event handling
+ gem 'listen', '~> 1.0.2'
gem 'rb-fchange', '~> 0.0.6', :require => false
gem 'rb-fsevent', '~> 0.9.3', :require => false
gem 'rb-inotify', '~> 0.9.0', :require => false
- gem 'listen', '~> 0.7.3'
-
# notification handling
gem 'libnotify', '~> 0.8.0', :require => false
gem 'rb-notifu', '~> 0.0.4', :require => false
gem 'terminal-notifier-guard', '~> 1.5.3', :require => false
end
group :metrics do
- gem 'backports', '~> 3.1', '>= 3.1.1'
- gem 'coveralls', '~> 0.6.2'
- gem 'flay', '~> 2.1.0'
- gem 'flog', '~> 3.2.2'
+ gem 'backports', '~> 3.3', '>= 3.3.0'
+ gem 'coveralls', '~> 0.6.6'
+ gem 'flay', '~> 2.2.0'
+ gem 'flog', '~> 4.0.0'
gem 'reek', '~> 1.3.1', :git => 'https://github.com/troessner/reek.git'
- gem 'metric_fu-roodi', '~> 2.2.1'
gem 'simplecov', '~> 0.7.1'
- gem 'yardstick', '~> 0.9.4'
+ gem 'yardstick', '~> 0.9.6'
- platforms :ruby_18, :ruby_19 do
- # this indirectly depends on ffi which does not build on ruby-head
+ platforms :ruby_19 do
gem 'yard-spellcheck', '~> 0.1.5'
end
View
@@ -1,3 +1,3 @@
---
-threshold: 14
-total_score: 71
+threshold: 12
+total_score: 69
View
@@ -1,2 +1,2 @@
---
-threshold: 16.7
+threshold: 13
View
@@ -1,95 +1,110 @@
---
-UncommunicativeParameterName:
- accept: []
- exclude: []
+Attribute:
+ enabled: true
+ exclude:
+ - Inflecto::Inflections
+BooleanParameter:
enabled: true
- reject:
- - !ruby/regexp /^.$/
- - !ruby/regexp /[0-9]$/
- - !ruby/regexp /[A-Z]/
-LargeClass:
- max_methods: 14
exclude: []
+ClassVariable:
enabled: true
- max_instance_variables: 8
-UncommunicativeMethodName:
- accept: []
exclude: []
+ControlParameter:
enabled: true
- reject:
- - !ruby/regexp /^[a-z]$/
- - !ruby/regexp /[0-9]$/
- - !ruby/regexp /[A-Z]/
-LongParameterList:
- max_params: 3
exclude: []
+DataClump:
enabled: true
- overrides: {}
-FeatureEnvy:
exclude:
- - Inflecto::Inflections#irregular
+ - Inflecto::Inflections
+ max_copies: 2
+ min_clump_size: 2
+DuplicateMethodCall:
enabled: true
-ClassVariable:
- exclude: []
+ exclude:
+ - Inflecto#self.camelize
+ max_calls: 1
+ allow_calls: []
+FeatureEnvy:
enabled: true
-BooleanParameter:
exclude: []
- enabled: true
IrresponsibleModule:
- exclude: []
enabled: true
-UncommunicativeModuleName:
- accept: []
exclude: []
+LongParameterList:
enabled: true
- reject:
- - !ruby/regexp /^.$/
- - !ruby/regexp /[0-9]$/
+ exclude:
+ - Inflecto::Inflections#add_irregular
+ - Inflecto::Inflections#rule
+ max_params: 2
+ overrides:
+ initialize:
+ max_params: 3
+LongYieldList:
+ enabled: true
+ exclude: []
+ max_params: 2
NestedIterators:
+ enabled: true
+ exclude: []
+ max_allowed_nesting: 1
ignore_iterators: []
+NilCheck:
+ enabled: true
exclude: []
+RepeatedConditional:
enabled: true
- max_allowed_nesting: 2
-LongMethod:
- max_statements: 8
- exclude:
- - Inflecto::Inflections#irregular
+ exclude: []
+ max_ifs: 1
+TooManyInstanceVariables:
enabled: true
-Duplication:
- allow_calls: []
exclude:
- - Inflecto::Inflections#irregular
+ - Inflecto::Inflections
+ max_instance_variables: 3
+TooManyMethods:
enabled: true
- max_calls: 2
-UtilityFunction:
- max_helper_calls: 1
exclude: []
+ max_methods: 10
+TooManyStatements:
+ enabled: true
+ exclude:
+ - each
+ - Inflecto#self.underscore
+ max_statements: 5
+UncommunicativeMethodName:
enabled: true
-Attribute:
exclude: []
- enabled: false
-UncommunicativeVariableName:
+ reject:
+ - !ruby/regexp /^[a-z]$/
+ - !ruby/regexp /[0-9]$/
+ - !ruby/regexp /[A-Z]/
accept: []
+UncommunicativeModuleName:
+ enabled: true
exclude: []
+ reject:
+ - !ruby/regexp /^.$/
+ - !ruby/regexp /[0-9]$/
+ accept: []
+UncommunicativeParameterName:
enabled: true
+ exclude: []
reject:
- !ruby/regexp /^.$/
- !ruby/regexp /[0-9]$/
- !ruby/regexp /[A-Z]/
-SimulatedPolymorphism:
- exclude: []
- enabled: true
- max_ifs: 2
-DataClump:
- exclude:
- - Inflecto::Inflections
+ accept: []
+UncommunicativeVariableName:
enabled: true
- max_copies: 2
- min_clump_size: 2
-ControlCouple:
exclude: []
+ reject:
+ - !ruby/regexp /^.$/
+ - !ruby/regexp /[0-9]$/
+ - !ruby/regexp /[A-Z]/
+ accept: []
+UnusedParameters:
enabled: true
-LongYieldList:
- max_params: 1
exclude: []
+UtilityFunction:
enabled: true
+ exclude: []
+ max_helper_calls: 0
View
@@ -23,7 +23,7 @@ module Inflecto
# @api public
#
def self.camelize(input)
- input.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ input.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:\A|_)(.)/) { $1.upcase }
end
# Convert input to underscored, lowercase string
@@ -41,11 +41,11 @@ def self.camelize(input)
# @api public
#
def self.underscore(input)
- word = input.to_s.dup
+ word = input.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
- word.tr!("-", "_")
+ word.tr!('-', '_')
word.downcase!
word
end
@@ -79,7 +79,7 @@ def self.dasherize(input)
# @api public
#
def self.demodulize(input)
- input.to_s.gsub(/^.*::/, '')
+ input.gsub(/\A.*::/, '')
end
# Creates a foreign key name
@@ -144,10 +144,12 @@ def self.constantize(input)
# @api private
#
def self.ordinalize(number)
- if (11..13).include?(number.to_i % 100)
+ abs_value = number.abs
+
+ if (11..13).include?(abs_value % 100)
"#{number}th"
else
- case number.to_i % 10
+ case abs_value % 10
when 1; "#{number}st"
when 2; "#{number}nd"
when 3; "#{number}rd"
@@ -169,11 +171,8 @@ def self.ordinalize(number)
# @api public
#
def self.inflections
- if block_given?
- yield Inflections.instance
- else
- Inflections.instance
- end
+ instance = Inflections.instance
+ block_given? ? yield(instance) : instance
end
# Convert input word string to plural
@@ -193,14 +192,8 @@ def self.inflections
# @api public
#
def self.pluralize(word)
- result = word.to_s.dup
-
- if result.empty? || inflections.uncountables.include?(result.downcase)
- result
- else
- inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
- result
- end
+ return word if uncountable?(word)
+ inflections.plurals.apply_to(word)
end
# Convert word to singular
@@ -220,14 +213,8 @@ def self.pluralize(word)
# @api public
#
def self.singularize(word)
- result = word.to_s.dup
-
- if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
- result
- else
- inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
- result
- end
+ return word if uncountable?(word)
+ inflections.singulars.apply_to(word)
end
# Humanize string
@@ -247,13 +234,13 @@ def self.singularize(word)
# @api private
#
def self.humanize(input)
- result = input.to_s.dup
-
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
- result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
+ result = inflections.humans.apply_to(input)
+ result.gsub!(/_id\z/, "")
+ result.gsub!(/_/, " ")
+ result.capitalize!
+ result
end
-
# Tabelize input string
#
# @param [String] input
@@ -272,7 +259,7 @@ def self.humanize(input)
# @api private
#
def self.tableize(input)
- pluralize(underscore(input).gsub('/','_'))
+ pluralize(underscore(input).gsub('/', '_'))
end
# Classify input
@@ -296,10 +283,29 @@ def self.tableize(input)
#
def self.classify(table_name)
# strip out any leading schema name
- camelize(singularize(table_name.to_s.sub(/.*\./, '')))
+ camelize(singularize(table_name.sub(/.*\./, '')))
end
+ # Test if word is uncountable
+ #
+ # @example
+ #
+ # Inflecto.uncountable?('rice') #=> true
+ # Inflecto.uncountable?('apple') #=> false
+ #
+ # @param [String] word
+ #
+ # @return [Boolean]
+ # true, if word is uncountable
+ #
+ # @api private
+ #
+ def self.uncountable?(word)
+ word.empty? || inflections.uncountables.include?(word.downcase)
+ end
+ private_class_method :uncountable?
end
+require 'inflecto/rules_collection'
require 'inflecto/inflections'
require 'inflecto/defaults'
Oops, something went wrong.

0 comments on commit 80fbcf6

Please sign in to comment.