Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make inflections public #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions lib/dry/inflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Inflector
require "dry/inflector/version"
require "dry/inflector/inflections"

attr_reader :inflections

# Instantiate the inflector
#
# @param blk [Proc] an optional block to specify custom inflection rules
Expand Down Expand Up @@ -310,6 +312,17 @@ def to_s
end
alias_method :inspect, :to_s

# Freeze the inflector instance
#
# Forbid later additions to the inflector rules
#
# @since 1.1.0
#
def freeze
inflections.freeze
super
end

private

# @since 0.1.0
Expand All @@ -320,8 +333,6 @@ def to_s
# @api private
DEFAULT_SEPARATOR = " "

attr_reader :inflections

# @since 0.1.3
# @api private
def internal_camelize(input, upper)
Expand Down
23 changes: 23 additions & 0 deletions lib/dry/inflector/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def singular(rule, replacement)
# inflections.singular "octopus", "octopi"
# end
def irregular(singular, plural)
raise FrozenError, "can't modify a frozen inflection" if frozen?

uncountables.delete(singular)
uncountables.delete(plural)

Expand All @@ -173,6 +175,8 @@ def irregular(singular, plural)
# inflections.uncountable %w(money information rice)
# end
def uncountable(*words)
raise FrozenError, "can't modify a frozen inflection" if frozen?

uncountables.merge(words.flatten)
end

Expand All @@ -195,6 +199,8 @@ def uncountable(*words)
# inflector.camelize("html") # => "HTML"
# inflector.underscore("HTMLIsFun") # => "html_is_fun"
def acronym(*words)
raise FrozenError, "can't modify a frozen inflection" if frozen?

words.each { |word| @acronyms.add(word.downcase, word) }
end

Expand Down Expand Up @@ -222,9 +228,24 @@ def acronym(*words)
# inflections.human("legacy_col_person_name", "Name")
# end
def human(rule, replacement)
raise FrozenError, "can't modify a frozen inflection" if frozen?

humans.insert(0, [rule, replacement])
end

# Freeze inflections
#
# @since 1.1.0
#
def freeze
@plurals.freeze
@singulars.freeze
@humans.freeze
@uncountables.freeze
@acronyms.freeze
super
end

private

# Add irregular inflection
Expand All @@ -250,6 +271,8 @@ def add_irregular(rule, replacement, target)
# @since 0.1.0
# @api private
def rule(rule, replacement, target)
raise FrozenError, "can't modify a frozen inflection" if frozen?

uncountables.delete(rule)
uncountables.delete(replacement)

Expand Down
15 changes: 15 additions & 0 deletions spec/unit/dry/inflector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@
expect(inflector.method(:inspect)).to eql(inflector.method(:to_s))
end
end

describe "#freeze" do
before(:each) do
inflector.freeze
end

it "freezes an inflections" do
expect { inflector.inflections.plural("ooh", "ooh") }.to raise_error(FrozenError, "can't modify a frozen inflection")
expect { inflector.inflections.singular("ooh", "ooh") }.to raise_error(FrozenError, "can't modify a frozen inflection")
expect { inflector.inflections.irregular("ooh", "ooh") }.to raise_error(FrozenError, "can't modify a frozen inflection")
expect { inflector.inflections.uncountable("ooh") }.to raise_error(FrozenError, "can't modify a frozen inflection")
expect { inflector.inflections.acronym("ooh") }.to raise_error(FrozenError, "can't modify a frozen inflection")
expect { inflector.inflections.human("barbra", "streisand") }.to raise_error(FrozenError, "can't modify a frozen inflection")
end
end
end