messages_dictionary 0.0.2 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +24 -13
- data/lib/messages_dictionary/injector.rb +1 -0
- data/lib/messages_dictionary/version.rb +1 -1
- data/spec/injector_spec.rb +11 -0
- data/spec/spec_helper.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 006a1d75edd683d26bb9ffae6490e2820fc764d0
|
4
|
+
data.tar.gz: 53fac182dd60a6e5ed9fba049e540ab0e8c8546a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fc44f71ab01840fbf419cf8b5f98dee6738e482d80454e4c81e43bc7f4740481f7d30bb8d7d0fecd7c0875d165895ddd158eea9ca34537be29c4c029c9bbc48
|
7
|
+
data.tar.gz: 7febb682b3f7b49f18a86b20da043fe1cfa5f7f840f9e5fa9e053d9eb22a47c7619c3dde4fd3eaad827c03a42dfb7f3b864f380d931f4707ad440f404076cf0e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,22 +4,31 @@
|
|
4
4
|
[![Code Climate](https://codeclimate.com/github/bodrovis-learning/messages_dictionary/badges/gpa.svg)](https://codeclimate.com/github/bodrovis-learning/messages_dictionary)
|
5
5
|
[![Dependency Status](https://gemnasium.com/bodrovis-learning/messages_dictionary.svg)](https://gemnasium.com/bodrovis-learning/messages_dictionary)
|
6
6
|
|
7
|
-
This gem
|
7
|
+
This gem started as an educational project for my student. The idea behind this gem is to organize
|
8
8
|
various messages in a simple key-value format that can be fetched later. Messages support interpolation,
|
9
9
|
can be stored inside files or passed as hashes (nested hashes are supported as well). Custom fetching rules
|
10
10
|
can be specified as well.
|
11
11
|
|
12
|
-
|
12
|
+
Install it
|
13
|
+
|
14
|
+
gem install messages_dictionary
|
15
|
+
|
16
|
+
and refer to the next sections to see it in action.
|
17
|
+
|
18
|
+
## Use Cases
|
19
|
+
|
20
|
+
Wanna see it in action? Some use-cases can be found, in the [Guesser](https://github.com/bodrovis/Guesser) game:
|
13
21
|
|
14
22
|
* [Messages are stored inside files](https://github.com/bodrovis/Guesser/tree/master/lib/guesser/messages)
|
15
23
|
* [Displaying errors](https://github.com/bodrovis/Guesser/blob/master/lib/guesser.rb#L25)
|
16
24
|
* [Displaying informational messages](https://github.com/bodrovis/Guesser/blob/master/lib/guesser/game.rb#L29)
|
17
25
|
|
18
|
-
|
19
|
-
|
20
|
-
gem install messages_dictionary
|
26
|
+
Another, a bit more complex, use case in the [lessons_indexer gem](https://github.com/bodrovis/lessons_indexer):
|
21
27
|
|
22
|
-
|
28
|
+
* [Messages are stored in a single file](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer/messages/messages.yml)
|
29
|
+
* [Messenger class equipped with messages_dictionary magic is defined](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer.rb#L7)
|
30
|
+
* [Other classes simply inherit from it](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer/indexer.rb#L2)
|
31
|
+
* [Messages are fetched easily](https://github.com/bodrovis/lessons_indexer/blob/master/lib/lessons_indexer/indexer.rb#L45)
|
23
32
|
|
24
33
|
## Usage
|
25
34
|
|
@@ -71,6 +80,8 @@ class MyOtherClass
|
|
71
80
|
|
72
81
|
def greet
|
73
82
|
pretty_output(:welcome)
|
83
|
+
# Or simply
|
84
|
+
pou(:welcome)
|
74
85
|
end
|
75
86
|
end
|
76
87
|
```
|
@@ -117,14 +128,14 @@ class MyClass
|
|
117
128
|
has_messages_dictionary
|
118
129
|
|
119
130
|
def do_something
|
120
|
-
|
131
|
+
pou('nested.value') # => 'Nested value'
|
121
132
|
end
|
122
133
|
end
|
123
134
|
```
|
124
135
|
|
125
136
|
### Indifferent Access
|
126
137
|
|
127
|
-
Keys can be passed to the `
|
138
|
+
Keys can be passed to the `pou` method as symbols or strings - it does not really matter:
|
128
139
|
|
129
140
|
```ruby
|
130
141
|
class MyClass
|
@@ -133,9 +144,9 @@ class MyClass
|
|
133
144
|
|
134
145
|
def calculate(a)
|
135
146
|
result = a ** 2
|
136
|
-
|
147
|
+
pou(:show_result, result: result)
|
137
148
|
# OR
|
138
|
-
|
149
|
+
pou('show_result', result: result)
|
139
150
|
end
|
140
151
|
end
|
141
152
|
```
|
@@ -199,7 +210,7 @@ class MyClass
|
|
199
210
|
has_messages_dictionary
|
200
211
|
|
201
212
|
def greet
|
202
|
-
|
213
|
+
pou(:welcome) do |msg|
|
203
214
|
msg.upcase!
|
204
215
|
end
|
205
216
|
end
|
@@ -218,7 +229,7 @@ class MyClass
|
|
218
229
|
has_messages_dictionary transform: ->(msg) {msg.upcase!}
|
219
230
|
|
220
231
|
def greet
|
221
|
-
|
232
|
+
pou(:welcome)
|
222
233
|
end
|
223
234
|
end
|
224
235
|
|
@@ -234,7 +245,7 @@ If you do want to output your message after transformation, you have to do it ex
|
|
234
245
|
|
235
246
|
```ruby
|
236
247
|
def greet
|
237
|
-
|
248
|
+
pou(:welcome) do |msg|
|
238
249
|
msg.upcase!
|
239
250
|
puts msg # => Prints "WELCOME"
|
240
251
|
end
|
data/spec/injector_spec.rb
CHANGED
@@ -40,6 +40,17 @@ RSpec.describe MessagesDictionary do
|
|
40
40
|
expect(output).to receive(:custom_puts).with('string')
|
41
41
|
object.send(:pretty_output, :test)
|
42
42
|
end
|
43
|
+
|
44
|
+
it "aliases pretty_output as pou" do
|
45
|
+
output = double('output')
|
46
|
+
@subject.class_eval do
|
47
|
+
has_messages_dictionary messages: {test: 'string'}, output: output
|
48
|
+
end
|
49
|
+
|
50
|
+
object = @subject.new
|
51
|
+
expect(output).to receive(:puts).with('string')
|
52
|
+
object.send(:pou, :test)
|
53
|
+
end
|
43
54
|
end
|
44
55
|
|
45
56
|
context "passed as hash" do
|
data/spec/spec_helper.rb
CHANGED