messages_dictionary 1.0.0 → 2.1.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 +33 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +67 -29
- data/LICENSE +1 -1
- data/README.md +107 -23
- data/Rakefile +39 -2
- data/lib/messages_dictionary/config.rb +66 -0
- data/lib/messages_dictionary/injector.rb +66 -29
- 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 +29 -17
- data/spec/lib/messages_dictionary/injector_spec.rb +244 -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 +37 -6
- data/spec/support/spec_utils.rb +16 -20
- metadata +129 -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: 3a6f7d3a4a63be59119844b7d3139f10b82118a7a726cbfb59f078dc68edb916
|
4
|
+
data.tar.gz: 1884503b8aad4f2c452666833a7a48867c2c0fa55c74854e94ea0a5a6c0bed79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2979cbdfacfdfdd47985304f3d6535ce4fe121ca240807e1700a8f9a15aa54e4301e487eeddbcaf9f456f4814f0f51d881e026cdafd65a23ce96d3c539b29057
|
7
|
+
data.tar.gz: eb7c8163836c469604d4fb1af38c84fc9c4d3a450dba73cb429e018a741e55768b0b25fa4f6be2715157e9ec794daf31bd21505db0179f9fe84f23b75cd289d6
|
@@ -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,33 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 2.1.0 (22-Nov-2022)
|
4
|
+
|
5
|
+
* `pou` and `pretty_output` are now available inside class methods
|
6
|
+
* `DICTIONARY_CONF` now contains an instance of the `Config` class that takes care of all configuration options
|
7
|
+
* Added `lazy` option that enables lazy loading
|
8
|
+
* Added `on_key_missing` option which is set to `:raise` by default. You can pass a proc or a lambda to this option in order to provide a custom handler that fires when a given key cannot be found.
|
9
|
+
* Added `file_loader` option to handle custom file loading
|
10
|
+
|
11
|
+
## 2.0.0 (21-Nov-2022)
|
12
|
+
|
13
|
+
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.
|
14
|
+
|
15
|
+
Previously you could say:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
def greet
|
19
|
+
pou(:welcome) do |msg|
|
20
|
+
puts msg.upcase!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
Now it's recommended to use "safe" `upcase` method:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
def greet
|
29
|
+
pou(:welcome) do |msg|
|
30
|
+
puts msg.upcase
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,46 +1,84 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
messages_dictionary (1.0
|
5
|
-
hashie (~>
|
4
|
+
messages_dictionary (2.1.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)
|
73
|
+
json (~> 2)
|
41
74
|
messages_dictionary!
|
42
|
-
rake (~>
|
75
|
+
rake (~> 13.0)
|
43
76
|
rspec (~> 3.6)
|
77
|
+
rubocop (~> 1.6)
|
78
|
+
rubocop-performance (~> 1.5)
|
79
|
+
rubocop-rake (~> 0.6)
|
80
|
+
rubocop-rspec (~> 2.0)
|
81
|
+
simplecov (~> 0.16)
|
44
82
|
|
45
83
|
BUNDLED WITH
|
46
|
-
|
84
|
+
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
|
|
@@ -32,9 +35,7 @@ Another, a bit more complex, use case in the [lessons_indexer gem](https://githu
|
|
32
35
|
* [Other classes simply inherit from it](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer/indexer.rb#L2)
|
33
36
|
* [Messages are fetched easily](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer/indexer.rb#L45)
|
34
37
|
|
35
|
-
##
|
36
|
-
|
37
|
-
### Basic Example
|
38
|
+
## Basic usage
|
38
39
|
|
39
40
|
Suppose you have the following program:
|
40
41
|
|
@@ -83,7 +84,7 @@ class MyOtherClass
|
|
83
84
|
def greet
|
84
85
|
pretty_output(:welcome)
|
85
86
|
# Or simply
|
86
|
-
pou
|
87
|
+
pou :welcome
|
87
88
|
end
|
88
89
|
end
|
89
90
|
```
|
@@ -153,9 +154,9 @@ class MyClass
|
|
153
154
|
end
|
154
155
|
```
|
155
156
|
|
156
|
-
|
157
|
+
## Further Customization
|
157
158
|
|
158
|
-
|
159
|
+
### Specifying File Name and Directory
|
159
160
|
|
160
161
|
By default `messages_dictionary` will search for a *.yml* file named after your class (converted to snake case,
|
161
162
|
so for the `MyClass` the file should be named *my_class.yml*)
|
@@ -167,13 +168,29 @@ inside the same directory. However, this behavior can be easily changed with the
|
|
167
168
|
```ruby
|
168
169
|
class MyClass
|
169
170
|
include MessagesDictionary
|
170
|
-
has_messages_dictionary file: 'some_file.yml', dir: '
|
171
|
+
has_messages_dictionary file: 'some_file.yml', dir: 'my_docs'
|
171
172
|
end
|
172
173
|
```
|
173
174
|
|
174
175
|
Both of these options are not mandatory.
|
175
176
|
|
176
|
-
|
177
|
+
### Providing a custom file loader
|
178
|
+
|
179
|
+
By default the gem a messages file in YAML format. However, you might want to use a different format: for example, JSON. In this case you'll have to provide a custom loader:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
class MyClass
|
183
|
+
include MessagesDictionary
|
184
|
+
has_messages_dictionary file: 'test_file.json', dir: 'my_dir',
|
185
|
+
file_loader: ->(file_path) { JSON.parse(File.read(file_path)) }
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
The `:file_loader` option accepts a proc or a lambda that receives a path to your messages file as an argument. This lambda must return a hash object with keys and the corresponding values.
|
190
|
+
|
191
|
+
The default value for the `:file_loader` is `->(f) { YAML.load_file(f) }`.
|
192
|
+
|
193
|
+
### Specifying Messages Hash
|
177
194
|
|
178
195
|
Instead of loading messages from a file, you can pass hash to the `has_messages_dictionary` using `:messages` option:
|
179
196
|
|
@@ -186,7 +203,7 @@ end
|
|
186
203
|
|
187
204
|
Nesting and all other features are supported as well.
|
188
205
|
|
189
|
-
|
206
|
+
### Specifying Output and Display Method
|
190
207
|
|
191
208
|
By default all messages will be outputted to `STDOUT` using `puts` method, however this can be changed:
|
192
209
|
|
@@ -201,7 +218,32 @@ class MyClass
|
|
201
218
|
end
|
202
219
|
```
|
203
220
|
|
204
|
-
|
221
|
+
### "Lazy" mode
|
222
|
+
|
223
|
+
By default this gem will load all messages from the given file. However, you can enable a "lazy" mode so that messages are not loaded until `pou` or `pretty_output` methods have been called. The "lazy" mode can only be enabled when the `:file` option is provided (in other words, `:lazy` has no effect with the `:messages` setting):
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
class MyClass
|
227
|
+
include MessagesDictionary
|
228
|
+
has_messages_dictionary lazy: true, file: 'my_file.yml'
|
229
|
+
|
230
|
+
def greet
|
231
|
+
pou :hi
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# At this point no messages are loaded from the given file
|
236
|
+
|
237
|
+
obj = MyClass.new
|
238
|
+
|
239
|
+
# ... doing some other stuff ...
|
240
|
+
|
241
|
+
# Messages are still not loaded at this point!
|
242
|
+
|
243
|
+
obj.greet # Now all messages will be loaded from the YAML file
|
244
|
+
```
|
245
|
+
|
246
|
+
### Providing Custom Transformation Logic
|
205
247
|
|
206
248
|
Suppose you want to transform your message somehow or even simply return it instead of printing on the screen.
|
207
249
|
`pretty_output` method accepts an optional block for this purpose:
|
@@ -213,8 +255,12 @@ class MyClass
|
|
213
255
|
|
214
256
|
def greet
|
215
257
|
pou(:welcome) do |msg|
|
216
|
-
msg.upcase
|
258
|
+
msg.upcase
|
217
259
|
end
|
260
|
+
|
261
|
+
# Or simply:
|
262
|
+
|
263
|
+
pou(:welcome, &:upcase)
|
218
264
|
end
|
219
265
|
end
|
220
266
|
|
@@ -228,7 +274,7 @@ option:
|
|
228
274
|
```ruby
|
229
275
|
class MyClass
|
230
276
|
include MessagesDictionary
|
231
|
-
has_messages_dictionary transform: ->(msg) {msg.upcase
|
277
|
+
has_messages_dictionary transform: ->(msg) {msg.upcase}
|
232
278
|
|
233
279
|
def greet
|
234
280
|
pou(:welcome)
|
@@ -248,16 +294,54 @@ it anywhere (for example, when raising a custom error - see use case [here](http
|
|
248
294
|
If you do want to output your message after transformation, you have to do it explicitly:
|
249
295
|
|
250
296
|
```ruby
|
297
|
+
def greet
|
298
|
+
pou(:welcome) do |msg|
|
299
|
+
puts msg.upcase # => Prints "WELCOME"
|
300
|
+
end
|
301
|
+
end
|
302
|
+
```
|
303
|
+
|
304
|
+
### Handling missing keys
|
305
|
+
|
306
|
+
By default when a non-existent key is requested, an error will be raised:
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
class MyClass
|
310
|
+
include MessagesDictionary
|
311
|
+
has_messages_dictionary messages: {key: 'value'}
|
312
|
+
|
251
313
|
def greet
|
252
|
-
pou
|
253
|
-
msg.upcase!
|
254
|
-
puts msg # => Prints "WELCOME"
|
255
|
-
end
|
314
|
+
pou :unknown_key # trying to use some unknown key...
|
256
315
|
end
|
316
|
+
end
|
317
|
+
|
318
|
+
obj = MyClass.new
|
319
|
+
|
320
|
+
obj.greet # KeyError is raised here!
|
257
321
|
```
|
258
322
|
|
323
|
+
However, you can adjust the `:on_key_missing` option and provide a custom proc or lambda to handle all missing keys:
|
324
|
+
|
325
|
+
```ruby
|
326
|
+
class MyClass
|
327
|
+
include MessagesDictionary
|
328
|
+
has_messages_dictionary messages: {key: 'value'},
|
329
|
+
on_key_missing: ->(key) { key } # We simply return the requested key itself
|
330
|
+
|
331
|
+
def greet
|
332
|
+
pou :unknown_key
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
obj = MyClass.new
|
337
|
+
|
338
|
+
obj.greet # Prints "unknown_key" to the screen, no errors will be raised
|
339
|
+
```
|
340
|
+
|
341
|
+
So, in the example above we simply return the key itself if it was not found in the messages hash.
|
342
|
+
|
259
343
|
## License
|
260
344
|
|
261
345
|
Licensed under the [MIT License](https://github.com/bodrovis-learning/messages_dictionary/blob/master/LICENSE).
|
262
346
|
|
263
|
-
Copyright (c)
|
347
|
+
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
|