simple_symbolize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 00aee90791b358af5b1a734d538f40bdd1a200b59888ca23efd18b1ceb40593a
4
+ data.tar.gz: a7e7e1087d2becd6181bf87243e823ce361dd8b40b3b25d04265ba589b6b4637
5
+ SHA512:
6
+ metadata.gz: 8f44009fa310ff0c0ad1595b5ece9f99c64168f3ca4bf5c4d50bce8edd198507d6761fe577894891aab26fbedb30e2d78bbcfcc9405ac6334c45060325d1cd09
7
+ data.tar.gz: 4468a0ebbe074887e985416a9c8abda90d688290c62d3c3e0faef0bd8ccddf52851848e896e57ed5835d05d9146cfe15852df6c285b47ea1c59e444afe4f6a57
data/.drone.yml ADDED
@@ -0,0 +1,32 @@
1
+ ---
2
+ kind: pipeline
3
+ name: default
4
+
5
+ platform:
6
+ os: linux
7
+ arch: amd64
8
+
9
+ steps:
10
+ - name: unit tests
11
+ pull: if-not-exists
12
+ image: ecr.tooling.dvla.gov.uk/base-images/qe-ruby:2.7.0-1
13
+ commands:
14
+ - bundle --jobs 5 --quiet
15
+ - bundle exec rspec
16
+ when:
17
+ event:
18
+ - push
19
+
20
+ - name: build and deploy
21
+ pull: if-not-exists
22
+ image: ecr.tooling.dvla.gov.uk/base-images/qe-ruby:2.7.0-1
23
+ commands:
24
+ - apk add --update git unzip alpine-sdk jq --no-cache
25
+ - gem install bundler
26
+ - gem install nexus
27
+ - bundle --jobs 5 --quiet
28
+ - gem build simple_symbolize.gemspec
29
+ - gem nexus --url https://nexus.tooling.dvla.gov.uk/repository/gem-private/ simple_symbolize*gem
30
+ when:
31
+ branch:
32
+ - master
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
10
+ *.gem
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ SuggestExtensions: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,67 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2021-06-29 14:23:53 UTC using RuboCop version 1.18.0.
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
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
12
+ Lint/UnusedMethodArgument:
13
+ Exclude:
14
+ - 'lib/simple_symbolize.rb'
15
+
16
+ # Offense count: 1
17
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
18
+ # IgnoredMethods: refine
19
+ Metrics/BlockLength:
20
+ Max: 88
21
+
22
+ # Offense count: 3
23
+ # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
24
+ # AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
25
+ Naming/MethodParameterName:
26
+ Exclude:
27
+ - 'lib/simple_symbolize/translations.rb'
28
+
29
+ # Offense count: 1
30
+ # Configuration parameters: AllowedConstants.
31
+ Style/Documentation:
32
+ Exclude:
33
+ - 'spec/**/*'
34
+ - 'test/**/*'
35
+ - 'lib/simple_symbolize.rb'
36
+
37
+ # Offense count: 10
38
+ # Cop supports --auto-correct.
39
+ # Configuration parameters: EnforcedStyle.
40
+ # SupportedStyles: always, always_true, never
41
+ Style/FrozenStringLiteralComment:
42
+ Exclude:
43
+ - 'Gemfile'
44
+ - 'Rakefile'
45
+ - 'bin/console'
46
+ - 'lib/simple_symbolize.rb'
47
+ - 'lib/simple_symbolize/string.rb'
48
+ - 'lib/simple_symbolize/translations.rb'
49
+ - 'lib/simple_symbolize/version.rb'
50
+ - 'simple_symbolize.gemspec'
51
+ - 'spec/simple_symbolize_spec.rb'
52
+ - 'spec/spec_helper.rb'
53
+
54
+ Style/MixinUsage:
55
+ Exclude:
56
+ - 'lib/simple_symbolize.rb'
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_symbolize.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_symbolize (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.4.4)
11
+ parallel (1.20.1)
12
+ parser (3.0.1.1)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (12.3.3)
16
+ regexp_parser (2.1.1)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.2)
31
+ rubocop (1.18.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.7.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.7.0)
41
+ parser (>= 3.0.1.1)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.0.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ rake (~> 12.0)
50
+ rspec (~> 3.0)
51
+ rubocop (~> 1.18.0)
52
+ simple_symbolize!
53
+
54
+ BUNDLED WITH
55
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 DVLA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # SimpleSymbolize
2
+
3
+ SimpleSymbolize takes a string and transforms it into a symbol. Why? Because working with symbols in Ruby makes for a
4
+ good time.
5
+
6
+ Wait, doesn't String already have a `to_sym` method?
7
+
8
+ Correct! However, this gem takes it one step further by transforming special characters and whitespace to give you a
9
+ simple easy to work with Symbol.
10
+
11
+ It works by transforming special characters in a String like `'!'` into underscores and removing whitespace.
12
+
13
+ #### Example
14
+
15
+ ```ruby
16
+ # to_sym
17
+ 'hello world!'.to_sym # => :"hello world!"
18
+
19
+ # Symbolize gem
20
+ 'hello world!'.symbolize # => :hello_world
21
+ ```
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'simple_symbolize'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle install
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install simple_symbolize
38
+
39
+ ## Usage
40
+
41
+ There are two ways to symbolize your String.
42
+
43
+ ### Call the symbolize method and pass it your String
44
+
45
+ ```ruby
46
+ require 'simple_symbolize'
47
+
48
+ SimpleSymbolize.symbolize('hello world!') # => :hello_world
49
+ ```
50
+
51
+ ### Call the symbolize method on your String object
52
+
53
+ ```ruby
54
+ require 'simple_symbolize'
55
+
56
+ 'hello world!'.symbolize # => :hello_world
57
+ ```
58
+
59
+ ## Configuration
60
+
61
+ Something not underscored or removed? Or even something underscored/removed that you didn't want transformed?
62
+
63
+ No sweat, you can configure this gem to underscore and remove to your hearts content!
64
+
65
+ ```ruby
66
+ SimpleSymbolize.translate do |trans|
67
+ trans.to_underscore('!')
68
+ trans.to_remove(' ')
69
+ trans.to_omit('@')
70
+ end
71
+ ```
72
+
73
+ [comment]: <> (## Contributing)
74
+
75
+ [comment]: <> (Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_symbolize.)
76
+
77
+
78
+ [comment]: <> (## License)
79
+
80
+ [comment]: <> (The gem is available as open source under the terms of the [MIT License]&#40;https://opensource.org/licenses/MIT&#41;.)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'simple_symbolize'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ require 'simple_symbolize/version'
2
+
3
+ require_relative 'simple_symbolize/string'
4
+ require_relative 'simple_symbolize/translations'
5
+
6
+ include SimpleSymbolize
7
+
8
+ module SimpleSymbolize
9
+ class Error < StandardError; end
10
+
11
+ # Returns the translations object, initializing it if necessary.
12
+ #
13
+ # @return [Translations] the translations object.
14
+ def self.translations
15
+ @translations ||= Translations.new
16
+ end
17
+
18
+ # Configures the Symbolize environment.
19
+ #
20
+ # @yieldparam [Translations] config the translations object yielded to the block.
21
+ def self.translate(&block)
22
+ yield translations
23
+ end
24
+
25
+ # Symbolizes a String object.
26
+ #
27
+ # @param str [String] the String object to be symbolized.
28
+ #
29
+ # @example Symbolize a string using the symbolize method
30
+ # symbolize("hello world!") #=> :hello_world
31
+ def symbolize(str)
32
+ return str if str.is_a?(Symbol) || str.nil?
33
+
34
+ str.downcase.tr(SimpleSymbolize.translations.underscore.join, '_')
35
+ &.tr(SimpleSymbolize.translations.remove.join, '')&.to_sym
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ require 'simple_symbolize'
2
+
3
+ # Extends the String class by mixing in the symbolize module.
4
+ class String
5
+ # @example Symbolize a string using the String object method
6
+ # "hello world!".symbolize #=> :hello_world
7
+ def symbolize
8
+ SimpleSymbolize.symbolize(self)
9
+ end
10
+ end
@@ -0,0 +1,68 @@
1
+ module SimpleSymbolize
2
+ # The translations class holds the attributes used to transform a String object.
3
+ # It also provides helper methods to manipulate those attributes.
4
+ class Translations
5
+ # @return [Array] the characters to be transformed into underscores.
6
+ attr_accessor :underscore
7
+ # @return [Array] the characters to be removed from the String.
8
+ attr_accessor :remove
9
+ # @return [Array] the characters to be untouched from the String.
10
+ attr_accessor :omit
11
+
12
+ # Creates an instance of the Translations class.
13
+ #
14
+ # Sets the class variables to a default state.
15
+ def initialize
16
+ @underscore = [' ']
17
+ @remove = %w[\' ( ) , . : "]
18
+ @omit = []
19
+ end
20
+
21
+ # 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.
23
+ #
24
+ # @param t [String] a String object containing characters to be underscored.
25
+ #
26
+ # @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)
31
+
32
+ @remove -= t.to_s.chars
33
+ @omit -= t.to_s.chars
34
+ @underscore |= t.to_s.chars
35
+ end
36
+
37
+ # Merges the String passed with the @remove Array omitting duplicates.
38
+ # Removes those characters from the @underscore and @omit Arrays to avoid the change being over-written.
39
+ #
40
+ # @param t [String] a String object containing characters to be removed.
41
+ #
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)
47
+
48
+ @underscore -= t.to_s.chars
49
+ @omit -= t.to_s.chars
50
+ @remove |= t.to_s.chars
51
+ end
52
+
53
+ # Removes characters within the String passed from the @remove and @underscore Arrays.
54
+ #
55
+ # @param t [String] a String object containing characters to be removed.
56
+ #
57
+ # @return [Array] the Array of characters to be omitted.
58
+ #
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)
62
+
63
+ @underscore -= t.to_s.chars
64
+ @remove -= t.to_s.chars
65
+ @omit += t.to_s.chars
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleSymbolize
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'lib/simple_symbolize/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'simple_symbolize'
5
+ spec.version = SimpleSymbolize::VERSION
6
+ spec.authors = ['alexo']
7
+ spec.email = ['']
8
+
9
+ spec.summary = 'Turns Strings into Symbols.'
10
+ spec.description = 'simple_symbolize will remove special characters from a String, ' \
11
+ 'replacing whitespace with an underscore, ' \
12
+ 'down-casing and finally calling the #to_sym String method. ' \
13
+ 'Configure this gem to your hearts content!'
14
+ spec.homepage = 'https://github.com/dvla/simple-symbolize'
15
+
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
18
+
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = spec.homepage
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_development_dependency 'rspec', '~> 3.2'
35
+ spec.add_development_dependency 'rubocop', '~> 1.18.0'
36
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_symbolize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - alexo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.18.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.18.0
41
+ description: 'simple_symbolize will remove special characters from a String, replacing
42
+ whitespace with an underscore, down-casing and finally calling the #to_sym String
43
+ method. Configure this gem to your hearts content!'
44
+ email:
45
+ - ''
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".drone.yml"
51
+ - ".gitignore"
52
+ - ".rspec"
53
+ - ".rubocop.yml"
54
+ - ".rubocop_todo.yml"
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/console
61
+ - bin/setup
62
+ - lib/simple_symbolize.rb
63
+ - lib/simple_symbolize/string.rb
64
+ - lib/simple_symbolize/translations.rb
65
+ - lib/simple_symbolize/version.rb
66
+ - simple_symbolize.gemspec
67
+ homepage: https://github.com/dvla/simple-symbolize
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/dvla/simple-symbolize
72
+ source_code_uri: https://github.com/dvla/simple-symbolize
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.5.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.2.15
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Turns Strings into Symbols.
92
+ test_files: []