├── .gitignore ├── lib ├── titleize │ └── version.rb └── titleize.rb ├── Gemfile ├── spec ├── spec_helper.rb └── titleize_spec.rb ├── Rakefile ├── History.txt ├── titleize.gemspec └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/titleize/version.rb: -------------------------------------------------------------------------------- 1 | module Titleize 2 | VERSION = "1.4.1" 3 | end 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in titleize.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | dir = File.dirname(__FILE__) 2 | lib_path = File.expand_path("#{dir}/../lib") 3 | $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path) 4 | 5 | require 'titleize' 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | require 'rspec/core/rake_task' 3 | Bundler::GemHelper.install_tasks 4 | 5 | desc "Run all specs" 6 | RSpec::Core::RakeTask.new(:spec) do |spec| 7 | spec.pattern = 'spec/*_spec.rb' 8 | spec.rspec_opts = '--format documentation --colour --backtrace' 9 | end 10 | 11 | task :default => [:spec] 12 | -------------------------------------------------------------------------------- /History.txt: -------------------------------------------------------------------------------- 1 | === 1.4.1 / 2016-09-16 2 | 3 | * fix capitalization of repeated small words 4 | * handle multiple punctuated small words ("v.", "vs.") 5 | 6 | === 1.4.0 / 2016-02-04 7 | 8 | * check for the presence of ActiveSupport::Inflector instead of just ActiveSupport [Connor Prussin] 9 | 10 | === 1.3.0 / 2013-03-07 11 | 12 | * add String#titleize! [Scott Pullen] 13 | * allow disabling of humanize/underscore in ActiveSupport#titleize [Jayme Deffenbaugh] 14 | * RSpec 2 15 | 16 | === 1.2.1 / 2011-04-15 17 | 18 | * Bundler 19 | 20 | === 1.2.0 / 2010-07-22 21 | 22 | * Ruby 1.9 compatibility. [Jason Weathered] 23 | * Active Support's Inflector is under the ActiveSupport namespace. [Jason Weathered] 24 | * have String#titleize act like Inflector#titleize when ActiveSupport is loaded 25 | 26 | === 1.1.0 / 2010-03-18 27 | 28 | * fix all-caps titles 29 | * don't capitalize words that start with a number 30 | * handle hyphenated words 31 | * change project url to rubygems.org 32 | 33 | === 1.0.0 / 2008-05-22 34 | 35 | * Hoe-ized for RubyGemming 36 | 37 | 38 | -------------------------------------------------------------------------------- /titleize.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "titleize/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "titleize" 7 | s.version = Titleize::VERSION 8 | s.platform = Gem::Platform::RUBY 9 | s.authors = ["Grant Hollingworth"] 10 | s.email = ["grant@antiflux.org"] 11 | s.homepage = "http://rubygems.org/gems/titleize" 12 | s.summary = "Adds String#titleize for creating properly capitalized titles." 13 | s.description = "#{s.summary} Replaces ActiveSupport::Inflector.titleize if ActiveSupport is present." 14 | 15 | s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version= 16 | s.rubyforge_project = "titleize" 17 | 18 | s.add_development_dependency "rspec", "~> 2.13" 19 | s.add_development_dependency "rake" 20 | 21 | s.files = `git ls-files`.split("\n") 22 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 23 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 24 | s.require_paths = ["lib"] 25 | end 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Titleize 2 | 3 | http://rubygems.org/gems/titleize 4 | 5 | ### Description 6 | 7 | Adds `String#titleize` for creating properly capitalized titles. 8 | It can be called as `Titleize.titleize` or `"a string".titleize`. It is also 9 | aliased as `titlecase`. 10 | 11 | The list of "small words" which are not capped comes from the New York Times 12 | Manual of Style, plus 'vs' and 'v'. 13 | 14 | If loaded in a Rails environment, it modifies `Inflector#titleize`. By default 15 | ActiveSupport calls `Inflector#underscore` and `Inflector#humanize`. This however 16 | can be problematic with words like "iPod", "GPS", and "McArthur". To disable 17 | this behavior, use the options `:underscore => false` and `:humanize => false`. 18 | 19 | Based on [TitleCase.pl](http://daringfireball.net/2008/05/title_case) by John Gruber. 20 | 21 | ### Synopsis 22 | 23 | `"a lovely and talented title".titleize # => "A Lovely and Talented Title"` 24 | 25 | ### Install 26 | 27 | `gem install titleize` 28 | 29 | ### License 30 | 31 | (The MIT License) 32 | 33 | Copyright (c) 2008 Grant Hollingworth 34 | 35 | Permission is hereby granted, free of charge, to any person obtaining 36 | a copy of this software and associated documentation files (the 37 | 'Software'), to deal in the Software without restriction, including 38 | without limitation the rights to use, copy, modify, merge, publish, 39 | distribute, sublicense, and/or sell copies of the Software, and to 40 | permit persons to whom the Software is furnished to do so, subject to 41 | the following conditions: 42 | 43 | The above copyright notice and this permission notice shall be 44 | included in all copies or substantial portions of the Software. 45 | 46 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 47 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 48 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 49 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 50 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 51 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 52 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 53 | -------------------------------------------------------------------------------- /lib/titleize.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Adds String#titleize for creating properly capitalized titles. 3 | # It can be called as Titleize.titleize or "a string".titleize. 4 | # 5 | # titlecase is included as an alias for titleize. 6 | # 7 | # If loaded in a Rails environment, it modifies Inflector.titleize. 8 | module Titleize 9 | SMALL_WORDS = %w{a an and as at but by en for if in of on or the to v v. via vs vs.} 10 | 11 | extend self 12 | 13 | # Capitalizes most words to create a nicer looking title string. 14 | # 15 | # The list of "small words" which are not capped comes from 16 | # the New York Times Manual of Style, plus 'vs' and 'v'. 17 | # 18 | # "notes on a scandal" # => "Notes on a Scandal" 19 | # "the good german" # => "The Good German" 20 | def titleize(title) 21 | title = title.dup 22 | title.downcase! unless title[/[[:lower:]]/] # assume all-caps need fixing 23 | 24 | phrases(title).map do |phrase| 25 | words = phrase.split 26 | words.map.with_index do |word, index| 27 | def word.capitalize 28 | # like String#capitalize, but it starts with the first letter 29 | self.sub(/[[:alpha:]].*/) {|subword| subword.capitalize} 30 | end 31 | 32 | case word 33 | when /[[:alpha:]]\.[[:alpha:]]/ # words with dots in, like "example.com" 34 | word 35 | when /[-‑]/ # hyphenated word (regular and non-breaking) 36 | word.split(/([-‑])/).map do |part| 37 | SMALL_WORDS.include?(part) ? part : part.capitalize 38 | end.join 39 | when /^[[:alpha:]].*[[:upper:]]/ # non-first letter capitalized already 40 | word 41 | when /^[[:digit:]]/ # first character is a number 42 | word 43 | when *(SMALL_WORDS + SMALL_WORDS.map {|small| small.capitalize }) 44 | if index == 0 || index == words.size - 1 45 | word.capitalize 46 | else 47 | word.downcase 48 | end 49 | else 50 | word.capitalize 51 | end 52 | end.join(" ") 53 | end.join(" ") 54 | end 55 | 56 | # Splits a title into an array based on punctuation. 57 | # 58 | # "simple title" # => ["simple title"] 59 | # "more complicated: titling" # => ["more complicated:", "titling"] 60 | def phrases(title) 61 | phrases = [[]] 62 | title.split.each do |word| 63 | phrases.last << word 64 | phrases << [] if ends_with_punctuation?(word) && !small_word?(word) 65 | end 66 | phrases.reject(&:empty?).map { |phrase| phrase.join " " } 67 | end 68 | 69 | private 70 | 71 | def small_word?(word) 72 | SMALL_WORDS.include? word.downcase 73 | end 74 | 75 | def ends_with_punctuation?(word) 76 | word =~ /[:.;?!]$/ 77 | end 78 | end 79 | 80 | class String 81 | # Capitalizes most words to create a nicer looking title string. 82 | # 83 | # The list of "small words" which are not capped comes from 84 | # the New York Times Manual of Style, plus 'vs' and 'v'. 85 | # 86 | # titleize is also aliased as titlecase. 87 | # 88 | # "notes on a scandal" # => "Notes on a Scandal" 89 | # "the good german" # => "The Good German" 90 | def titleize(opts={}) 91 | if defined? ActiveSupport::Inflector 92 | ActiveSupport::Inflector.titleize(self, opts) 93 | else 94 | Titleize.titleize(self) 95 | end 96 | end 97 | alias_method :titlecase, :titleize 98 | 99 | def titleize! 100 | replace(titleize) 101 | end 102 | alias_method :titlecase!, :titleize! 103 | end 104 | 105 | if defined? ActiveSupport::Inflector 106 | module ActiveSupport::Inflector 107 | extend self 108 | 109 | # Capitalizes most words to create a nicer looking title string. 110 | # 111 | # The list of "small words" which are not capped comes from 112 | # the New York Times Manual of Style, plus 'vs' and 'v'. 113 | # 114 | # This replaces the default Rails titleize. Like the default, it uses 115 | # Inflector.underscore and Inflector.humanize to convert 116 | # underscored_names and CamelCaseNames to a more human form. However, you can change 117 | # this behavior by passing :humanize => false or :underscore => false as options. 118 | # This can be useful when dealing with words like "iPod" and "GPS". 119 | # 120 | # titleize is also aliased as titlecase. 121 | # 122 | # "notes on an active_record" # => "Notes on an Active Record" 123 | # "the GoodGerman" # => "The Good German" 124 | def titleize(title, opts={}) 125 | opts = {:humanize => true, :underscore => true}.merge(opts) 126 | title = ActiveSupport::Inflector.underscore(title) if opts[:underscore] 127 | title = ActiveSupport::Inflector.humanize(title) if opts[:humanize] 128 | 129 | Titleize.titleize(title) 130 | end 131 | alias_method :titlecase, :titleize 132 | end 133 | end 134 | -------------------------------------------------------------------------------- /spec/titleize_spec.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | module ActiveSupport 4 | module Inflector 5 | #stub 6 | def underscore(string) string; end 7 | def humanize(string) string; end 8 | end 9 | end 10 | 11 | require 'spec_helper' 12 | 13 | SMALL_WORDS = %w{a an and as at but by en for if in of on or the to v v. via vs vs.} 14 | 15 | describe Titleize do 16 | include Titleize 17 | 18 | describe "phrases" do 19 | it "should return an array" do 20 | phrases("a little sentence").should be_an_instance_of(Array) 21 | end 22 | 23 | it "should split on colons" do 24 | phrases("this: a subphrase").should == ["this:", "a subphrase"] 25 | end 26 | 27 | it "should split on semi-colons" do 28 | phrases("this; that").should == ["this;", "that"] 29 | end 30 | 31 | it "should split on question marks" do 32 | phrases("this? that").should == ["this?", "that"] 33 | end 34 | 35 | it "should split on periods" do 36 | phrases("this. that.").should == ["this.", "that."] 37 | end 38 | 39 | it "should split on exclamation marks" do 40 | phrases("headache! yes").should == ["headache!", "yes"] 41 | end 42 | 43 | it "should rejoin into the original string" do 44 | title = "happy: not sad; pushing! laughing? ok." 45 | phrases(title).join(" ").should == title 46 | end 47 | 48 | it "should not get confused by small words with punctuation" do 49 | phrases("this vs. that").should == ["this vs. that"] 50 | phrases("this vs. that. no").should == ["this vs. that.", "no"] 51 | phrases("this: that vs. him. no. why?").should == 52 | ["this:", "that vs. him.", "no.", "why?"] 53 | phrases("this vs. that vs. what").should == ["this vs. that vs. what"] 54 | end 55 | 56 | it "should handle punctuation combined with a small word as the last word" do 57 | phrases("this. that of").should == ["this.", "that of"] 58 | end 59 | end 60 | 61 | describe "titleize" do 62 | it "should return a string" do 63 | titleize("this").should be_an_instance_of(String) 64 | end 65 | 66 | it "should capitalize the first letter of regular words" do 67 | titleize("cat beats monkey").should == "Cat Beats Monkey" 68 | end 69 | 70 | it "should not capitalize small words" do 71 | SMALL_WORDS.each do |word| 72 | titleize("first #{word} last").should == "First #{word} Last" 73 | end 74 | end 75 | 76 | it "should downcase a small word if it is capitalized" do 77 | SMALL_WORDS.each do |word| 78 | titleize("first #{word.capitalize} last").should == "First #{word} Last" 79 | end 80 | end 81 | 82 | it "should capitalize a small word if it is the first word" do 83 | SMALL_WORDS.each do |word| 84 | titleize("#{word} is small").should == "#{word.capitalize} Is Small" 85 | titleize("after: #{word} ok").should == "After: #{word.capitalize} Ok" 86 | titleize("after; #{word} ok").should == "After; #{word.capitalize} Ok" 87 | titleize("after. #{word} ok").should == "After. #{word.capitalize} Ok" 88 | titleize("after? #{word} ok").should == "After? #{word.capitalize} Ok" 89 | titleize("after! #{word} ok").should == "After! #{word.capitalize} Ok" 90 | end 91 | end 92 | 93 | it "does not capitalize later uses of the first small word" do 94 | SMALL_WORDS.each do |word| 95 | titleize("#{word} and #{word} are different").should == "#{word.capitalize} and #{word} Are Different" 96 | end 97 | end 98 | 99 | it "should capitalize a small word if it is the last word" do 100 | SMALL_WORDS.each do |word| 101 | titleize("small #{word}").should == "Small #{word.capitalize}" 102 | end 103 | end 104 | 105 | it "should not screw up acronyms" do 106 | titleize("the SEC's decision").should == "The SEC's Decision" 107 | end 108 | 109 | it "should not capitalize words with dots" do 110 | titleize("del.icio.us web site").should == "del.icio.us Web Site" 111 | end 112 | 113 | it "should not think a quotation mark makes a dot word" do 114 | titleize("'quoted.' yes.").should == "'Quoted.' Yes." 115 | titleize("ends with 'quotation.'").should == "Ends With 'Quotation.'" 116 | end 117 | 118 | it "should not capitalize words that have a lowercase first letter" do 119 | titleize("iTunes").should == "iTunes" 120 | end 121 | 122 | it "should not capitalize words that start with a number" do 123 | titleize("2lmc").should == "2lmc" 124 | end 125 | 126 | describe "with hyphens" do 127 | it "should handle hyphenated words" do 128 | titleize("iPhone la-la land").should == "iPhone La-La Land" 129 | end 130 | 131 | it "should handle non-breaking hyphens" do 132 | titleize("non‑breaking hyphen").should == "Non‑Breaking Hyphen" 133 | end 134 | 135 | it "should not capitalize small words within a hyphenated word" do 136 | titleize("step-by-step directions").should == "Step-by-Step Directions" 137 | end 138 | end 139 | 140 | it "should fix all-caps titles" do 141 | titleize("IF IT’S ALL CAPS, FIX IT").should == "If It’s All Caps, Fix It" 142 | end 143 | 144 | # test suite from Perl titlecase 145 | # http://github.com/ap/titlecase/blob/master/test.pl 146 | it "should handle edge cases" do 147 | { 148 | %{For step-by-step directions email someone@gmail.com} => 149 | %{For Step-by-Step Directions Email someone@gmail.com}, 150 | 151 | %{2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'} => 152 | %{2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'}, 153 | 154 | %{Have you read “The Lottery”?} => 155 | %{Have You Read “The Lottery”?}, 156 | 157 | %{your hair[cut] looks (nice)} => 158 | %{Your Hair[cut] Looks (Nice)}, 159 | 160 | %{People probably won't put http://foo.com/bar/ in titles} => 161 | %{People Probably Won't Put http://foo.com/bar/ in Titles}, 162 | 163 | %{Scott Moritz and TheStreet.com’s million iPhone la‑la land} => 164 | %{Scott Moritz and TheStreet.com’s Million iPhone La‑La Land}, 165 | 166 | %{BlackBerry vs. iPhone} => 167 | %{BlackBerry vs. iPhone}, 168 | 169 | %{Notes and observations regarding Apple’s announcements from ‘The Beat Goes On’ special event} => 170 | %{Notes and Observations Regarding Apple’s Announcements From ‘The Beat Goes On’ Special Event}, 171 | 172 | %{Read markdown_rules.txt to find out how _underscores around words_ will be interpretted} => 173 | %{Read markdown_rules.txt to Find Out How _Underscores Around Words_ Will Be Interpretted}, 174 | 175 | %{Q&A with Steve Jobs: 'That's what happens in technology'} => 176 | %{Q&A With Steve Jobs: 'That's What Happens in Technology'}, 177 | 178 | %{What Is AT&T's Problem?} => %{What Is AT&T's Problem?}, 179 | 180 | %{Apple Deal With AT&T Falls Through} => 181 | %{Apple Deal With AT&T Falls Through}, 182 | 183 | %{this v that} => %{This v That}, 184 | %{this vs that} => %{This vs That}, 185 | %{this v. that} => %{This v. That}, 186 | %{this vs. that} => %{This vs. That}, 187 | 188 | %{The SEC's Apple Probe: What You Need to Know} => 189 | %{The SEC's Apple Probe: What You Need to Know}, 190 | 191 | %{'by the Way, small word at the start but within quotes.'} => 192 | %{'By the Way, Small Word at the Start but Within Quotes.'}, 193 | 194 | %{Small word at end is nothing to be afraid of} => 195 | %{Small Word at End Is Nothing to Be Afraid Of}, 196 | 197 | %{Starting Sub-Phrase With a Small Word: a Trick, Perhaps?} => 198 | %{Starting Sub-Phrase With a Small Word: A Trick, Perhaps?}, 199 | 200 | %{Sub-Phrase With a Small Word in Quotes: 'a Trick, Perhaps?'} => 201 | %{Sub-Phrase With a Small Word in Quotes: 'A Trick, Perhaps?'}, 202 | 203 | %{Sub-Phrase With a Small Word in Quotes: "a Trick, Perhaps?"} => 204 | %{Sub-Phrase With a Small Word in Quotes: "A Trick, Perhaps?"}, 205 | 206 | %{"Nothing to Be Afraid of?"} => %{"Nothing to Be Afraid Of?"}, 207 | %{"Nothing to Be Afraid Of?"} => %{"Nothing to Be Afraid Of?"}, 208 | %{a thing} => %{A Thing}, 209 | 210 | %{Dr. Strangelove (or: how I Learned to Stop Worrying and Love the Bomb)} => 211 | %{Dr. Strangelove (Or: How I Learned to Stop Worrying and Love the Bomb)}, 212 | 213 | %{ this is trimming} => 214 | %{This Is Trimming}, 215 | 216 | %{this is trimming } => 217 | %{This Is Trimming}, 218 | 219 | %{ this is trimming } => 220 | %{This Is Trimming}, 221 | 222 | %{IF IT’S ALL CAPS, FIX IT} => 223 | %{If It’s All Caps, Fix It}, 224 | }.each do |before, after| 225 | titleize(before).should == after 226 | end 227 | end 228 | end 229 | 230 | it "should have titleize as a singleton method" do 231 | Titleize.singleton_methods.map(&:to_sym).should include(:titleize) 232 | end 233 | end 234 | 235 | describe ActiveSupport::Inflector do 236 | include ActiveSupport::Inflector 237 | 238 | describe "titleize" do 239 | before(:each) do 240 | @title = "active_record and ActiveResource" 241 | end 242 | 243 | it "should call humanize and underscore like the default in Rails" do 244 | underscored_title = "active_record and active_resource" 245 | humanized_title = "Active record and active resource" 246 | ActiveSupport::Inflector.should_receive(:underscore).with(@title).and_return(underscored_title) 247 | ActiveSupport::Inflector.should_receive(:humanize).with(underscored_title).and_return(humanized_title) 248 | titleize(@title).should == "Active Record and Active Resource" 249 | end 250 | 251 | it "should allow disabling of Inflector#underscore" do 252 | humanized_title = "Active record and activeresource" 253 | ActiveSupport::Inflector.should_not_receive(:underscore) 254 | ActiveSupport::Inflector.should_receive(:humanize).with(@title).and_return(humanized_title) 255 | titleize(@title, :underscore => false).should == "Active Record and Activeresource" 256 | end 257 | 258 | it "should allow disabling of Inflector#humanize" do 259 | underscored_title = "active_record and active_resource" 260 | ActiveSupport::Inflector.should_not_receive(:humanize) 261 | ActiveSupport::Inflector.should_receive(:underscore).with(@title).and_return(underscored_title) 262 | titleize(@title, :humanize => false).should == "Active_record and Active_resource" 263 | end 264 | 265 | it "should replace Inflector.titleize" do 266 | Titleize.should_receive(:titleize).with(@title) 267 | ActiveSupport::Inflector.stub!(:underscore).and_return(@title) 268 | ActiveSupport::Inflector.stub!(:humanize).and_return(@title) 269 | ActiveSupport::Inflector.titleize(@title) 270 | end 271 | 272 | it "should be aliased as titlecase" do 273 | ActiveSupport::Inflector.singleton_methods.map(&:to_sym).should include(:titlecase) 274 | ActiveSupport::Inflector.stub!(:titlecase).and_return("title") 275 | ActiveSupport::Inflector.stub!(:titleize).and_return("title") 276 | ActiveSupport::Inflector.titlecase("this").should == ActiveSupport::Inflector.titleize("this") 277 | end 278 | end 279 | end 280 | 281 | describe String do 282 | it "should have a titleize method" do 283 | String.instance_methods.map(&:to_sym).should include(:titleize) 284 | end 285 | 286 | it "should have a titleize! method" do 287 | String.instance_methods.map(&:to_sym).should include(:titleize!) 288 | end 289 | 290 | it "should work" do 291 | "this is a test".titleize.should == "This Is a Test" 292 | end 293 | 294 | it "should be aliased as #titlecase" do 295 | String.instance_methods.map(&:to_sym).should include(:titlecase) 296 | title = "this is a pile of testing text" 297 | title.titlecase.should == title.titleize 298 | end 299 | 300 | context 'when using the self modified version of titleize' do 301 | it 'should work' do 302 | test_str = 'this is a test' 303 | test_str.titleize! 304 | test_str.should == 'This Is a Test' 305 | end 306 | 307 | it 'should be aliased as #titlecase!' do 308 | test_str = 'this is a test' 309 | test_str.titlecase! 310 | test_str.should == 'This Is a Test' 311 | end 312 | end 313 | 314 | context "when ActiveSupport is loaded" do 315 | it "should act the same as Inflector#titleize" do 316 | ActiveSupport::Inflector.should_receive(:titleize).with("title", {}) 317 | "title".titleize 318 | end 319 | 320 | it "should allow disabling of Inflector#underscore" do 321 | ActiveSupport::Inflector.should_not_receive(:underscore) 322 | "title".titleize(:underscore => false) 323 | end 324 | 325 | it "should allow disabling of Inflector#humanize" do 326 | ActiveSupport::Inflector.should_not_receive(:humanize) 327 | "title".titleize(:humanize => false) 328 | end 329 | end 330 | 331 | context "when ActiveSupport is not loaded" do 332 | before(:all) do 333 | Object.send :remove_const, :ActiveSupport 334 | end 335 | 336 | it "should work" do 337 | lambda { "foo".titleize }.should_not raise_exception 338 | end 339 | 340 | it "should call Titleize#titleize" do 341 | Titleize.should_receive(:titleize).with("title") 342 | "title".titleize 343 | end 344 | end 345 | end 346 | --------------------------------------------------------------------------------