ruby-nuggets 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/nuggets/string/camelscore.rb +5 -0
- data/lib/nuggets/string/camelscore_mixin.rb +117 -0
- data/lib/nuggets/version.rb +1 -1
- data/spec/nuggets/string/camelscore_spec.rb +116 -0
- metadata +9 -6
data/README
CHANGED
@@ -0,0 +1,117 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2012 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU Affero General Public License as published by #
|
14
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
+
# option) any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
+
# for more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU Affero General Public License #
|
23
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class String
|
30
|
+
module CamelscoreMixin
|
31
|
+
|
32
|
+
# List of acronyms to treat specially
|
33
|
+
# in #camelcase and #underscore.
|
34
|
+
CAMELSCORE_ACRONYMS = {
|
35
|
+
'html' => 'HTML',
|
36
|
+
'rss' => 'RSS',
|
37
|
+
'sql' => 'SQL',
|
38
|
+
'ssl' => 'SSL',
|
39
|
+
'xml' => 'XML'
|
40
|
+
}
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# str.camelcase => new_string
|
44
|
+
#
|
45
|
+
# Returns the CamelCase form of _str_.
|
46
|
+
def camelcase
|
47
|
+
dup.camelcase!
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :camelize, :camelcase
|
51
|
+
|
52
|
+
# call-seq:
|
53
|
+
# str.camelcase! => str
|
54
|
+
#
|
55
|
+
# Replaces _str_ with its CamelCase form and returns _str_.
|
56
|
+
def camelcase!
|
57
|
+
sub!(/^[a-z]+/) {
|
58
|
+
CAMELSCORE_ACRONYMS[$&] || $&.capitalize
|
59
|
+
}
|
60
|
+
|
61
|
+
gsub!(/(?:_|([\/\d]))([a-z]+)/i) {
|
62
|
+
"#{$1}#{CAMELSCORE_ACRONYMS[$2] || $2.capitalize}"
|
63
|
+
}
|
64
|
+
|
65
|
+
gsub!('/', '::')
|
66
|
+
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
alias_method :camelize!, :camelcase!
|
71
|
+
|
72
|
+
# call-seq:
|
73
|
+
# str.underscore => new_string
|
74
|
+
#
|
75
|
+
# Returns the under_score form of _str_.
|
76
|
+
def underscore
|
77
|
+
dup.underscore!
|
78
|
+
end
|
79
|
+
|
80
|
+
# call-seq:
|
81
|
+
# str.underscore! => str
|
82
|
+
#
|
83
|
+
# Replaces _str_ with its under_score form and returns _str_.
|
84
|
+
def underscore!
|
85
|
+
gsub!(/::/, '/')
|
86
|
+
|
87
|
+
a = CAMELSCORE_ACRONYMS.values
|
88
|
+
r = a.empty? ? /(?=a)b/ : Regexp.union(*a)
|
89
|
+
|
90
|
+
gsub!(/(?:([A-Za-z])|(\d)|^)(#{r})(?=\b|[^a-z])/) {
|
91
|
+
"#{$1 || $2}#{'_' if $1}#{$3.downcase}"
|
92
|
+
}
|
93
|
+
|
94
|
+
gsub!(/([A-Z])(?=[A-Z])/, '\1_')
|
95
|
+
gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
96
|
+
|
97
|
+
downcase!
|
98
|
+
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
# call-seq:
|
103
|
+
# str.constantize(base = Object) => anObject
|
104
|
+
#
|
105
|
+
# Returns the constant pointed to by _str_, relative to +base+.
|
106
|
+
def constantize(base = Object)
|
107
|
+
names = split('::')
|
108
|
+
return if names.empty?
|
109
|
+
|
110
|
+
const = names.first.empty? ? (names.shift; Object) : base
|
111
|
+
names.each { |name| const = const.const_get(name) }
|
112
|
+
const
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'nuggets/string/camelscore'
|
2
|
+
|
3
|
+
describe String, 'when extended by', Nuggets::String::CamelscoreMixin do
|
4
|
+
|
5
|
+
it { String.ancestors.should include(Nuggets::String::CamelscoreMixin) }
|
6
|
+
|
7
|
+
def self.with_acronyms(acronyms, &block)
|
8
|
+
describe "with acronyms #{acronyms.inspect}" do
|
9
|
+
before :all do String::CAMELSCORE_ACRONYMS.replace(acronyms) end
|
10
|
+
after :all do String::CAMELSCORE_ACRONYMS.clear end
|
11
|
+
|
12
|
+
instance_eval(&block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
with_acronyms({}) { [
|
17
|
+
['', ''],
|
18
|
+
%w[a A],
|
19
|
+
%w[a_b AB],
|
20
|
+
%w[a_b_c ABC],
|
21
|
+
%w[a/b A::B],
|
22
|
+
%w[a/b_c A::BC],
|
23
|
+
%w[a/b/c A::B::C],
|
24
|
+
%w[foo Foo],
|
25
|
+
%w[foo_bar FooBar],
|
26
|
+
%w[foo_bar_baz FooBarBaz],
|
27
|
+
%w[foo/bar Foo::Bar],
|
28
|
+
%w[foo/bar_baz Foo::BarBaz],
|
29
|
+
%w[foo/bar/baz Foo::Bar::Baz],
|
30
|
+
%w[active_model ActiveModel],
|
31
|
+
%w[active_model/errors ActiveModel::Errors],
|
32
|
+
%w[ssl_error SslError],
|
33
|
+
%w[s_s_l_error SSLError]
|
34
|
+
].each { |a, b|
|
35
|
+
example { a.camelcase.should == b }
|
36
|
+
example { b.underscore.should == a }
|
37
|
+
} }
|
38
|
+
|
39
|
+
with_acronyms('ab' => 'AB') {
|
40
|
+
example { 'ab'.camelcase.should == 'AB' }
|
41
|
+
example { 'AB'.underscore.should == 'ab' }
|
42
|
+
example { 'abc'.camelcase.should == 'Abc' }
|
43
|
+
example { 'Abc'.underscore.should == 'abc' }
|
44
|
+
example { 'ab_c'.camelcase.should == 'ABC' }
|
45
|
+
example { 'ABC'.underscore.should == 'ab_c' }
|
46
|
+
}
|
47
|
+
|
48
|
+
with_acronyms('abc' => 'ABC') {
|
49
|
+
example { 'ab'.camelcase.should == 'Ab' }
|
50
|
+
example { 'AB'.underscore.should == 'a_b' }
|
51
|
+
example { 'abc'.camelcase.should == 'ABC' }
|
52
|
+
example { 'ABC'.underscore.should == 'abc' }
|
53
|
+
}
|
54
|
+
|
55
|
+
with_acronyms('ab' => 'AB', 'abc' => 'ABC') {
|
56
|
+
example { 'ab'.camelcase.should == 'AB' }
|
57
|
+
example { 'AB'.underscore.should == 'ab' }
|
58
|
+
example { 'abc'.camelcase.should == 'ABC' }
|
59
|
+
example { 'Abc'.underscore.should == 'abc' }
|
60
|
+
example { 'ab_c'.camelcase.should == 'ABC' }
|
61
|
+
example { 'ABC'.underscore.should == 'ab_c' }
|
62
|
+
}
|
63
|
+
|
64
|
+
with_acronyms('xml' => 'XML', 'html' => 'HTML', 'sql' => 'SQL') {
|
65
|
+
example { 'xml'.camelcase.should == 'XML' }
|
66
|
+
example { 'XML'.underscore.should == 'xml' }
|
67
|
+
example { 'html'.camelcase.should == 'HTML' }
|
68
|
+
example { 'HTML'.underscore.should == 'html' }
|
69
|
+
example { 'html5'.camelcase.should == 'HTML5' }
|
70
|
+
example { 'HTML5'.underscore.should == 'html5' }
|
71
|
+
pending { 'xhtml'.camelcase.should == 'XHTML' }
|
72
|
+
pending { 'XHTML'.underscore.should == 'xhtml' }
|
73
|
+
example { 'sql'.camelcase.should == 'SQL' }
|
74
|
+
example { 'SQL'.underscore.should == 'sql' }
|
75
|
+
pending { 'mysql'.camelcase.should == 'MySQL' }
|
76
|
+
pending { 'MySQL'.underscore.should == 'mysql' }
|
77
|
+
pending { 'postgresql'.camelcase.should == 'PostgreSQL' }
|
78
|
+
pending { 'PostgreSQL'.underscore.should == 'postgresql' }
|
79
|
+
pending { 'sqlite'.camelcase.should == 'SQLite' }
|
80
|
+
pending { 'SQLite'.underscore.should == 'sqlite' }
|
81
|
+
pending { 'nosql'.camelcase.should == 'NoSQL' }
|
82
|
+
pending { 'NoSQL'.underscore.should == 'nosql' }
|
83
|
+
}
|
84
|
+
|
85
|
+
with_acronyms('rss' => 'RSS', 'xml' => 'XML') {
|
86
|
+
example { 'rss2xml'.camelcase.should == 'RSS2XML' }
|
87
|
+
example { 'RSS2XML'.underscore.should == 'rss2xml' }
|
88
|
+
}
|
89
|
+
|
90
|
+
with_acronyms('ssl' => 'SSL') {
|
91
|
+
example { 'ssl'.camelcase.should == 'SSL' }
|
92
|
+
example { 'SSL'.underscore.should == 'ssl' }
|
93
|
+
example { 'ssl_error'.camelcase.should == 'SSLError' }
|
94
|
+
example { 'SSLError'.underscore.should == 'ssl_error' }
|
95
|
+
}
|
96
|
+
|
97
|
+
example { ''.constantize.should be_nil }
|
98
|
+
example { '::'.constantize.should be_nil }
|
99
|
+
|
100
|
+
example { 'Nuggets'.constantize.should == ::Nuggets }
|
101
|
+
example { '::Nuggets'.constantize.should == ::Nuggets }
|
102
|
+
|
103
|
+
example { 'Nuggets::String'.constantize.should == ::Nuggets::String }
|
104
|
+
example { '::Nuggets::String'.constantize.should == ::Nuggets::String }
|
105
|
+
|
106
|
+
example { 'String'.constantize.should == ::String }
|
107
|
+
example { 'String'.constantize(::Nuggets).should == ::Nuggets::String }
|
108
|
+
example { '::String'.constantize(::Nuggets).should == ::String }
|
109
|
+
example { 'String'.constantize(::Nuggets::String).should == ::String }
|
110
|
+
example { 'String'.constantize(::Nuggets::String::CamelscoreMixin).should == ::String }
|
111
|
+
|
112
|
+
example { lambda { 'CamelscoreMixin'.constantize }.should raise_error(NameError) }
|
113
|
+
example { lambda { 'CamelscoreMixin'.constantize(::Nuggets) }.should raise_error(NameError) }
|
114
|
+
example { 'CamelscoreMixin'.constantize(::Nuggets::String).should == ::Nuggets::String::CamelscoreMixin }
|
115
|
+
|
116
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 53
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 5
|
10
|
+
version: 0.8.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jens Wille
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-27 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Some extensions to the Ruby programming language.
|
@@ -85,9 +85,11 @@ files:
|
|
85
85
|
- lib/nuggets/string/capitalize_first.rb
|
86
86
|
- lib/nuggets/string/wc.rb
|
87
87
|
- lib/nuggets/string/evaluate.rb
|
88
|
+
- lib/nuggets/string/camelscore_mixin.rb
|
88
89
|
- lib/nuggets/string/msub.rb
|
89
90
|
- lib/nuggets/string/nsub.rb
|
90
91
|
- lib/nuggets/string/xor.rb
|
92
|
+
- lib/nuggets/string/camelscore.rb
|
91
93
|
- lib/nuggets/string/wc_mixin.rb
|
92
94
|
- lib/nuggets/string/case.rb
|
93
95
|
- lib/nuggets/string/xor_mixin.rb
|
@@ -181,6 +183,7 @@ files:
|
|
181
183
|
- spec/nuggets/integer/map_spec.rb
|
182
184
|
- spec/nuggets/integer/length_spec.rb
|
183
185
|
- spec/nuggets/string/evaluate_spec.rb
|
186
|
+
- spec/nuggets/string/camelscore_spec.rb
|
184
187
|
- spec/nuggets/string/xor_spec.rb
|
185
188
|
- spec/nuggets/string/wc_spec.rb
|
186
189
|
- spec/nuggets/array/regression_spec.rb
|
@@ -203,14 +206,14 @@ licenses: []
|
|
203
206
|
|
204
207
|
post_install_message:
|
205
208
|
rdoc_options:
|
209
|
+
- --line-numbers
|
206
210
|
- --main
|
207
211
|
- README
|
208
212
|
- --all
|
209
213
|
- --charset
|
210
214
|
- UTF-8
|
211
215
|
- --title
|
212
|
-
- ruby-nuggets Application documentation (v0.8.
|
213
|
-
- --line-numbers
|
216
|
+
- ruby-nuggets Application documentation (v0.8.5)
|
214
217
|
require_paths:
|
215
218
|
- lib
|
216
219
|
required_ruby_version: !ruby/object:Gem::Requirement
|