caseconverter 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,25 +1,77 @@
1
+ # CaseConverter - A module that converts to and from different casings.
2
+ #
3
+ # ==Class Methods
4
+ # For example:
5
+ # require 'caseconverter'
6
+ #
7
+ # CaseConverter.to_underscore_case('TestTheCasing')
8
+ # # returns 'test_the_casing'
9
+ #
10
+ # This module support conversion to UpperCamelCase, lowerCamelCase,
11
+ # and underscore_case.
12
+ #
13
+ # The string can also be tokenized. This is useful for creating a custom
14
+ # casing.
15
+ # For example:
16
+ # require 'caseconverter'
17
+ #
18
+ # ar = CaseConverter.tokenize('TestTheCasing')
19
+ # # ar = ['test', 'the', 'casing']
20
+ #
21
+ # ar.join('__')
22
+ # # returns 'test__the__casing'
23
+ # ar.join('+')
24
+ # # returns 'test+the+casing'
25
+ #
26
+ # ==Instance Methods
27
+ # This module can also be used to extend strings.
28
+ #
29
+ # For example:
30
+ # require 'caseconverter'
31
+ #
32
+ # s = 'TestTheCasing'
33
+ # s.extend(CaseConverter)
34
+ # s.to_underscore_case
35
+ # # returns 'test_the_casing'
36
+ #
37
+ # # Extending a symbol will cause an error.
38
+ # # Extend the corresponding string by using the to_s method to be safe.
39
+ # s = :TestTheCasing.to_s
40
+ # s.extend(CaseConverter)
41
+ # s.to_underscore_case
42
+ # # returns 'test_the_casing'
43
+ #
44
+ # This is applicable for all of the module methods.
45
+
1
46
  module CaseConverter
2
47
  class << self
3
48
  public
4
49
 
50
+ # Converts a string casing to UpperCamelCase.
5
51
  def to_upper_camel_case(str)
6
52
  tokens = tokenize(str)
7
53
  tokens.each { |t| t.capitalize! }
8
54
  tokens.join('')
9
55
  end
10
56
 
57
+ # Converts a string casing to lowerCamelCase.
11
58
  def to_lower_camel_case(str)
12
59
  tokens = tokenize(str)
13
60
  tokens.each_with_index { |t, i| t.capitalize! unless i == 0 }
14
61
  tokens.join('')
15
62
  end
16
63
 
64
+ # Converts a string casing to underscore_case.
17
65
  def to_underscore_case(str)
18
66
  tokens = tokenize(str)
19
67
  tokens.join('_')
20
68
  end
21
69
 
22
- def tokenize(str)
70
+ # Tokenizes a string into an array. Any non alphanumeric character,
71
+ # or capitalized character will start a new word. This method is
72
+ # useful for creating custom casing that are not supported in this module.
73
+ def tokenize(value)
74
+ str = value.to_s
23
75
  str.squeeze(' ')
24
76
  tokens = str.split(' ')
25
77
 
@@ -56,18 +108,22 @@ class << self
56
108
  end
57
109
  end
58
110
 
111
+ # Instance method of CaseConverter.to_upper_camel_case
59
112
  def to_upper_camel_case
60
113
  CaseConverter.to_upper_camel_case(self.to_s)
61
114
  end
62
115
 
63
- def to_lower_camel_case(str)
116
+ # Instance method of CaseConverter.to_lower_camel_case
117
+ def to_lower_camel_case
64
118
  CaseConverter.to_lower_camel_case(self.to_s)
65
119
  end
66
120
 
67
- def to_underscore_case(str)
121
+ # Instance method of CaseConverter.to_underscore_case
122
+ def to_underscore_case
68
123
  CaseConverter.to_underscore_case(self.to_s)
69
124
  end
70
125
 
126
+ # Instance method of CaseConverter.tokenize
71
127
  def tokenize
72
128
  CaseConverter.tokenize(self.to_s)
73
129
  end
@@ -6,46 +6,72 @@ class TestCaseConverter < Test::Unit::TestCase
6
6
  public
7
7
 
8
8
  def test_to_upper_camel_case
9
- strings_to_test.each do |s|
10
- assert_equal(
11
- 'OneTwoThreeFour',
12
- CaseConverter.to_upper_camel_case(s)
13
- )
14
- end
9
+ do_test_module_method('OneTwoThreeFour', :to_upper_camel_case)
10
+ end
11
+
12
+ def test_instance_to_upper_camel_case
13
+ do_test_instance_method('OneTwoThreeFour', :to_upper_camel_case)
15
14
  end
16
15
 
17
16
  def test_to_lower_camel_case
17
+ do_test_module_method('oneTwoThreeFour', :to_lower_camel_case)
18
+ end
19
+
20
+ def test_instance_to_lower_camel_case
21
+ do_test_instance_method('oneTwoThreeFour', :to_lower_camel_case)
22
+ end
23
+
24
+ def test_to_underscore_case
25
+ do_test_module_method('one_two_three_four', :to_underscore_case)
26
+ end
27
+
28
+ def test_instance_to_underscore_case
29
+ do_test_instance_method('one_two_three_four', :to_underscore_case)
30
+ end
31
+
32
+ def test_tokenize
33
+ strings_to_test.each do |s|
34
+ do_test_tokenize(CaseConverter.tokenize(s))
35
+ end
36
+ end
37
+
38
+ def test_instance_tokenize
39
+ strings_to_test.each do |s|
40
+ str = s.to_s
41
+ str.extend(CaseConverter)
42
+ do_test_tokenize(str.tokenize)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def do_test_module_method(expected_output, method_sym)
18
49
  strings_to_test.each do |s|
19
50
  assert_equal(
20
- 'oneTwoThreeFour',
21
- CaseConverter.to_lower_camel_case(s)
51
+ expected_output,
52
+ CaseConverter.send(method_sym, s)
22
53
  )
23
54
  end
24
55
  end
25
56
 
26
- def test_to_underscore_case
57
+ def do_test_instance_method(expected_output, method_sym)
27
58
  strings_to_test.each do |s|
59
+ str = s.to_s
60
+ str.extend(CaseConverter)
28
61
  assert_equal(
29
- 'one_two_three_four',
30
- CaseConverter.to_underscore_case(s)
62
+ expected_output,
63
+ str.send(method_sym)
31
64
  )
32
65
  end
33
66
  end
34
67
 
35
- def test_tokenize
36
- strings_to_test.each do |s|
37
- tokens = CaseConverter.tokenize(s)
38
- assert_equal('one', tokens[0])
39
- assert_equal('two', tokens[1])
40
- assert_equal('three', tokens[2])
41
- assert_equal('four', tokens[3])
42
- end
68
+ def do_test_tokenize(tokens)
69
+ assert_equal('one', tokens[0])
70
+ assert_equal('two', tokens[1])
71
+ assert_equal('three', tokens[2])
72
+ assert_equal('four', tokens[3])
43
73
  end
44
74
 
45
- private
46
-
47
- include(CaseConverter)
48
-
49
75
  def strings_to_test
50
76
  [
51
77
  'one two three four',
@@ -55,7 +81,10 @@ class TestCaseConverter < Test::Unit::TestCase
55
81
  'one two_threeFour',
56
82
  'one_TwoThree Four',
57
83
  'one,two, three, four',
58
- 'One-two_threeFour'
84
+ 'One-two_threeFour',
85
+ :one_two_three_four,
86
+ :oneTwoThreeFour,
87
+ :OneTwoThreeFour,
59
88
  ]
60
89
  end
61
90
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: caseconverter
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2005-09-06 00:00:00 -07:00
6
+ version: 0.1.1
7
+ date: 2005-09-09 00:00:00 -07:00
8
8
  summary: Operations that change the casing of a string.
9
9
  require_paths:
10
10
  - lib