messages_dictionary 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/CODE_OF_CONDUCT.md +46 -0
- data/.github/CONTRIBUTING.md +14 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +24 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +13 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
- data/.github/dependabot.yml +11 -0
- data/.github/workflows/ci.yml +27 -0
- data/.gitignore +11 -3
- data/.rubocop.yml +46 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +66 -29
- data/LICENSE +1 -1
- data/README.md +20 -14
- data/Rakefile +39 -2
- data/lib/messages_dictionary/injector.rb +72 -27
- data/lib/messages_dictionary/utils/dict.rb +9 -4
- data/lib/messages_dictionary/utils/string_utils.rb +21 -0
- data/lib/messages_dictionary/version.rb +4 -2
- data/lib/messages_dictionary.rb +20 -4
- data/messages_dictionary.gemspec +28 -17
- data/spec/lib/messages_dictionary/injector_spec.rb +168 -0
- data/spec/lib/messages_dictionary/utils/dict_spec.rb +10 -0
- data/spec/lib/messages_dictionary_spec.rb +20 -0
- data/spec/spec_helper.rb +15 -6
- data/spec/support/spec_files_setup.rb +10 -5
- data/spec/support/spec_utils.rb +16 -20
- metadata +114 -23
- data/.travis.yml +0 -12
- data/lib/messages_dictionary/utils/snake_case.rb +0 -17
- data/spec/injector_spec.rb +0 -146
- data/spec/support/test_class.rb +0 -4
- data/spec/utils/dict_spec.rb +0 -8
- data/spec/utils/snake_case_spec.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 270aca02bdc13e851f345d7ce783fbd8329e95f2054c3865fa5b5c1cc2b09c6c
|
4
|
+
data.tar.gz: be5c5615e7c87d7423019fcd96afdf6695ea8527b7c63777c550ca8f6c4fdde7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9661d7b64b05ee3d68042938cf05f56b4606107104122e087b39ee86c0b45db79e6ec304e89785d53c1565e7f18a200e542f3ef0cff81acf94afd1cae31aa222
|
7
|
+
data.tar.gz: e401f904c1e3efa0215a0551a33f4dced1a915f7173705232aee7f660c21f04b0f4f6853b2b280a167183d9e8c2960d6287ad0f156f7f54e967f85eba88d2781
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
## Our Standards
|
8
|
+
|
9
|
+
Examples of behavior that contributes to creating a positive environment include:
|
10
|
+
|
11
|
+
* Using welcoming and inclusive language
|
12
|
+
* Being respectful of differing viewpoints and experiences
|
13
|
+
* Gracefully accepting constructive criticism
|
14
|
+
* Focusing on what is best for the community
|
15
|
+
* Showing empathy towards other community members
|
16
|
+
|
17
|
+
Examples of unacceptable behavior by participants include:
|
18
|
+
|
19
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
20
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
21
|
+
* Public or private harassment
|
22
|
+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
23
|
+
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
24
|
+
|
25
|
+
## Our Responsibilities
|
26
|
+
|
27
|
+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
28
|
+
|
29
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
30
|
+
|
31
|
+
## Scope
|
32
|
+
|
33
|
+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
34
|
+
|
35
|
+
## Enforcement
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at golosizpru@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
38
|
+
|
39
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
40
|
+
|
41
|
+
## Attribution
|
42
|
+
|
43
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
44
|
+
|
45
|
+
[homepage]: http://contributor-covenant.org
|
46
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
1. [Fork the repository.][fork]
|
4
|
+
2. [Create a topic branch.][branch]
|
5
|
+
3. Implement your feature or bug fix.
|
6
|
+
4. Don't forget to add specs and make sure they pass by running `rspec .`.
|
7
|
+
5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you. Run `rubocop -A` to make it more aggressive.
|
8
|
+
6. If necessary, add documentation for your feature or bug fix.
|
9
|
+
7. Commit and push your changes.
|
10
|
+
8. [Submit a pull request.][pr]
|
11
|
+
|
12
|
+
[fork]: http://help.github.com/fork-a-repo/
|
13
|
+
[branch]: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-branches
|
14
|
+
[pr]: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
title: ''
|
5
|
+
labels: bug
|
6
|
+
assignees: bodrovis
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**Describe the bug**
|
11
|
+
A clear and concise description of what the bug is.
|
12
|
+
|
13
|
+
**To Reproduce**
|
14
|
+
Steps to reproduce the behavior.
|
15
|
+
|
16
|
+
**Expected behavior**
|
17
|
+
A clear and concise description of what you expected to happen.
|
18
|
+
|
19
|
+
**Your environment:**
|
20
|
+
- OS
|
21
|
+
- Ruby version
|
22
|
+
|
23
|
+
**Additional context**
|
24
|
+
Add any other context about the problem here.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
title: ''
|
5
|
+
labels: enhancement
|
6
|
+
assignees: bodrovis
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**The feature I'd like is...**
|
11
|
+
|
12
|
+
**Additional context**
|
13
|
+
Add any other context or screenshots about the feature request here.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
### Summary
|
2
|
+
|
3
|
+
Provide a general description of the code changes in your pull
|
4
|
+
request. Were there any bugs you had fixed? If so, mention them. If
|
5
|
+
these bugs have open GitHub issues, be sure to tag them here as well,
|
6
|
+
to keep the conversation linked together.
|
7
|
+
|
8
|
+
### Other Information
|
9
|
+
|
10
|
+
If there's anything else that's important and relevant to your pull
|
11
|
+
request, mention that information here.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
3
|
+
# Please see the documentation for all configuration options:
|
4
|
+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
5
|
+
|
6
|
+
version: 2
|
7
|
+
updates:
|
8
|
+
- package-ecosystem: "bundler" # See documentation for possible values
|
9
|
+
directory: "/" # Location of package manifests
|
10
|
+
schedule:
|
11
|
+
interval: "monthly"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
|
6
|
+
push:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
strategy:
|
12
|
+
fail-fast: false
|
13
|
+
matrix:
|
14
|
+
os: [ubuntu-latest]
|
15
|
+
ruby:
|
16
|
+
- 2.7
|
17
|
+
- '3.0'
|
18
|
+
- 3.1
|
19
|
+
runs-on: ${{ matrix.os }}
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v3
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
bundler-cache: true
|
27
|
+
- run: bundle exec rake rubospec
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
- rubocop-rake
|
5
|
+
|
6
|
+
AllCops:
|
7
|
+
TargetRubyVersion: 2.7
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Layout/SpaceInsideHashLiteralBraces:
|
11
|
+
EnforcedStyle: no_space
|
12
|
+
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Max: 36
|
15
|
+
Exclude:
|
16
|
+
- spec/**/*.rb
|
17
|
+
- rating-chgk-v2.gemspec
|
18
|
+
|
19
|
+
Metrics/BlockNesting:
|
20
|
+
Max: 2
|
21
|
+
|
22
|
+
Metrics/MethodLength:
|
23
|
+
CountComments: false
|
24
|
+
Max: 10
|
25
|
+
|
26
|
+
Style/CollectionMethods:
|
27
|
+
Enabled: true
|
28
|
+
PreferredMethods:
|
29
|
+
collect: 'map'
|
30
|
+
collect!: 'map!'
|
31
|
+
inject: 'reduce'
|
32
|
+
find: 'detect'
|
33
|
+
find_all: 'select'
|
34
|
+
delete: 'gsub'
|
35
|
+
|
36
|
+
Layout/DotPosition:
|
37
|
+
EnforcedStyle: trailing
|
38
|
+
|
39
|
+
RSpec/MultipleExpectations:
|
40
|
+
Max: 10
|
41
|
+
|
42
|
+
RSpec/ExampleLength:
|
43
|
+
Max: 35
|
44
|
+
|
45
|
+
RSpec/MessageSpies:
|
46
|
+
EnforcedStyle: have_received
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 2.0.0
|
4
|
+
|
5
|
+
This is a major re-write of the gem. All core features stay the same and there should not be any breaking changes, except for one thing: you should not use "destructive" methods when transforming your messages.
|
6
|
+
|
7
|
+
Previously you could say:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def greet
|
11
|
+
pou(:welcome) do |msg|
|
12
|
+
puts msg.upcase!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
Now it's recommended to use "safe" `upcase` method:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
def greet
|
21
|
+
pou(:welcome) do |msg|
|
22
|
+
puts msg.upcase
|
23
|
+
end
|
24
|
+
end
|
25
|
+
```
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,46 +1,83 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
messages_dictionary (
|
5
|
-
hashie (~>
|
4
|
+
messages_dictionary (2.0.0)
|
5
|
+
hashie (~> 5.0)
|
6
|
+
zeitwerk (~> 2.4)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
9
10
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
ast (2.4.2)
|
12
|
+
codecov (0.6.0)
|
13
|
+
simplecov (>= 0.15, < 0.22)
|
14
|
+
diff-lcs (1.5.0)
|
15
|
+
docile (1.4.0)
|
16
|
+
hashie (5.0.0)
|
17
|
+
json (2.6.2)
|
18
|
+
parallel (1.22.1)
|
19
|
+
parser (3.1.2.1)
|
20
|
+
ast (~> 2.4.1)
|
21
|
+
rainbow (3.1.1)
|
22
|
+
rake (13.0.6)
|
23
|
+
regexp_parser (2.6.1)
|
24
|
+
rexml (3.2.5)
|
25
|
+
rspec (3.12.0)
|
26
|
+
rspec-core (~> 3.12.0)
|
27
|
+
rspec-expectations (~> 3.12.0)
|
28
|
+
rspec-mocks (~> 3.12.0)
|
29
|
+
rspec-core (3.12.0)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-expectations (3.12.0)
|
24
32
|
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
-
rspec-support (~> 3.
|
26
|
-
rspec-mocks (3.
|
33
|
+
rspec-support (~> 3.12.0)
|
34
|
+
rspec-mocks (3.12.0)
|
27
35
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
-
rspec-support (~> 3.
|
29
|
-
rspec-support (3.
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
rspec-support (~> 3.12.0)
|
37
|
+
rspec-support (3.12.0)
|
38
|
+
rubocop (1.39.0)
|
39
|
+
json (~> 2.3)
|
40
|
+
parallel (~> 1.10)
|
41
|
+
parser (>= 3.1.2.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
regexp_parser (>= 1.8, < 3.0)
|
44
|
+
rexml (>= 3.2.5, < 4.0)
|
45
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
46
|
+
ruby-progressbar (~> 1.7)
|
47
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
48
|
+
rubocop-ast (1.23.0)
|
49
|
+
parser (>= 3.1.1.0)
|
50
|
+
rubocop-performance (1.15.1)
|
51
|
+
rubocop (>= 1.7.0, < 2.0)
|
52
|
+
rubocop-ast (>= 0.4.0)
|
53
|
+
rubocop-rake (0.6.0)
|
54
|
+
rubocop (~> 1.0)
|
55
|
+
rubocop-rspec (2.15.0)
|
56
|
+
rubocop (~> 1.33)
|
57
|
+
ruby-progressbar (1.11.0)
|
58
|
+
simplecov (0.21.2)
|
59
|
+
docile (~> 1.1)
|
60
|
+
simplecov-html (~> 0.11)
|
61
|
+
simplecov_json_formatter (~> 0.1)
|
62
|
+
simplecov-html (0.12.3)
|
63
|
+
simplecov_json_formatter (0.1.4)
|
64
|
+
unicode-display_width (2.3.0)
|
65
|
+
zeitwerk (2.6.6)
|
35
66
|
|
36
67
|
PLATFORMS
|
37
|
-
|
68
|
+
x64-mingw-ucrt
|
69
|
+
x86_64-linux
|
38
70
|
|
39
71
|
DEPENDENCIES
|
40
|
-
|
72
|
+
codecov (~> 0.1)
|
41
73
|
messages_dictionary!
|
42
|
-
rake (~>
|
74
|
+
rake (~> 13.0)
|
43
75
|
rspec (~> 3.6)
|
76
|
+
rubocop (~> 1.6)
|
77
|
+
rubocop-performance (~> 1.5)
|
78
|
+
rubocop-rake (~> 0.6)
|
79
|
+
rubocop-rspec (~> 2.0)
|
80
|
+
simplecov (~> 0.16)
|
44
81
|
|
45
82
|
BUNDLED WITH
|
46
|
-
|
83
|
+
2.3.26
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# MessagesDictionary
|
2
|
+
|
2
3
|
[![Gem Version](https://badge.fury.io/rb/messages_dictionary.svg)](https://badge.fury.io/rb/messages_dictionary)
|
3
|
-
|
4
|
-
[![
|
5
|
-
|
4
|
+
![CI](https://github.com/bodrovis-learning/messages_dictionary/actions/workflows/ci.yml/badge.svg)
|
5
|
+
[![Test Coverage](https://codecov.io/gh/bodrovis-learning/messages_dictionary/graph/badge.svg)](https://codecov.io/gh/bodrovis-learning/messages_dictionary)
|
6
|
+
![Downloads total](https://img.shields.io/gem/dt/messages_dictionary)
|
6
7
|
|
7
8
|
This gem started as an educational project for my student. The idea behind this gem is to organize
|
8
9
|
various messages in a simple key-value format that can be fetched later. Messages support interpolation,
|
@@ -11,11 +12,13 @@ can be specified as well.
|
|
11
12
|
|
12
13
|
[Here is my article](https://www.sitepoint.com/learn-ruby-metaprogramming-for-great-good/) describing how this gem was actually written.
|
13
14
|
|
14
|
-
Install it
|
15
|
+
This gem requires Ruby 2.7+. Install it by running:
|
15
16
|
|
16
|
-
|
17
|
+
```
|
18
|
+
gem install messages_dictionary
|
19
|
+
```
|
17
20
|
|
18
|
-
|
21
|
+
Refer to the next sections to see it in action.
|
19
22
|
|
20
23
|
## Use Cases
|
21
24
|
|
@@ -213,8 +216,12 @@ class MyClass
|
|
213
216
|
|
214
217
|
def greet
|
215
218
|
pou(:welcome) do |msg|
|
216
|
-
msg.upcase
|
219
|
+
msg.upcase
|
217
220
|
end
|
221
|
+
|
222
|
+
# Or simply:
|
223
|
+
|
224
|
+
pou(:welcome, &:upcase)
|
218
225
|
end
|
219
226
|
end
|
220
227
|
|
@@ -228,7 +235,7 @@ option:
|
|
228
235
|
```ruby
|
229
236
|
class MyClass
|
230
237
|
include MessagesDictionary
|
231
|
-
has_messages_dictionary transform: ->(msg) {msg.upcase
|
238
|
+
has_messages_dictionary transform: ->(msg) {msg.upcase}
|
232
239
|
|
233
240
|
def greet
|
234
241
|
pou(:welcome)
|
@@ -248,16 +255,15 @@ it anywhere (for example, when raising a custom error - see use case [here](http
|
|
248
255
|
If you do want to output your message after transformation, you have to do it explicitly:
|
249
256
|
|
250
257
|
```ruby
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
puts msg # => Prints "WELCOME"
|
255
|
-
end
|
258
|
+
def greet
|
259
|
+
pou(:welcome) do |msg|
|
260
|
+
puts msg.upcase # => Prints "WELCOME"
|
256
261
|
end
|
262
|
+
end
|
257
263
|
```
|
258
264
|
|
259
265
|
## License
|
260
266
|
|
261
267
|
Licensed under the [MIT License](https://github.com/bodrovis-learning/messages_dictionary/blob/master/LICENSE).
|
262
268
|
|
263
|
-
Copyright (c)
|
269
|
+
Copyright (c) 2022 [Ilya Krukowski](http://bodrovis.tech)
|
data/Rakefile
CHANGED
@@ -1,6 +1,43 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/clean'
|
2
5
|
require 'rspec/core/rake_task'
|
6
|
+
require 'rubocop/rake_task'
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'bundler/setup'
|
10
|
+
Bundler::GemHelper.install_tasks
|
11
|
+
rescue LoadError
|
12
|
+
puts 'although not required, bundler is recommened for running the tests'
|
13
|
+
end
|
3
14
|
|
4
15
|
RSpec::Core::RakeTask.new(:spec)
|
5
16
|
|
6
|
-
|
17
|
+
RuboCop::RakeTask.new do |task|
|
18
|
+
task.requires << 'rubocop-performance'
|
19
|
+
task.requires << 'rubocop-rspec'
|
20
|
+
end
|
21
|
+
|
22
|
+
CLOBBER.include(FileList['./*.gem'])
|
23
|
+
|
24
|
+
namespace :messages_dictionary do
|
25
|
+
desc 'Updates RubyGems, installs dependencies'
|
26
|
+
task :install do
|
27
|
+
puts 'Running bundle install'
|
28
|
+
sh 'gem update --system'
|
29
|
+
sh 'bundle'
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Builds the gem'
|
33
|
+
task :build do
|
34
|
+
puts 'Building'
|
35
|
+
sh 'gem build messages_dictionary.gemspec'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task rubospec: %w[rubocop spec]
|
40
|
+
|
41
|
+
task full_build: %w[clobber messages_dictionary:install messages_dictionary:build]
|
42
|
+
|
43
|
+
task default: :full_build
|
@@ -1,38 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module MessagesDictionary
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
# Main module that injects all the necessary methods in the target class
|
5
|
+
module Injector
|
6
|
+
using MessagesDictionary::Utils::StringUtils
|
7
|
+
|
8
|
+
def self.included(klass)
|
9
|
+
klass.extend ClassMethods
|
10
|
+
klass.include InstanceMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
# Class methods to be defined in the target (where the module was included)
|
14
|
+
module ClassMethods
|
15
|
+
# rubocop:disable Naming/PredicateName
|
16
|
+
# This is the main method that equips the target class
|
17
|
+
# with all the necesary goodies
|
18
|
+
def has_messages_dictionary(opts = {})
|
19
|
+
# rubocop:enable Naming/PredicateName
|
20
|
+
messages = MessagesDictionary::Utils::Dict.new(
|
21
|
+
opts.fetch(:messages) { __from_file(opts) }
|
22
|
+
)
|
23
|
+
|
24
|
+
const_set(:DICTIONARY_CONF, {msgs: messages,
|
25
|
+
output: opts[:output] || $stdout,
|
26
|
+
method: opts[:method] || :puts,
|
27
|
+
transform: opts[:transform]})
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def __from_file(opts)
|
33
|
+
file = opts[:file] || "#{name.nil? ? 'unknown' : name.snakecase}.yml"
|
34
|
+
file = File.expand_path(file, opts[:dir]) if opts[:dir]
|
35
|
+
|
36
|
+
begin
|
37
|
+
YAML.load_file(file)
|
38
|
+
rescue Errno::ENOENT
|
39
|
+
abort "File #{file} does not exist..."
|
15
40
|
end
|
16
|
-
klass.const_set(:DICTIONARY_CONF, {msgs: messages.extend(Hashie::Extensions::DeepFetch),
|
17
|
-
output: opts[:output] || STDOUT,
|
18
|
-
method: opts[:method] || :puts,
|
19
|
-
transform: opts[:transform]})
|
20
41
|
end
|
42
|
+
end
|
21
43
|
|
22
|
-
|
23
|
-
|
44
|
+
# Instance methods to be defined in the target class
|
45
|
+
module InstanceMethods
|
46
|
+
# This method will output your messages, perform interpolation,
|
47
|
+
# and transformations
|
48
|
+
def pretty_output(key, values = {}, &block)
|
49
|
+
msg = self.class::DICTIONARY_CONF[:msgs].deep_fetch(*key.to_s.split('.')) do
|
24
50
|
raise KeyError, "#{key} cannot be found in the provided file..."
|
25
51
|
end
|
52
|
+
|
53
|
+
__process(
|
54
|
+
__replace(msg, values),
|
55
|
+
&block
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
private :pretty_output
|
60
|
+
alias pou pretty_output
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def __replace(msg, values)
|
26
65
|
values.each do |k, v|
|
27
|
-
msg.gsub!(Regexp.new(
|
66
|
+
msg.gsub!(Regexp.new("\\{\\{#{k}\\}\\}"), v.to_s)
|
67
|
+
end
|
68
|
+
|
69
|
+
msg
|
70
|
+
end
|
71
|
+
|
72
|
+
def __process(msg, &block)
|
73
|
+
transform = block || self.class::DICTIONARY_CONF[:transform]
|
74
|
+
|
75
|
+
if transform
|
76
|
+
transform.call(msg)
|
77
|
+
else
|
78
|
+
self.class::DICTIONARY_CONF[:output].send(self.class::DICTIONARY_CONF[:method].to_sym, msg)
|
28
79
|
end
|
29
|
-
transform = block || klass::DICTIONARY_CONF[:transform]
|
30
|
-
transform ?
|
31
|
-
transform.call(msg) :
|
32
|
-
klass::DICTIONARY_CONF[:output].send(klass::DICTIONARY_CONF[:method].to_sym, msg)
|
33
80
|
end
|
34
|
-
private :pretty_output
|
35
|
-
alias_method :pou, :pretty_output
|
36
81
|
end
|
37
82
|
end
|
38
|
-
end
|
83
|
+
end
|
@@ -1,6 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module MessagesDictionary
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
module Utils
|
5
|
+
class Dict < Hash
|
6
|
+
include Hashie::Extensions::MergeInitializer
|
7
|
+
include Hashie::Extensions::IndifferentAccess
|
8
|
+
include Hashie::Extensions::DeepFetch
|
9
|
+
end
|
5
10
|
end
|
6
|
-
end
|
11
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MessagesDictionary
|
4
|
+
module Utils
|
5
|
+
# Utility methods for strings
|
6
|
+
module StringUtils
|
7
|
+
refine String do
|
8
|
+
# Underscore a string such that camelcase, dashes and spaces are
|
9
|
+
# replaced by underscores.
|
10
|
+
def snakecase
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
13
|
+
tr('-', '_').
|
14
|
+
gsub(/\s/, '_').
|
15
|
+
gsub(/__+/, '_').
|
16
|
+
downcase
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|