Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion class2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activesupport", ">= 3.2", "< 7"
spec.add_dependency "strings-inflection", "~> 0.1"

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
41 changes: 37 additions & 4 deletions lib/class2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,44 @@
require "date"
require "time" # for parse()
require "json"
require "active_support/core_ext/module"
require "active_support/inflector"
require "strings/inflection"

require "class2/version"

# String extensions to replace ActiveSupport inflector functionality
class String
def classify
# Use strings-inflection for singularization, then apply classification logic
# Handle the known issue with "address" -> "addres"
singular = case self
when /(.*)address(es)?$/
# Handle any word ending with "address" or "addresses"
prefix = $1
prefix + 'address'
else
Strings::Inflection.singularize(self)
end

singular.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

def pluralize
Strings::Inflection.pluralize(self)
end

def singularize
# Handle known issue with strings-inflection for "address"
case self
when /(.*)address(es)?$/
# Handle any word ending with "address" or "addresses"
prefix = $1
prefix + 'address'
else
Strings::Inflection.singularize(self)
end
end
end

no_export = ENV["CLASS2_NO_EXPORT"]

unless no_export == "1"
Expand Down Expand Up @@ -251,8 +284,8 @@ def assign_attributes(attributes)

name = key.to_s.classify

# parent is deprecated in ActiveSupport 6 and its warning uses Strong#squish! which they don't include!
parent = self.class.respond_to?(:module_parent) ? self.class.module_parent : self.class.parent
# Get the parent module (equivalent to ActiveSupport's module_parent/parent)
parent = self.class.name.split('::')[0..-2].inject(Object) { |mod, name| mod.const_get(name) }
next unless parent.const_defined?(name)

klass = parent.const_get(name)
Expand Down