Permalink
Browse files

Allow custom masculine to feminine forms

Rearrange methods
Updated specs
  • Loading branch information...
1 parent d0d780a commit e3d70b03ba055295d6ab0f980625cc10d96c9ffc @brundage committed May 26, 2012
Showing with 207 additions and 147 deletions.
  1. +109 −73 lib/genderizr.rb
  2. +98 −74 spec/genderizr_spec.rb
View
@@ -1,97 +1,133 @@
+# encoding: utf-8
require 'strscan'
module Genderizr
- VERSION = "0.0.6"
+ VERSION = "0.0.7"
- extend self
-
- def feminize(string)
- genderize(string, true)
+ def self.monkey_patch(mod)
+ mod.send(:include, Genderizr::InstanceMethods)
+ mod.send(:extend, Genderizr::Forms)
end
- def genderize(string,turn_feminine)
- return "" if string.nil?
- lookup = turn_feminine ? masculine_to_feminine : feminine_to_masculine
- result = ""
- s = StringScanner.new(string)
- until s.eos? do
- word = s.scan(/\w+|\W+/)
- result << (lookup[word].nil? ? word : lookup[word])
+ module Changers
+
+ def feminize(string)
+ genderize(string, true)
end
- result
- end
- def masculize(string)
- genderize(string, false)
- end
+ def genderize(string,turn_feminine)
+ return "" if string.nil?
+ lookup = turn_feminine ? masculine_to_feminine : feminine_to_masculine
+ result = ""
+ s = StringScanner.new(string)
+ until s.eos? do
+ word = s.scan(/\w+\W?|\W+/)
+ result << (lookup[word].nil? ? word : lookup[word])
+ end
+ result
+ end
- module StringMethods
- def feminize; Genderizr.feminize(self); end
- def genderize(turn_feminine); Genderizr.genderize(self,turn_feminine); end
- def masculize; Genderizr.masculize(self); end
+ def masculize(string)
+ genderize(string, false)
+ end
+
end
+ extend Genderizr::Changers
-private
+ module Forms
+
+ def m2f_forms
+ @m2f_forms ||= masculine_to_feminine
+ end
+
+
+ def m2f_forms=(hash)
+ @f2m_forms = hash.invert
+ @m2f_forms = hash
+ end
+
+ private
+
+ def feminine_to_masculine
+ @f2m_forms ||= m2f_forms.invert
+ end
+
+
+ def default_m2f_forms
+ {
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ 'boy' => 'girl',
+ 'boyish' => 'girlish',
+ 'boys' => 'girls',
+ 'brother' => 'sister',
+ 'brothers' => 'sisters',
+ 'cowboy' => 'cowgirl',
+ 'cowboys' => 'cowgirls',
+ 'craftsman' => 'craftswoman',
+ 'dad' => 'mom',
+ 'daddy' => 'mommy',
+ 'dick' => 'pussy',
+ 'dude' => 'lady',
+ 'dudes' => 'ladies',
+ 'ex-wife' => 'ex-husband',
+ 'father' => 'mother',
+ 'fathers' => 'mothers',
+ 'gentleman' => 'lady',
+ 'gentlemen' => 'ladies',
+ 'guy' => 'gal',
+ 'guys' => 'gals',
+ 'he' => 'she',
+ 'him' => 'her',
+ 'himself' => 'herself',
+ 'his' => 'her',
+ 'king' => 'queen',
+ 'kings' => 'queens',
+ 'male' => 'female',
+ 'man' => 'woman',
+ 'manhood' => 'womanhood',
+ 'manlier' => 'womanlier',
+ 'manliest' => 'womanliest',
+ 'manliness' => 'womanliness',
+ 'manly' => 'womanly',
+ 'masculine' => 'feminine',
+ 'masculinity' => 'femininity',
+ 'men' => 'women',
+ 'mr' => 'ms',
+ 'mr.' => 'ms.',
+ 'nobleman' => 'noblewoman',
+ 'patriarch' => 'matriarch',
+ 'prince' => 'princess',
+ 'princes' => 'princesses',
+ 'waited' => 'waitressed',
+ 'waiter' => 'waitress'
+ }
+ end
+
+
+ def masculine_to_feminine
+ @m2f_forms ||= default_m2f_forms
+ end
- def feminine_to_masculine
- @f2m ||= masculine_to_feminine.invert
end
+ extend Genderizr::Forms
- def masculine_to_feminine
- {
- 'boy' => 'girl',
- 'boyish' => 'girlish',
- 'boys' => 'girls',
- 'brother' => 'sister',
- 'brothers' => 'sisters',
- 'cowboy' => 'cowgirl',
- 'cowboys' => 'cowgirls',
- 'craftsman' => 'craftswoman',
- 'dad' => 'mom',
- 'daddy' => 'mommy',
- 'dick' => 'pussy',
- 'dude' => 'lady',
- 'dudes' => 'ladies',
- 'ex-wife' => 'ex-husband',
- 'father' => 'mother',
- 'fathers' => 'mothers',
- 'gentleman' => 'lady',
- 'gentlemen' => 'ladies',
- 'guy' => 'gal',
- 'guys' => 'gals',
- 'he' => 'she',
- 'him' => 'her',
- 'himself' => 'herself',
- 'his' => 'her',
- 'king' => 'queen',
- 'kings' => 'queens',
- 'male' => 'female',
- 'man' => 'woman',
- 'manhood' => 'womanhood',
- 'manlier' => 'womanlier',
- 'manliest' => 'womanliest',
- 'manliness' => 'womanliness',
- 'manly' => 'womanly',
- 'masculine' => 'feminine',
- 'masculinity' => 'femininity',
- 'men' => 'women',
- 'mr' => 'ms',
- 'mr.' => 'ms.',
- 'nobleman' => 'noblewoman',
- 'patriarch' => 'matriarch',
- 'prince' => 'princess',
- 'princes' => 'princesses',
- 'waitress' => 'waiter',
- 'waitressed' => 'waited'
- }
+
+ module InstanceMethods
+ def feminize; Genderizr.feminize(self); end
+ def genderize(turn_feminine); Genderizr.genderize(self,turn_feminine); end
+ def masculize; Genderizr.masculize(self); end
end
+
end
if defined?(Rails)
- class String; include Genderizr::StringMethods; end
+ Genderizr.monkey_patch(String)
end
View
@@ -1,90 +1,114 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'genderizr'
-describe Genderizr do
+MASCULINE = "king"
+FEMININE = "queen"
- before :all do
- MASCULINE = "king"
- FEMININE = "queen"
+shared_examples 'a genderized module' do
+
+ it "handles a blank string" do
+ subject.feminize("").should eq ""
+ subject.masculize("").should eq ""
+ end
+
+ it "handles a nil argument" do
+ subject.feminize(nil).should eq ""
+ subject.masculize(nil).should eq ""
+ end
+
+ it "switches genders of a masculine string when requested" do
+ subject.genderize(MASCULINE,true).should eq FEMININE
+ end
+
+ it "doesn't switch genders of a masculine string when requested" do
+ subject.genderize(MASCULINE,false).should eq MASCULINE
+ end
+
+ it "switches genders of a feminine string when requested" do
+ subject.genderize(FEMININE,false).should eq MASCULINE
+ end
+
+ it "doesn't switch genders of a feminine string when requested" do
+ subject.genderize(FEMININE,true).should eq FEMININE
+ end
+
+ it "does the same thing with the explicit and generic method calls" do
+ subject.feminize(MASCULINE).should eq subject.genderize(MASCULINE,true)
+ subject.feminize(FEMININE).should eq subject.genderize(FEMININE,true)
+ subject.masculize(MASCULINE).should eq subject.genderize(MASCULINE,false)
+ subject.masculize(FEMININE).should eq subject.genderize(FEMININE,false)
+ end
+
+ it "ignores punctuation" do
+ subject.feminize("#{MASCULINE}.").should eq "#{FEMININE}."
+ subject.feminize("#{MASCULINE}-").should eq "#{FEMININE}-"
+ subject.feminize("!#{MASCULINE}-").should eq "!#{FEMININE}-"
+ end
+
+ it "doesn't ignore punctuation in 'mr.' and 'mrs.'" do
+ mr = 'mr.'; mrs = 'mrs.'
+ subject.feminize(mr).should eq mrs
+ subject.masculize(mrs).should eq mr
+ end
+
+ it "preserves whitespace" do
+ subject.feminize(" #{MASCULINE}").should eq " #{FEMININE}"
+ subject.feminize(" #{MASCULINE}").should eq " #{FEMININE}"
+ subject.feminize("\t#{MASCULINE}").should eq " #{FEMININE}"
+ end
+
+ it "deals with multi-word strings" do
+ subject.feminize("#{MASCULINE} #{MASCULINE} #{FEMININE}").should eq "#{FEMININE} #{FEMININE} #{FEMININE}"
+ end
+
+end
+
+
+shared_examples 'genderizr form changer' do
+
+ subject { described_class }
+
+ it "allows you to change the masculine to feminine forms" do
+ forms = { "han" => "leia" }
+ subject.m2f_forms = forms
+ subject.m2f_forms.should eq forms
end
- context "module methods" do
-
- it "handles a blank string" do
- subject.feminize("").should eq ""
- subject.masculize("").should eq ""
- end
-
- it "handles a nil argument" do
- subject.feminize(nil).should eq ""
- subject.masculize(nil).should eq ""
- end
-
- it "switches genders of a masculine string when requested" do
- subject.genderize(MASCULINE,true).should eq FEMININE
- end
-
- it "doesn't switch genders of a masculine string when requested" do
- subject.genderize(MASCULINE,false).should eq MASCULINE
- end
-
- it "switches genders of a feminine string when requested" do
- subject.genderize(FEMININE,false).should eq MASCULINE
- end
-
- it "doesn't switch genders of a feminine string when requested" do
- subject.genderize(FEMININE,true).should eq FEMININE
- end
-
- it "does the same thing with the explicit and generic method calls" do
- subject.feminize(MASCULINE).should eq subject.genderize(MASCULINE,true)
- subject.feminize(FEMININE).should eq subject.genderize(FEMININE,true)
- subject.masculize(MASCULINE).should eq subject.genderize(MASCULINE,false)
- subject.masculize(FEMININE).should eq subject.genderize(FEMININE,false)
- end
-
- it "ignores punctuation" do
- subject.feminize("#{MASCULINE}.").should eq "#{FEMININE}."
- subject.feminize("#{MASCULINE}-").should eq "#{FEMININE}-"
- subject.feminize("!#{MASCULINE}-").should eq "!#{FEMININE}-"
- end
-
- it "preserves whitespace" do
- subject.feminize(" #{MASCULINE}").should eq " #{FEMININE}"
- subject.feminize(" #{MASCULINE}").should eq " #{FEMININE}"
- subject.feminize("\t#{MASCULINE}").should eq " #{FEMININE}"
- end
-
- it "deals with multi-word strings" do
- subject.feminize("#{MASCULINE} #{MASCULINE} #{FEMININE}").should eq "#{FEMININE} #{FEMININE} #{FEMININE}"
- end
-
- it "should not add methods to String by default" do
- FEMININE.respond_to?(:feminize).should be false
- FEMININE.respond_to?(:genderize).should be false
- FEMININE.respond_to?(:masculize).should be false
- end
+end
+
+
+describe Genderizr do
+
+ it_behaves_like 'a genderized module'
+ it_behaves_like 'genderizr form changer'
+ it "doesn't add methods to String by default" do
+ FEMININE.should_not respond_to(:feminize)
+ FEMININE.should_not respond_to(:genderize)
+ FEMININE.should_not respond_to(:masculize)
end
- context "String methods" do
+end
+
+
+describe String do
- before :all do
- class String; include Genderizr::StringMethods; end
- end
+ before :all do
+ Genderizr.monkey_patch(String)
+ end
- it "behave the same" do
- FEMININE.feminize.should eq subject.feminize(FEMININE)
- FEMININE.genderize(false).should eq subject.genderize(FEMININE,false)
- FEMININE.genderize(true).should eq subject.genderize(FEMININE,true)
- FEMININE.masculize.should eq subject.masculize(FEMININE)
+ it_behaves_like 'genderizr form changer'
- MASCULINE.feminize.should eq subject.feminize(MASCULINE)
- MASCULINE.genderize(true).should eq subject.genderize(MASCULINE,true)
- MASCULINE.genderize(false).should eq subject.genderize(MASCULINE,false)
- MASCULINE.masculize.should eq subject.masculize(MASCULINE)
- end
+ it "behaves the same" do
+ FEMININE.feminize.should eq Genderizr.feminize(FEMININE)
+ FEMININE.genderize(false).should eq Genderizr.genderize(FEMININE,false)
+ FEMININE.genderize(true).should eq Genderizr.genderize(FEMININE,true)
+ FEMININE.masculize.should eq Genderizr.masculize(FEMININE)
+ MASCULINE.feminize.should eq Genderizr.feminize(MASCULINE)
+ MASCULINE.genderize(true).should eq Genderizr.genderize(MASCULINE,true)
+ MASCULINE.genderize(false).should eq Genderizr.genderize(MASCULINE,false)
+ MASCULINE.masculize.should eq Genderizr.masculize(MASCULINE)
end
end

0 comments on commit e3d70b0

Please sign in to comment.