diff --git a/lib/dry/inflector.rb b/lib/dry/inflector.rb index b09d23a..cf01f25 100644 --- a/lib/dry/inflector.rb +++ b/lib/dry/inflector.rb @@ -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 @@ -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 @@ -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) diff --git a/lib/dry/inflector/inflections.rb b/lib/dry/inflector/inflections.rb index 2113746..6094161 100644 --- a/lib/dry/inflector/inflections.rb +++ b/lib/dry/inflector/inflections.rb @@ -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) @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/spec/unit/dry/inflector_spec.rb b/spec/unit/dry/inflector_spec.rb index 4153a13..316333a 100644 --- a/spec/unit/dry/inflector_spec.rb +++ b/spec/unit/dry/inflector_spec.rb @@ -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