strings-case 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strings
4
+ class Case
5
+ # A collection of acronyms
6
+ #
7
+ # @api private
8
+ class Acronyms
9
+ # Create instance from an array of acronyms
10
+ #
11
+ # @example
12
+ # acronyms = Strings::Case::Acronyms.new(%w[HTTP XML])
13
+ #
14
+ # @param [Array<String>, Strings::Case::Acronyms] acronyms
15
+ # the acronyms to add
16
+ #
17
+ # @return [Strings::Case::Acronyms]
18
+ #
19
+ # @api public
20
+ def self.from(acronyms = [])
21
+ return acronyms if acronyms.is_a?(self)
22
+
23
+ new(acronyms)
24
+ end
25
+
26
+ # Mappings of downcased string to an acronym
27
+ #
28
+ # @example
29
+ # acronyms.entries
30
+ # # => {"http" => "HTTP"}
31
+ #
32
+ # @return [Hash{String => String}]
33
+ #
34
+ # @api private
35
+ attr_reader :entries
36
+
37
+ # A pattern
38
+ #
39
+ # @example
40
+ # acronyms.pattern
41
+ #
42
+ # @return [Regexp]
43
+ #
44
+ # @api public
45
+ attr_reader :pattern
46
+
47
+ # Create an Acronyms instance
48
+ #
49
+ # @example
50
+ # acronyms = Strings::Case::Acronyms.new(%w[HTTP XML])
51
+ #
52
+ # @param [Array<String>] acronyms
53
+ # an array of acronyms
54
+ #
55
+ # @api public
56
+ def initialize(acronyms = [])
57
+ @entries = {}
58
+ @pattern = /(?!)/ # match nothing
59
+
60
+ acronyms.each { |acronym| add(acronym) }
61
+ end
62
+
63
+ # Add an acronym
64
+ #
65
+ # @example
66
+ # acronyms.add("HTTP")
67
+ #
68
+ # @example
69
+ # acronyms << "HTTP"
70
+ #
71
+ # @param [String] string
72
+ # the string name to add to the acronyms
73
+ #
74
+ # @return [void]
75
+ #
76
+ # @api public
77
+ def add(string)
78
+ @entries[string.downcase] = string
79
+ @pattern = /#{Regexp.union(to_a)}(?=\b|[^\p{Ll}])/
80
+ end
81
+ alias << add
82
+
83
+ # Find an acronym
84
+ #
85
+ # @example
86
+ # acronyms.fetch("http")
87
+ #
88
+ # @param [String] string
89
+ # the string to search for an acronym
90
+ #
91
+ # @return [String]
92
+ #
93
+ # @api public
94
+ def fetch(string)
95
+ @entries[string.downcase]
96
+ end
97
+
98
+ # Convert to an array of all acronyms
99
+ #
100
+ # @example
101
+ # acronyms.to_a
102
+ #
103
+ # @return [Array<String>]
104
+ #
105
+ # @api public
106
+ def to_a
107
+ @entries.values
108
+ end
109
+ end # Acronyms
110
+ end # Case
111
+ end # Strings
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "acronyms"
4
+
5
+ module Strings
6
+ class Case
7
+ # Responsible for storing acronyms configuration
8
+ #
9
+ # @api private
10
+ class Configuration
11
+ # All the registered acronyms
12
+ #
13
+ # @example
14
+ # configuration.acronyms
15
+ #
16
+ # @return [Strings::Case::Acronyms]
17
+ #
18
+ # @api public
19
+ attr_reader :acronyms
20
+
21
+ # Create a Configuration instance
22
+ #
23
+ # @example
24
+ # configuration = Strings::Case::Cofiguration.new
25
+ #
26
+ # @api public
27
+ def initialize
28
+ @acronyms = Acronyms.new
29
+ end
30
+
31
+ # Add an acronym
32
+ #
33
+ # @example
34
+ # strings = Strings::Case.new
35
+ # strings.configure do |config|
36
+ # config.acronym "HTTP"
37
+ # end
38
+ # strings.pascalcase("http_response") # => "HTTPResponse"
39
+ # strings.camelcase("http_response") # => "HTTPResponse"
40
+ # strings.snakecase("HTTPResponse") # => "http_response"
41
+ # strings.titlecase("http_response") # => "HTTP Response"
42
+ # strings.sentencecase("http_response") # => "HTTP response"
43
+ #
44
+ # @param [Array<String>] names
45
+ # the names to add to the acronyms list
46
+ #
47
+ # @return [void]
48
+ #
49
+ # @api public
50
+ def acronym(*names)
51
+ names.each { |name| @acronyms << name }
52
+ end
53
+ end # Configuration
54
+ end # Case
55
+ end # Strings
@@ -1,46 +1,96 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../case"
4
+
3
5
  module Strings
4
- module Case
6
+ class Case
7
+ # Responsible for refining the String class with case conversions
8
+ #
9
+ # @api public
5
10
  module Extensions
6
11
  refine String do
12
+ # Convert string to camel case
13
+ #
14
+ # @see Strings::Case#camelcase
15
+ #
16
+ # @api public
7
17
  def camelcase(*args, **options)
8
18
  Strings::Case.camelcase(self, *args, **options)
9
19
  end
10
- alias lower_camelcase camelcase
20
+ alias_method :lower_camelcase, :camelcase
11
21
 
22
+ # Convert string to constant case
23
+ #
24
+ # @see Strings::Case#constcase
25
+ #
26
+ # @api public
12
27
  def constcase(*args, **options)
13
28
  Strings::Case.constcase(self, *args, **options)
14
29
  end
15
- alias constantcase constcase
30
+ alias_method :constantcase, :constcase
16
31
 
32
+ # Convert string to an HTTP header
33
+ #
34
+ # @see Strings::Case#headercase
35
+ #
36
+ # @api public
17
37
  def headercase(*args, **options)
18
38
  Strings::Case.headercase(self, *args, **options)
19
39
  end
20
40
 
41
+ # Convert string to lowercase words linked by hyphens
42
+ #
43
+ # @see Strings::Case#kebabcase
44
+ #
45
+ # @api public
21
46
  def kebabcase(*args, **options)
22
47
  Strings::Case.kebabcase(self, *args, **options)
23
48
  end
24
- alias dashcase kebabcase
49
+ alias_method :dashcase, :kebabcase
25
50
 
51
+ # Convert string to pascal case
52
+ #
53
+ # @see Strings::Case#pascalcase
54
+ #
55
+ # @api public
26
56
  def pascalcase(*args, **options)
27
57
  Strings::Case.pascalcase(self, *args, **options)
28
58
  end
29
- alias upper_camelcase pascalcase
59
+ alias_method :upper_camelcase, :pascalcase
30
60
 
61
+ # Convert string to file path case
62
+ #
63
+ # @see Strings::Case#pathcase
64
+ #
65
+ # @api public
31
66
  def pathcase(*args, **options)
32
67
  Strings::Case.pathcase(self, *args, **options)
33
68
  end
34
69
 
70
+ # Convert string to sentence case
71
+ #
72
+ # @see Strings::Case#sentencecase
73
+ #
74
+ # @api public
35
75
  def sentencecase(*args, **options)
36
76
  Strings::Case.sentencecase(self, *args, **options)
37
77
  end
38
78
 
79
+ # Convert string to snake case
80
+ #
81
+ # @see Strings::Case#snakecase
82
+ #
83
+ # @api public
39
84
  def snakecase(*args, **options)
40
85
  Strings::Case.snakecase(self, *args, **options)
41
86
  end
42
- alias underscore snakecase
87
+ alias_method :underscore, :snakecase
43
88
 
89
+ # Convert string to title case
90
+ #
91
+ # @see Strings::Case#titlecase
92
+ #
93
+ # @api public
44
94
  def titlecase(*args, **options)
45
95
  Strings::Case.titlecase(self, *args, **options)
46
96
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strings
4
- module Case
5
- VERSION = "0.3.0"
6
- end
7
- end
4
+ class Case
5
+ VERSION = "0.4.0"
6
+ end # Case
7
+ end # Strings