simple_symbolize 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b0904a9e68211c27ef36b9f8e05fd2966963db4aff8c875fae76138ce7e08f4
4
- data.tar.gz: 92cf5435b255b07461f0d13bd122d3257f6f869bcf6b95d08d9b67b9805a96c1
3
+ metadata.gz: fcd4f78253aed92cf1895fd948418deac6fe69f2e3ce80efae55eb79556b157c
4
+ data.tar.gz: 7cefb49253c485e825b05e27bf6fcf020f4ef24f1995cc0ab1590e76a5ef0aed
5
5
  SHA512:
6
- metadata.gz: 7670670156db2d4b8b538c52479cb4fc63a2a4f1f2191dc4944849dfd40249450c014d8026984408a0c6cebc13fcd3e0f5a978855edc4f0b30237c0fb9dfb543
7
- data.tar.gz: f82b71d63638bbc6cd8b067bfdc955cb24ee39353746d283afab6591d1a3649e5091916bb3fe2caf92db21e11e5d5bc05ee17d2294194021c5a1eccada7f34c5
6
+ metadata.gz: fee566211bb3708c7f3225897008f9f5434fbaa8840f3fcab3b50d7031e74263a4b14a39fbf383834bbd3948cb4020457e00c6d83777f3235fa7d3d77877f312
7
+ data.tar.gz: a453a5a9be5d81ea8198f21b5fbd0fc1663f71f58f6051a9aa3ebc7c87893d76413d7b1c38b3e184032a7b773ed9ecabe92374f88fb467853ab986a55d17e57b
@@ -18,7 +18,7 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- ruby-version: ['2.7', '3.0', '3.1']
21
+ ruby-version: [ '3.0', '3.1', '3.2' ]
22
22
 
23
23
  steps:
24
24
  - uses: actions/checkout@v2
@@ -9,7 +9,7 @@ jobs:
9
9
  runs-on: ubuntu-latest
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: [ '2.7', '3.0', '3.1' ]
12
+ ruby-version: [ '3.0', '3.1', '3.2' ]
13
13
 
14
14
  steps:
15
15
  - uses: actions/checkout@v2
data/.rubocop.yml CHANGED
@@ -1,5 +1,30 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
1
  AllCops:
4
2
  NewCops: enable
5
- SuggestExtensions: false
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 3.0
5
+
6
+ Gemspec/DevelopmentDependencies:
7
+ EnforcedStyle: gemspec
8
+
9
+ Gemspec/RequireMFA:
10
+ Enabled: false
11
+
12
+ Metrics/BlockLength:
13
+ Max: 120
14
+ Exclude:
15
+ - spec/**/*.rb
16
+
17
+ Style/MixinUsage:
18
+ Enabled: false
19
+
20
+ Lint/BooleanSymbol:
21
+ Enabled: false
22
+
23
+ Metrics/AbcSize:
24
+ Enabled: false
25
+
26
+ Metrics/MethodLength:
27
+ Max: 25
28
+
29
+ Layout/LineLength:
30
+ Max: 130
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in simple_symbolize.gemspec
data/README.md CHANGED
@@ -64,40 +64,59 @@ No sweat, you can configure this gem to underscore and remove to your hearts con
64
64
 
65
65
  ```ruby
66
66
  SimpleSymbolize.translate do |trans|
67
- trans.to_underscore('!')
68
- trans.to_remove(' ')
69
- trans.to_omit('@')
67
+ trans.to_underscore = '!'
68
+ trans.to_remove = ' '
69
+ trans.to_omit = '@'
70
70
  end
71
71
  ```
72
72
 
73
73
  ## Updates!
74
74
 
75
- V1.1: SimpleSymbolize has got new friends!
75
+ ### V3
76
+ #### String to_snake_case
77
+
78
+ `#to_snake_case` extends the String class to return you your String object in snake_case format.
79
+
80
+ #### Handle camelCase with Symbolize
81
+
82
+ ```ruby
83
+ symbolize('helloWorld!') # => :hello_world
84
+ ```
85
+
86
+ This is the default behaviour and can be switched off by setting `#handle_camel_case` to `false`
87
+
88
+ ```ruby
89
+ SimpleSymbolize.translate { |trans| trans.handle_camel_case = false }
90
+ ```
91
+
92
+ #### Additional ways to configure SimpleSymbolize
93
+
94
+ Arrays are now supported when configuring the gem
95
+
96
+ ```ruby
97
+ SimpleSymbolize.translate { |trans| trans.to_underscore = %w[!&*] }
98
+ ```
99
+
100
+ ### V2
101
+
102
+ SimpleSymbolize has got new friends!
76
103
 
77
104
  Introducing `elementize` and `camelize`.
78
105
 
79
- ### Elementize
106
+ #### Elementize
80
107
 
81
108
  Sometimes you just want a simple String obj without all the fuss. Elementize takes your String obj, removes that fuss
82
109
  and returns you a simple-to-use String.
83
110
 
84
- #### Example
85
-
86
111
  ```ruby
87
112
  elementize('hello world!') # => "hello_world"
88
113
  ```
89
114
 
90
- ### Camelize
115
+ #### Camelize
91
116
 
92
117
  Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
93
118
  a Symbolized object in camelCase.
94
119
 
95
- #### Example
96
-
97
- ```ruby
98
- camelize('hello world!') # => :helloWorld
99
- ```
100
-
101
120
  [comment]: <> (## Contributing)
102
121
 
103
122
  [comment]: <> (Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_symbolize.)
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'simple_symbolize'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simple_symbolize'
2
4
 
3
5
  # Extends the String class by mixing in the symbolize module.
@@ -7,4 +9,14 @@ class String
7
9
  def symbolize
8
10
  SimpleSymbolize.symbolize(self)
9
11
  end
12
+
13
+ # @example Turns a String into it's snake_case equivalent
14
+ # "helloWorld".to_snake_case => 'hello_word'
15
+ def to_snake_case
16
+ # rubocop:disable Style/RedundantSelf
17
+ self.gsub(/([a-z\d])([A-Z])/, '\1_\2')
18
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
19
+ .downcase
20
+ # rubocop:enable Style/RedundantSelf
21
+ end
10
22
  end
@@ -1,68 +1,93 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SimpleSymbolize
2
4
  # The translations class holds the attributes used to transform a String object.
3
5
  # It also provides helper methods to manipulate those attributes.
4
6
  class Translations
5
7
  # @return [Array] the characters to be transformed into underscores.
6
- attr_accessor :underscore
8
+ attr_reader :underscore
7
9
  # @return [Array] the characters to be removed from the String.
8
- attr_accessor :remove
10
+ attr_reader :remove
9
11
  # @return [Array] the characters to be untouched from the String.
10
- attr_accessor :omit
12
+ attr_reader :omit
13
+
14
+ attr_reader :handle_camel_case
11
15
 
12
16
  # Creates an instance of the Translations class.
13
17
  #
14
18
  # Sets the class variables to a default state.
15
19
  def initialize
16
- @underscore = [' ']
17
- @remove = %w[\' ( ) , . : "]
18
- @omit = []
20
+ reset!
19
21
  end
20
22
 
21
23
  # Merges the String passed with the @underscore Array omitting duplicates.
22
- # Removes those characters from the @remove ans @omit Arrays to avoid the change being over-written.
24
+ # Removes those characters from the @remove and @omit Arrays to avoid the change being over-written.
23
25
  #
24
- # @param t [String] a String object containing characters to be underscored.
26
+ # @param chars [Array] an object containing characters to be underscored.
25
27
  #
26
28
  # @return [Array] the Array of characters to be underscored.
27
- #
28
- # @raise [ArgumentError] if the param does not respond to #to_s.
29
- def to_underscore(t)
30
- raise ArgumentError 'needs to be a String or respond to #to_s' unless t.respond_to?(:to_s)
29
+ def to_underscore=(chars)
30
+ chars = sanitise_chars(chars)
31
31
 
32
- @remove -= t.to_s.chars
33
- @omit -= t.to_s.chars
34
- @underscore |= t.to_s.chars
32
+ @remove -= chars
33
+ @omit -= chars
34
+ @underscore |= chars
35
35
  end
36
36
 
37
37
  # Merges the String passed with the @remove Array omitting duplicates.
38
38
  # Removes those characters from the @underscore and @omit Arrays to avoid the change being over-written.
39
39
  #
40
- # @param t [String] a String object containing characters to be removed.
40
+ # @param chars [String] a String object containing characters to be removed.
41
41
  #
42
42
  # @return [Array] the Array of characters to be removed.
43
- #
44
- # @raise [ArgumentError] if the param does not respond to #to_s.
45
- def to_remove(t)
46
- raise ArgumentError 'needs to be a String or respond to #to_s' unless t.respond_to?(:to_s)
43
+ def to_remove=(chars)
44
+ chars = sanitise_chars(chars)
47
45
 
48
- @underscore -= t.to_s.chars
49
- @omit -= t.to_s.chars
50
- @remove |= t.to_s.chars
46
+ @underscore -= chars
47
+ @omit -= chars
48
+ @remove |= chars
51
49
  end
52
50
 
53
51
  # Removes characters within the String passed from the @remove and @underscore Arrays.
54
52
  #
55
- # @param t [String] a String object containing characters to be removed.
53
+ # @param chars [String] a String object containing characters to be removed.
56
54
  #
57
55
  # @return [Array] the Array of characters to be omitted.
56
+ def to_omit=(chars)
57
+ chars = sanitise_chars(chars)
58
+
59
+ @underscore -= chars
60
+ @remove -= chars
61
+ @omit += chars
62
+ end
63
+
64
+ def handle_camel_case=(handle)
65
+ handle = handle.to_s.downcase
66
+ raise ArgumentError 'needs to be either `true` or `false`' unless %w[true false].include?(handle)
67
+
68
+ @handle_camel_case = handle.eql?('true')
69
+ end
70
+
71
+ def reset!
72
+ @underscore = [' ', '::', '-']
73
+ @remove = %w[' ( ) , . : " ! @ £ $ % ^ & * / { } [ ] < > ; = #]
74
+ @omit = []
75
+ @handle_camel_case = true
76
+ end
77
+
78
+ private
79
+
80
+ # Converts chars into Array of Strings
81
+ #
82
+ # @param arg Arg to be converted
83
+ #
84
+ # @return Array of Strings
58
85
  #
59
- # @raise [ArgumentError] if the param does not respond to #to_s.
60
- def to_omit(t)
61
- raise ArgumentError 'needs to be a String or respond to #to_s' unless t.respond_to?(:to_s)
86
+ # @raise [ArgumentError] if the arg is not either a String or an Array.
87
+ def sanitise_chars(arg)
88
+ raise ArgumentError 'needs to be a String or an Array of characters' unless [String, Array].include?(arg.class)
62
89
 
63
- @underscore -= t.to_s.chars
64
- @remove -= t.to_s.chars
65
- @omit += t.to_s.chars
90
+ arg.respond_to?(:chars) ? arg.chars : arg.map(&:to_s)
66
91
  end
67
92
  end
68
93
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SimpleSymbolize
2
- VERSION = '2.0.2'.freeze
4
+ VERSION = '3.0.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simple_symbolize/version'
2
4
 
3
5
  require_relative 'simple_symbolize/string'
@@ -5,6 +7,8 @@ require_relative 'simple_symbolize/translations'
5
7
 
6
8
  include SimpleSymbolize
7
9
 
10
+ # Main module for the gem
11
+ # Contains the base methods and allows configuration
8
12
  module SimpleSymbolize
9
13
  class Error < StandardError; end
10
14
 
@@ -18,46 +22,55 @@ module SimpleSymbolize
18
22
  # Configures the Symbolize environment.
19
23
  #
20
24
  # @yieldparam [Translations] config the translations object yielded to the block.
21
- def self.translate(&block)
22
- yield translations
25
+ def self.translate
26
+ yield(translations) if block_given?
23
27
  end
24
28
 
25
29
  # Symbolizes a String object.
26
30
  #
27
- # @param str [String] the String object to be symbolized.
31
+ # @param obj [Object] the String object to be symbolized.
28
32
  #
29
33
  # @example Symbolize a string using the symbolize method
30
34
  # symbolize("hello world!") #=> :hello_world
31
- def symbolize(str)
32
- return str if str.is_a?(Symbol) || str.nil?
35
+ def symbolize(obj)
36
+ return obj unless obj.respond_to?(:to_s)
37
+ return obj if [Hash, Array, NilClass].include?(obj.class)
38
+
39
+ str = obj.to_s
40
+ str = str.to_snake_case if SimpleSymbolize.translations.handle_camel_case
33
41
 
34
- str.to_s.downcase.tr(SimpleSymbolize.translations.underscore.join, '_')
35
- &.tr(SimpleSymbolize.translations.remove.join, '')&.to_sym
42
+ str.downcase
43
+ .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
44
+ .gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
45
+ .to_sym
36
46
  end
37
47
 
38
48
  # Symbolizes a String object and returns it as a String object.
39
49
  #
40
- # @param str [String] the String object to be symbolized.
50
+ # @param obj [Object] the object to be symbolized.
41
51
  #
42
52
  # @example Elementize a string using the elementize method
43
53
  # elementize("hello world!") #=> "helloWorld"
44
- def elementize(str)
45
- return str unless str.is_a?(Symbol) || str.is_a?(String)
54
+ def elementize(obj)
55
+ return obj unless obj.respond_to?(:to_s)
56
+ return obj if [Hash, Array, NilClass].include?(obj.class)
46
57
 
47
- symbolize(str).to_s
58
+ symbolize(obj).to_s
48
59
  end
49
60
 
50
61
  # Turns a String object into a camelCase Symbol.
51
62
  #
52
- # @param str [String] the String object to be camelized.
63
+ # @param obj [Object] the String object to be camelized.
53
64
  #
54
65
  # @example Camelize a string using the camelize method
55
66
  # camelize("hello world!") #=> :helloWorld
56
- def camelize(str)
57
- return str unless str.is_a?(String) || str.is_a?(Symbol)
58
- return symbolize(str) if str.is_a?(String) && (str.split(/[_ ]/).size <= 1)
67
+ def camelize(obj)
68
+ return obj unless obj.respond_to?(:to_s)
69
+ return obj if [Hash, Array, NilClass].include?(obj.class)
70
+
71
+ first, *rest = elementize(obj).split('_')
72
+ return obj if first.nil?
59
73
 
60
- first, *rest = elementize(str).split('_')
61
- rest ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
74
+ rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
62
75
  end
63
76
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/simple_symbolize/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
@@ -14,7 +16,7 @@ Gem::Specification.new do |spec|
14
16
  spec.homepage = 'https://github.com/dvla/simple-symbolize'
15
17
 
16
18
  spec.license = 'MIT'
17
- spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
19
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
18
20
 
19
21
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
22
 
@@ -32,7 +34,7 @@ Gem::Specification.new do |spec|
32
34
  spec.require_paths = ['lib']
33
35
 
34
36
  spec.add_development_dependency 'pry', '~> 0.14'
35
- spec.add_development_dependency 'rake', '~> 12.0'
36
- spec.add_development_dependency 'rspec', '~> 3.2'
37
- spec.add_development_dependency 'rubocop', '~> 1.29.0'
37
+ spec.add_development_dependency 'rake', '~> 13.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.12'
39
+ spec.add_development_dependency 'rubocop', '~> 1.54'
38
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_symbolize
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-28 00:00:00.000000000 Z
11
+ date: 2023-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -30,42 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '12.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '12.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.2'
47
+ version: '3.12'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.2'
54
+ version: '3.12'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.29.0
61
+ version: '1.54'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 1.29.0
68
+ version: '1.54'
69
69
  description: 'simple_symbolize will remove special characters from a String, replacing
70
70
  whitespace with an underscore, down-casing and finally calling the #to_sym String
71
71
  method. Configure this gem to your hearts content!'
@@ -80,7 +80,7 @@ files:
80
80
  - ".gitignore"
81
81
  - ".rspec"
82
82
  - ".rubocop.yml"
83
- - ".rubocop_todo.yml"
83
+ - ".ruby-version"
84
84
  - Gemfile
85
85
  - LICENSE.txt
86
86
  - README.md
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- version: 2.7.0
109
+ version: '3.0'
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
112
  - - ">="
data/.rubocop_todo.yml DELETED
@@ -1,82 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2022-05-20 09:59:54 UTC using RuboCop version 1.29.1.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 1
10
- # This cop supports safe auto-correction (--auto-correct).
11
- # Configuration parameters: Include.
12
- # Include: **/*.gemspec
13
- Gemspec/RequireMFA:
14
- Exclude:
15
- - 'simple_symbolize.gemspec'
16
-
17
- # Offense count: 1
18
- # Configuration parameters: Include.
19
- # Include: **/*.gemspec
20
- Gemspec/RequiredRubyVersion:
21
- Exclude:
22
- - 'simple_symbolize.gemspec'
23
-
24
- # Offense count: 1
25
- # This cop supports safe auto-correction (--auto-correct).
26
- # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
27
- Lint/UnusedMethodArgument:
28
- Exclude:
29
- - 'lib/simple_symbolize.rb'
30
-
31
- # Offense count: 1
32
- # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
33
- # IgnoredMethods: refine
34
- Metrics/BlockLength:
35
- Max: 88
36
-
37
- # Offense count: 3
38
- # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
39
- # AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
40
- Naming/MethodParameterName:
41
- Exclude:
42
- - 'lib/simple_symbolize/translations.rb'
43
-
44
- # Offense count: 1
45
- # Configuration parameters: AllowedConstants.
46
- Style/Documentation:
47
- Exclude:
48
- - 'spec/**/*'
49
- - 'test/**/*'
50
- - 'lib/simple_symbolize.rb'
51
-
52
- # Offense count: 10
53
- # This cop supports safe auto-correction (--auto-correct).
54
- # Configuration parameters: EnforcedStyle.
55
- # SupportedStyles: always, always_true, never
56
- Style/FrozenStringLiteralComment:
57
- Exclude:
58
- - 'Gemfile'
59
- - 'Rakefile'
60
- - 'bin/console'
61
- - 'lib/simple_symbolize.rb'
62
- - 'lib/simple_symbolize/string.rb'
63
- - 'lib/simple_symbolize/translations.rb'
64
- - 'lib/simple_symbolize/version.rb'
65
- - 'simple_symbolize.gemspec'
66
- - 'spec/simple_symbolize_spec.rb'
67
- - 'spec/spec_helper.rb'
68
-
69
- # Offense count: 1
70
- Style/MixinUsage:
71
- Exclude:
72
- - 'lib/simple_symbolize.rb'
73
-
74
- # Offense count: 2
75
- Lint/BooleanSymbol:
76
- Exclude:
77
- - 'spec/simple_symbolize_spec.rb'
78
-
79
- # Offense count: 2
80
- Metrics/BlockLength:
81
- Exclude:
82
- - 'spec/simple_symbolize_spec.rb'