casings 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +37 -0
- data/lib/casings.rb +13 -0
- data/lib/casings/camel_case.rb +19 -0
- data/lib/casings/pascal_case.rb +15 -0
- data/lib/casings/title_case.rb +43 -0
- data/spec/camel_case_spec.rb +29 -0
- data/spec/pascal_case_spec.rb +25 -0
- data/spec/title_case_spec.rb +34 -0
- metadata +61 -0
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc "run complete spec"
|
5
|
+
Spec::Rake::SpecTask.new do |t|
|
6
|
+
t.spec_opts << "--format specdoc"
|
7
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'clean up all non source files and directories'
|
11
|
+
task :clean do
|
12
|
+
FileUtils.rm_r('pkg', :force => true)
|
13
|
+
FileUtils.rm_r('doc', :force => true)
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
Gem::manage_gems
|
18
|
+
require 'rake/gempackagetask'
|
19
|
+
|
20
|
+
gemspec = Gem::Specification.new do |s|
|
21
|
+
s.name = "casings"
|
22
|
+
s.rubyforge_project = "casings"
|
23
|
+
s.version = "0.5"
|
24
|
+
s.author = "Larry Myers"
|
25
|
+
s.email = "larry@larrymyers.com"
|
26
|
+
s.homepage = "http://rubyforge.org/projects/casings/"
|
27
|
+
s.platform = Gem::Platform::RUBY
|
28
|
+
s.summary = "A mixin for String to add title, camel, and pascal casing"
|
29
|
+
s.files = FileList["{lib,spec}/**/*","Rakefile"].to_a
|
30
|
+
s.require_path = "lib"
|
31
|
+
s.test_files = FileList["test/test*.rb"].to_a
|
32
|
+
s.has_rdoc = true
|
33
|
+
end
|
34
|
+
|
35
|
+
Rake::GemPackageTask.new(gemspec) do |pkg|
|
36
|
+
pkg.need_tar = true
|
37
|
+
end
|
data/lib/casings.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Casings
|
2
|
+
module CamelCase
|
3
|
+
def camel_case
|
4
|
+
parts = []
|
5
|
+
|
6
|
+
self.split(' ').each do |word|
|
7
|
+
word.split('_').each do |part|
|
8
|
+
if parts.empty?
|
9
|
+
parts << part.downcase
|
10
|
+
else
|
11
|
+
parts << part.capitalize
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
parts.join
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Casings
|
2
|
+
module TitleCase
|
3
|
+
# proper short words, conjunctions, and articles
|
4
|
+
LOWERCASE = %w(a at an the and but for nor or so yet in etc)
|
5
|
+
COMPANIES = %w(MCI AT&T BP AMC AMD MS A&M)
|
6
|
+
|
7
|
+
def title_case
|
8
|
+
str = ''
|
9
|
+
|
10
|
+
self.split(' ').each_with_index do |word, i|
|
11
|
+
str << ' ' unless str.empty?
|
12
|
+
|
13
|
+
if COMPANIES.include?(word.upcase)
|
14
|
+
str << word.upcase
|
15
|
+
next
|
16
|
+
end
|
17
|
+
|
18
|
+
if /^[oO]'.+/.match(word)
|
19
|
+
str << word[0..2].upcase + word[3...word.length]
|
20
|
+
next
|
21
|
+
end
|
22
|
+
|
23
|
+
if i == 0
|
24
|
+
str << word.capitalize
|
25
|
+
next
|
26
|
+
end
|
27
|
+
|
28
|
+
if LOWERCASE.include?(word.downcase)
|
29
|
+
str << word.downcase
|
30
|
+
next
|
31
|
+
end
|
32
|
+
|
33
|
+
str << word.capitalize
|
34
|
+
|
35
|
+
str
|
36
|
+
end
|
37
|
+
|
38
|
+
str = self if str.empty?
|
39
|
+
|
40
|
+
str
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'casings/camel_case'
|
4
|
+
|
5
|
+
class String
|
6
|
+
include Casings::CamelCase
|
7
|
+
end
|
8
|
+
|
9
|
+
describe String, " class, when performing camel casing" do
|
10
|
+
it "should always lowercase the first word, uppercase the rest" do
|
11
|
+
"this is sparta".camel_case.should == "thisIsSparta"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should always remove delimiters and collapse words together" do
|
15
|
+
"what a great spec".camel_case.should == "whatAGreatSpec"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should convert space delimited words" do
|
19
|
+
"running back".camel_case.should == "runningBack"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should convert underscored delimited words" do
|
23
|
+
"running_back".camel_case.should == "runningBack"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should convert spaced and underscore delimited words together" do
|
27
|
+
"shifty running_back".camel_case.should == "shiftyRunningBack"
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'casings/pascal_case'
|
4
|
+
|
5
|
+
class String
|
6
|
+
include Casings::PascalCase
|
7
|
+
end
|
8
|
+
|
9
|
+
describe String, " class, when performing pascal casing" do
|
10
|
+
it "should always capitalize each word and remove delimiters" do
|
11
|
+
"this is sparta".pascal_case.should == "ThisIsSparta"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should convert space delimited words" do
|
15
|
+
"running back".pascal_case.should == "RunningBack"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should convert underscored delimited words" do
|
19
|
+
"running_back".pascal_case.should == "RunningBack"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should convert spaced and underscore delimited words together" do
|
23
|
+
"shifty running_back".pascal_case.should == "ShiftyRunningBack"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'casings/title_case'
|
4
|
+
|
5
|
+
class String
|
6
|
+
include Casings::TitleCase
|
7
|
+
end
|
8
|
+
|
9
|
+
describe String, " class, when performing title casing" do
|
10
|
+
it "should always uppercase the letter after a leading O + apostrophe" do
|
11
|
+
"o'brian".title_case.should == "O'Brian"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should always lowercase an S after an apostrophe at the end" do
|
15
|
+
"handel'S messiah".title_case.should == "Handel's Messiah"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should always uppercase name/company abbreviations" do
|
19
|
+
"at&t".title_case.should == "AT&T"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should always capitalize the first word" do
|
23
|
+
"THE CAT IN THE HAT".title_case.should == "The Cat in the Hat"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should lowercase all short words (ex: and, at, the, etc)" do
|
27
|
+
"i'm at turkey hill".title_case.should == "I'm at Turkey Hill"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should uppercase the first occuring letter of a word" do
|
31
|
+
"3com".title_case.should == "3Com"
|
32
|
+
"7-eleven".title_case.should == "7-Eleven"
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: casings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.5"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Larry Myers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-01-25 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: larry@larrymyers.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/casings
|
26
|
+
- lib/casings/camel_case.rb
|
27
|
+
- lib/casings/pascal_case.rb
|
28
|
+
- lib/casings/title_case.rb
|
29
|
+
- lib/casings.rb
|
30
|
+
- spec/camel_case_spec.rb
|
31
|
+
- spec/pascal_case_spec.rb
|
32
|
+
- spec/title_case_spec.rb
|
33
|
+
- Rakefile
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://rubyforge.org/projects/casings/
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project: casings
|
56
|
+
rubygems_version: 1.0.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: A mixin for String to add title, camel, and pascal casing
|
60
|
+
test_files: []
|
61
|
+
|