magic_words 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/lib/magic_words.rb +11 -0
- data/lib/magic_words/dict.rb +20 -0
- data/lib/magic_words/letters.rb +30 -0
- data/lib/magic_words/numerals.rb +14 -0
- data/lib/magic_words/punctuation.rb +40 -0
- data/lib/magic_words/version.rb +3 -0
- data/lib/magic_words/whitespace.rb +7 -0
- data/magic_words.gemspec +25 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 75473fa3644dc20da4f989c704449cc84e4abb75175a833ef9bc528259016e2f
|
4
|
+
data.tar.gz: 11a83ddd2222390749f36a5d5f1792e9264776bdd75edf7cb6836c1dfb94a7a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6bf3f02b643f9be1251eb091e1737a50687b95112d440a8c9e8dc295e0c44305dfa6fdb427d6f8d98175d09e87fb58af6f6a2b8fc505d13be630f670bef414b
|
7
|
+
data.tar.gz: 114e4d57af83e087c3cd22e1cde727b6736303d377ae95e6f433454ec9c75bbd08ac9a15c346c17e5a05200c3fd9d6b1eea79b45edab2e87c4fd76e00bc9e2b7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
magic_words (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.4.4)
|
10
|
+
rspec (3.10.0)
|
11
|
+
rspec-core (~> 3.10.0)
|
12
|
+
rspec-expectations (~> 3.10.0)
|
13
|
+
rspec-mocks (~> 3.10.0)
|
14
|
+
rspec-core (3.10.0)
|
15
|
+
rspec-support (~> 3.10.0)
|
16
|
+
rspec-expectations (3.10.0)
|
17
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
18
|
+
rspec-support (~> 3.10.0)
|
19
|
+
rspec-mocks (3.10.0)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.10.0)
|
22
|
+
rspec-support (3.10.0)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
magic_words!
|
29
|
+
rspec (~> 3.0)
|
30
|
+
|
31
|
+
BUNDLED WITH
|
32
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Aidan Coyle
|
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,65 @@
|
|
1
|
+
# MagicWords
|
2
|
+
|
3
|
+
I often come across code that has been refactored by some well meaning engineers to as to avoid the usage of plain string literals. For example imagine some http request handling code.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
request.add_headers({"Content-Type" => "application/json"})
|
7
|
+
```
|
8
|
+
|
9
|
+
While this code may _appear_ simple and straightforward is is clearly in need of refactoring to avoid string literals.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# ideally in a separate far-away file like lib/mygem/http/consts.rb
|
13
|
+
CONTENT_TYPE = "Content-Type"
|
14
|
+
APPLICATION_JSON = "application/json"
|
15
|
+
|
16
|
+
# much easier to maintain
|
17
|
+
request.add_headers({CONTENT_TYPE => APPLICATION_JSON})
|
18
|
+
```
|
19
|
+
|
20
|
+
However a major issue many developers run into when refactoring is the need to maintain said `const.rb` file(s) in a sufficiently indirect location. The purpose of the `magic_words` gem is to make managing such constants simple, automated, and abstract.
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Using `magic_words` is simple. We have some predefined dictionaries of commonly used consts, and otherwise you can add new dictionaries either by array or by file. Lets take a look at an example in practice.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# Oh no, a "magic" string literal out in the wild!
|
28
|
+
puts "Thou shalt not create a constant whose value can't be anything else by definition."
|
29
|
+
|
30
|
+
require "magic_words"
|
31
|
+
require "active_support/all" # for String#titleize
|
32
|
+
|
33
|
+
# Load up the english dictionary
|
34
|
+
MagicWords::Dict.from_file("english", "/usr/share/dict/words")
|
35
|
+
|
36
|
+
# Much better
|
37
|
+
puts MagicWords::Dict::ENGLISH::THOU.titleize + MagicWords::Whitespace::SPACE +
|
38
|
+
MagicWords::Dict::ENGLISH::SHALT + MagicWords::Whitespace::SPACE +
|
39
|
+
MagicWords::Dict::ENGLISH::NOT + MagicWords::Whitespace::SPACE +
|
40
|
+
MagicWords::Dict::ENGLISH::CREATE + MagicWords::Whitespace::SPACE +
|
41
|
+
MagicWords::Dict::ENGLISH::A + MagicWords::Whitespace::SPACE +
|
42
|
+
MagicWords::Dict::ENGLISH::CONSTANT + MagicWords::Whitespace::SPACE +
|
43
|
+
MagicWords::Dict::ENGLISH::WHOSE + MagicWords::Whitespace::SPACE +
|
44
|
+
MagicWords::Dict::ENGLISH::VALUE + MagicWords::Whitespace::SPACE +
|
45
|
+
MagicWords::Dict::ENGLISH::CAN + MagicWords::Punctuation::SINGLE_QUOTE + MagicWords::Letters::T + MagicWords::Whitespace::SPACE +
|
46
|
+
MagicWords::Dict::ENGLISH::BE + MagicWords::Whitespace::SPACE +
|
47
|
+
MagicWords::Dict::ENGLISH::ANYTHING + MagicWords::Whitespace::SPACE +
|
48
|
+
MagicWords::Dict::ENGLISH::ELSE + MagicWords::Whitespace::SPACE +
|
49
|
+
MagicWords::Dict::ENGLISH::BY + MagicWords::Whitespace::SPACE +
|
50
|
+
MagicWords::Dict::ENGLISH::DEFINITION + MagicWords::Punctuation::PERIOD
|
51
|
+
```
|
52
|
+
|
53
|
+
Couldn't be easier to avoid those pesky string literals floating around your codebase!
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
If you're interested in helping expand the feature set of `magic_words` please let me know. I'm currently looking for help with the following.
|
58
|
+
|
59
|
+
* A tool to automatically refactor existing repos to use `magic_words`.
|
60
|
+
* Internationalization.
|
61
|
+
* Remedial education for developers who mindlessly apply overgeneralizations such as "avoid hard coded string literals".
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/magic_words.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module MagicWords
|
2
|
+
module Dict
|
3
|
+
def self.from_list(name, words)
|
4
|
+
const_set(
|
5
|
+
name.upcase,
|
6
|
+
Module.new do
|
7
|
+
words.map(&:strip).each do |word|
|
8
|
+
next unless /^[A-Za-z]*$/.match?(word)
|
9
|
+
|
10
|
+
const_set(word.upcase, word.downcase) unless const_defined?(word.upcase)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_file(name, filename)
|
17
|
+
from_list(name, File.readlines(filename))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MagicWords
|
2
|
+
module Letters
|
3
|
+
A = 'a'
|
4
|
+
B = 'b'
|
5
|
+
C = 'c'
|
6
|
+
D = 'd'
|
7
|
+
E = 'e'
|
8
|
+
F = 'f'
|
9
|
+
G = 'g'
|
10
|
+
H = 'h'
|
11
|
+
I = 'i'
|
12
|
+
J = 'j'
|
13
|
+
K = 'k'
|
14
|
+
L = 'l'
|
15
|
+
M = 'm'
|
16
|
+
N = 'n'
|
17
|
+
O = 'o'
|
18
|
+
P = 'p'
|
19
|
+
Q = 'q'
|
20
|
+
R = 'r'
|
21
|
+
S = 's'
|
22
|
+
T = 't'
|
23
|
+
U = 'u'
|
24
|
+
V = 'v'
|
25
|
+
W = 'w'
|
26
|
+
X = 'x'
|
27
|
+
Y = 'y'
|
28
|
+
Z = 'z'
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module MagicWords
|
2
|
+
module Punctuation
|
3
|
+
PERIOD = '.'
|
4
|
+
COMMA = ','
|
5
|
+
EXCLAMATION_POINT = '!'
|
6
|
+
QUESTION_MARK = '?'
|
7
|
+
COLON = ':'
|
8
|
+
SEMICOLON = ';'
|
9
|
+
|
10
|
+
TILDE = '~'
|
11
|
+
BACKTICK = '`'
|
12
|
+
AT_SIGN = '@'
|
13
|
+
OCTOTHORPE = '#'
|
14
|
+
DOLLAR_SIGN = '$'
|
15
|
+
PERCENT_SIGN ='%'
|
16
|
+
CARET = '^'
|
17
|
+
AMPERSAND = '&'
|
18
|
+
ASTERISK = '*'
|
19
|
+
DASH = '-'
|
20
|
+
UNDERSCORE = '_'
|
21
|
+
PLUS_SIGN = '+'
|
22
|
+
EQUALS_SIGN = '='
|
23
|
+
|
24
|
+
SINGLE_QUOTE = "'"
|
25
|
+
DOUBLE_QUOTE = '"'
|
26
|
+
|
27
|
+
OPEN_PARENTHESIS = '('
|
28
|
+
CLOSE_PARENTHESIS = ')'
|
29
|
+
OPEN_SQUARE_BRACKET = '['
|
30
|
+
CLOSE_SQUARE_BRACKET = ']'
|
31
|
+
OPEN_CURLY_BRACKET = '{'
|
32
|
+
CLOSE_CURLY_BRACKEt = '}'
|
33
|
+
OPEN_ANGLE_BRACKET = '<'
|
34
|
+
CLOSE_ANGLE_BRACKET = '>'
|
35
|
+
|
36
|
+
FORWARD_SLASH = '/'
|
37
|
+
BACK_SLASH = '\\'
|
38
|
+
VERTICAL_BAR = '|'
|
39
|
+
end
|
40
|
+
end
|
data/magic_words.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'lib/magic_words/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "magic_words"
|
5
|
+
spec.version = MagicWords::VERSION
|
6
|
+
spec.authors = ["Aidan Coyle"]
|
7
|
+
spec.email = ["packrat386@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{avoid those messy string literals}
|
10
|
+
spec.homepage = "https://github.com/packrat386/magic_words"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/packrat386/magic_words"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magic_words
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aidan Coyle
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- packrat386@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- lib/magic_words.rb
|
27
|
+
- lib/magic_words/dict.rb
|
28
|
+
- lib/magic_words/letters.rb
|
29
|
+
- lib/magic_words/numerals.rb
|
30
|
+
- lib/magic_words/punctuation.rb
|
31
|
+
- lib/magic_words/version.rb
|
32
|
+
- lib/magic_words/whitespace.rb
|
33
|
+
- magic_words.gemspec
|
34
|
+
homepage: https://github.com/packrat386/magic_words
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/packrat386/magic_words
|
39
|
+
source_code_uri: https://github.com/packrat386/magic_words
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.3.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.1.4
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: avoid those messy string literals
|
59
|
+
test_files: []
|