messages_dictionary 0.0.1.rc2 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9c2443ad99393ac68dd66fd8311f93a22e6c990
4
- data.tar.gz: 62ed879f27e7be5be35ce595b7e8d87498937fa0
3
+ metadata.gz: 36034266259848faccc844f5b655b8afae375e05
4
+ data.tar.gz: 9585e85d023b9531ba57f43c59931da54ffefc4f
5
5
  SHA512:
6
- metadata.gz: 482c06deceb1b6db8d5b21752400417dfa1f8c7ea6ae9f9eefa0d8d6349d133c01aa1dd16b28964800d5b4871b133c96109e1fed6823f1b11ff97e4d551a524e
7
- data.tar.gz: 02a34c68b32170ba8172a089066273389f7690b84a06e379fadeab4d292a12234f8ab339df89e3cc2afbe529c8a3f0bdc1e111098549a700de1a8d2e3a170e29
6
+ metadata.gz: cfa579275d388efe14516ea0374459dc152ada66803e2b37165f301d13c0ef939ba58cba9c4f8c0733f5d593836579395283a58d5b5a809b611134ee8c1be04a
7
+ data.tar.gz: a3abcb0af2d095b7a8a938c445aab3d64db900ea31b004d99ff02f63da2009ebd28db6f26dc96632b1fba98e2ec06bac4eed1e781e0fea5cfef7db6348c81a1b
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc --order rand --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.5
6
+ - 2.2.2
7
+ - rbx
8
+
9
+ install: bundle install --retry=3
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
data/Gemfile.lock CHANGED
@@ -1,20 +1,55 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- messages_dictionary (0.0.1.rc2)
4
+ messages_dictionary (0.0.1)
5
5
  hashie (~> 3.4)
6
6
 
7
7
  GEM
8
- remote: http://rubygems.org/
8
+ remote: https://rubygems.org/
9
9
  specs:
10
+ codeclimate-test-reporter (0.5.0)
11
+ simplecov (>= 0.7.1, < 1.0.0)
12
+ coderay (1.1.1)
13
+ diff-lcs (1.2.5)
14
+ docile (1.1.5)
10
15
  hashie (3.4.3)
16
+ json (1.8.3)
17
+ method_source (0.8.2)
18
+ pry (0.10.3)
19
+ coderay (~> 1.1.0)
20
+ method_source (~> 0.8.1)
21
+ slop (~> 3.4)
22
+ rake (11.1.2)
23
+ rspec (3.4.0)
24
+ rspec-core (~> 3.4.0)
25
+ rspec-expectations (~> 3.4.0)
26
+ rspec-mocks (~> 3.4.0)
27
+ rspec-core (3.4.4)
28
+ rspec-support (~> 3.4.0)
29
+ rspec-expectations (3.4.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.4.0)
32
+ rspec-mocks (3.4.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.4.0)
35
+ rspec-support (3.4.1)
36
+ simplecov (0.11.2)
37
+ docile (~> 1.1.0)
38
+ json (~> 1.8)
39
+ simplecov-html (~> 0.10.0)
40
+ simplecov-html (0.10.0)
41
+ slop (3.6.0)
11
42
 
12
43
  PLATFORMS
13
44
  ruby
14
45
  x86-mingw32
15
46
 
16
47
  DEPENDENCIES
48
+ codeclimate-test-reporter (~> 0.5)
17
49
  messages_dictionary!
50
+ pry
51
+ rake (~> 11.1)
52
+ rspec (~> 3.4)
18
53
 
19
54
  BUNDLED WITH
20
55
  1.11.2
data/README.md CHANGED
@@ -1,11 +1,25 @@
1
1
  # MessagesDictionary
2
+ [![Gem Version](https://badge.fury.io/rb/messages_dictionary.svg)](https://badge.fury.io/rb/messages_dictionary)
3
+ [![Build Status](https://travis-ci.org/bodrovis-learning/messages_dictionary.svg?branch=master)](https://travis-ci.org/bodrovis-learning/messages_dictionary)
4
+ [![Code Climate](https://codeclimate.com/github/bodrovis-learning/messages_dictionary/badges/gpa.svg)](https://codeclimate.com/github/bodrovis-learning/messages_dictionary)
5
+ [![Dependency Status](https://gemnasium.com/bodrovis-learning/messages_dictionary.svg)](https://gemnasium.com/bodrovis-learning/messages_dictionary)
2
6
 
3
7
  This gem was created as an educational project for my student. The idea behind this gem is to organize
4
8
  various messages in a simple key-value format that can be fetched later. Messages support interpolation,
5
9
  can be stored inside files or passed as hashes (nested hashes are supported as well). Custom fetching rules
6
10
  can be specified as well.
7
11
 
8
- Refer to the next section to see it in action.
12
+ Some use-cases can be found, for example, in the [Guesser](https://github.com/bodrovis/Guesser) game:
13
+
14
+ * [Messages are stored inside files](https://github.com/bodrovis/Guesser/tree/master/lib/guesser/messages)
15
+ * [Displaying errors](https://github.com/bodrovis/Guesser/blob/master/lib/guesser.rb#L25)
16
+ * [Displaying informational messages](https://github.com/bodrovis/Guesser/blob/master/lib/guesser/game.rb#L29)
17
+
18
+ Install it
19
+
20
+ gem install messages_dictionary
21
+
22
+ and refer to the next section to see it in action.
9
23
 
10
24
  ## Usage
11
25
 
@@ -32,9 +46,11 @@ class MyOtherClass
32
46
  end
33
47
  ```
34
48
 
35
- With `messages_dictionary` you can transform it into
49
+ These messages are scattered all over the program and can be hard to maintain. With `messages_dictionary` you can transform it into
36
50
 
37
51
  ```ruby
52
+ require 'messages_dictionary' # For brevity this line will be omitted in other examples
53
+
38
54
  class MyClass
39
55
  include MessagesDictionary
40
56
  has_messages_dictionary
@@ -74,14 +90,62 @@ show_values: "The first value is {{a}}, the second is {{b}}"
74
90
  welcome: "Welcome!"
75
91
  ```
76
92
 
93
+ **Please note**, that if your class is named `MyModule::MyClass`, then by default the program will search
94
+ for a file named `my_class.yml` inside `my_module` directory. This can be further customized, refer
95
+ the "Further Customization" section for more info.
96
+
77
97
  So by saying `pretty_output(:show_result, result: result)` you are fetching a message under the key
78
98
  `show_result` and replace the `{{result}}` part with the value of the `result` variable. Simple, eh?
79
99
 
100
+ ### Nesting
101
+
102
+ MessagesDictionary supports nesting (similar to localization files in Rails):
103
+
104
+ *my_class.yml*
105
+
106
+ ```yaml
107
+ show_result: "The result is {{result}}"
108
+ nested:
109
+ value: 'Nested value'
110
+ ```
111
+
112
+ Nested messages can be easily accessed with dot notation:
113
+
114
+ ```ruby
115
+ class MyClass
116
+ include MessagesDictionary
117
+ has_messages_dictionary
118
+
119
+ def do_something
120
+ pretty_output('nested.value') # => 'Nested value'
121
+ end
122
+ end
123
+ ```
124
+
125
+ ### Indifferent Access
126
+
127
+ Keys can be passed to the `pretty_output` method as symbols or strings - it does not really matter:
128
+
129
+ ```ruby
130
+ class MyClass
131
+ include MessagesDictionary
132
+ has_messages_dictionary
133
+
134
+ def calculate(a)
135
+ result = a ** 2
136
+ pretty_output(:show_result, result: result)
137
+ # OR
138
+ pretty_output('show_result', result: result)
139
+ end
140
+ end
141
+ ```
142
+
80
143
  ### Further Customization
81
144
 
82
145
  #### Specifying File Name and Directory
83
146
 
84
- By default `messages_dictionary` will search for a *.yml* file named after your class (converted snake case)
147
+ By default `messages_dictionary` will search for a *.yml* file named after your class (converted to snake case,
148
+ so for the `MyClass` the file should be named *my_class.yml*)
85
149
  inside the same directory. However, this behavior can be easily changed with the following options:
86
150
 
87
151
  * `:file` (`string`) - specifies the file name to load messages from (extension has to be provided).
@@ -94,9 +158,24 @@ class MyClass
94
158
  end
95
159
  ```
96
160
 
161
+ Both of these options are not mandatory.
162
+
163
+ #### Specifying Messages Hash
164
+
165
+ Instead of loading messages from a file, you can pass hash to the `has_messages_dictionary` using `:messages` option:
166
+
167
+ ```ruby
168
+ class MyClass
169
+ include MessagesDictionary
170
+ has_messages_dictionary messages: {key: 'value'}
171
+ end
172
+ ```
173
+
174
+ Nesting and all other features are supported as well.
175
+
97
176
  #### Specifying Output and Display Method
98
177
 
99
- By default all messages will be outputted to `STDOUT` using `puts` method, however this can be changed as well:
178
+ By default all messages will be outputted to `STDOUT` using `puts` method, however this can be changed:
100
179
 
101
180
  * `:output` (`object`) - specify your own output. The object you provide has to implement `puts` method
102
181
  or any other method you provide for the `:method` option.
@@ -111,7 +190,7 @@ end
111
190
 
112
191
  #### Providing Custom Transformation Logic
113
192
 
114
- Suppose you want to transform your message somehow or even return it instead of printing on the screen.
193
+ Suppose you want to transform your message somehow or even simply return it instead of printing on the screen.
115
194
  `pretty_output` method accepts an optional block for this purpose:
116
195
 
117
196
  ```ruby
@@ -147,6 +226,21 @@ my_object = MyClass.new
147
226
  my_object.greet # Will return "WELCOME", nothing will be put on the screen
148
227
  ```
149
228
 
229
+ **Please note** that by default MessagesDictionary **does not output anything** when you provide transformation
230
+ block. This is done to allow more control, because sometimes you may want to fetch a message, but not output
231
+ it anywhere (for example, when raising a custom error - see use case [here](https://github.com/bodrovis/Guesser/blob/master/lib/guesser.rb#L25)).
232
+
233
+ If you do want to output your message after transformation, you have to do it explicitly:
234
+
235
+ ```ruby
236
+ def greet
237
+ pretty_output(:welcome) do |msg|
238
+ msg.upcase!
239
+ puts msg # => Prints "WELCOME"
240
+ end
241
+ end
242
+ ```
243
+
150
244
  ## License
151
245
 
152
246
  Licensed under the [MIT License](https://github.com/bodrovis-learning/messages_dictionary/blob/master/LICENSE).
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
@@ -26,13 +26,11 @@ module MessagesDictionary
26
26
  values.each do |k, v|
27
27
  msg.gsub!(Regexp.new('\{\{' + k.to_s + '\}\}'), v.to_s)
28
28
  end
29
-
30
29
  transform = klass::DICTIONARY_CONF[:transform] || block
31
30
  transform ?
32
31
  transform.call(msg) :
33
32
  klass::DICTIONARY_CONF[:output].send(klass::DICTIONARY_CONF[:method].to_sym, msg)
34
33
  end
35
-
36
34
  private :pretty_output
37
35
  end
38
36
  end
@@ -1,3 +1,3 @@
1
1
  module MessagesDictionary
2
- VERSION = '0.0.1.rc2'
2
+ VERSION = '0.0.1'
3
3
  end
@@ -17,4 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_dependency 'hashie', '~> 3.4'
20
+
21
+ spec.add_development_dependency "rake", "~> 11.1"
22
+ spec.add_development_dependency "rspec", "~> 3.4"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.5"
20
25
  end
@@ -0,0 +1,125 @@
1
+ RSpec.describe MessagesDictionary do
2
+ before :all do
3
+ @subject = SpecAddons::TestClass
4
+ @subject.class_eval do
5
+ include MessagesDictionary
6
+ end
7
+ end
8
+
9
+ before :each do
10
+ @subject.class_eval do
11
+ remove_const(:DICTIONARY_CONF) if const_defined?(:DICTIONARY_CONF)
12
+ end
13
+ end
14
+
15
+ context "messages" do
16
+ context "outputting" do
17
+ it "uses STDOUT by default" do
18
+ @subject.class_eval do
19
+ has_messages_dictionary messages: {test: 'string'}
20
+ end
21
+
22
+ expect(@subject::DICTIONARY_CONF[:output]).to eq(STDOUT)
23
+ end
24
+
25
+ it "uses puts method by default" do
26
+ @subject.class_eval do
27
+ has_messages_dictionary messages: {test: 'string'}
28
+ end
29
+
30
+ expect(@subject::DICTIONARY_CONF[:method]).to eq(:puts)
31
+ end
32
+
33
+ it "allows customizing output and method" do
34
+ output = double('output')
35
+ @subject.class_eval do
36
+ has_messages_dictionary messages: {test: 'string'}, output: output, method: :custom_puts
37
+ end
38
+
39
+ object = @subject.new
40
+ expect(output).to receive(:custom_puts).with('string')
41
+ object.send(:pretty_output, :test)
42
+ end
43
+ end
44
+
45
+ context "passed as hash" do
46
+ it "supports nesting" do
47
+ @subject.class_eval do
48
+ has_messages_dictionary messages: {parent: {child: 'child_string'} }
49
+ end
50
+
51
+ object = @subject.new
52
+ expect( object.send(:pretty_output, 'parent.child') {|msg| msg} ).to eq('child_string')
53
+ end
54
+ end
55
+
56
+ context "passed in file" do
57
+ it "searches file named after class name by default" do
58
+ setup_env!('spec_addons', 'test_class.yml')
59
+
60
+ @subject.class_eval do
61
+ has_messages_dictionary
62
+ end
63
+
64
+ object = @subject.new
65
+ expect( object.send(:pretty_output, :test) {|msg| msg} ).to eq('string')
66
+ clear_env!('spec_addons')
67
+ end
68
+
69
+ it "allows passing path and file" do
70
+ setup_env!('my_test_dir', 'my_test_file.yml')
71
+
72
+ @subject.class_eval do
73
+ has_messages_dictionary file: 'my_test_file.yml', dir: 'my_test_dir'
74
+ end
75
+
76
+ object = @subject.new
77
+ expect( object.send(:pretty_output, :test) {|msg| msg} ).to eq('string')
78
+ clear_env!('my_test_dir')
79
+ end
80
+ end
81
+ end
82
+
83
+ context "error" do
84
+ it "is raised when key is not found" do
85
+ @subject.class_eval do
86
+ has_messages_dictionary messages: {test: 'string'}
87
+ end
88
+
89
+ object = @subject.new
90
+ expect( -> {object.send(:pretty_output, :does_not_exist)} ).to raise_error(KeyError)
91
+ end
92
+
93
+ it "is raised when file is not found and the program aborts" do
94
+ err = capture_stderr do
95
+ expect(-> {
96
+ @subject.class_eval do
97
+ has_messages_dictionary dir: 'random', file: 'not_exist.yml'
98
+ end
99
+ }).to raise_error(SystemExit)
100
+ end.strip
101
+ expect(err).to eq("File #{File.expand_path('random/not_exist.yml')} does not exist...")
102
+ end
103
+ end
104
+
105
+ context "transformations" do
106
+ it "applies per-method transformations" do
107
+ @subject.class_eval do
108
+ has_messages_dictionary messages: {test: 'string'}
109
+ end
110
+
111
+ object = @subject.new
112
+ expect( object.send(:pretty_output, :test) {|msg| msg.upcase!} ).to eq('STRING')
113
+ end
114
+
115
+ it "applies per-class transformations" do
116
+ @subject.class_eval do
117
+ has_messages_dictionary messages: {test: 'string'},
118
+ transform: ->(msg) {msg.upcase!}
119
+ end
120
+
121
+ object = @subject.new
122
+ expect( object.send(:pretty_output, :test) ).to eq('STRING')
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,14 @@
1
+ require "pry"
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+
5
+ $LOAD_PATH << File.expand_path('../../../lib', __FILE__)
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
7
+
8
+ require 'messages_dictionary'
9
+
10
+ RSpec.configure do |config|
11
+ config.include SpecAddons
12
+ config.include SpecFilesSetup
13
+ config.include SpecUtils
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'fileutils'
2
+
3
+ module SpecFilesSetup
4
+ def setup_env!(path, file)
5
+ FileUtils.mkdir_p(path)
6
+ f = File.new("#{path}/#{file}", 'w+')
7
+ f.write('test: string')
8
+ f.close
9
+ end
10
+
11
+ def clear_env!(path)
12
+ FileUtils.remove_entry(path)
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module SpecUtils
2
+ def capture_stderr(&block)
3
+ original_stderr = $stderr
4
+ $stderr = fake = StringIO.new
5
+ begin
6
+ yield
7
+ ensure
8
+ $stderr = original_stderr
9
+ end
10
+ fake.string
11
+ end
12
+
13
+ def capture_stdout(&block)
14
+ original_stdout = $stdout
15
+ $stdout = fake = StringIO.new
16
+ begin
17
+ yield
18
+ ensure
19
+ $stdout = original_stdout
20
+ end
21
+ fake.string
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module SpecAddons
2
+ class TestClass
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+ RSpec.describe MessagesDictionary::Dict do
2
+ subject { described_class.new key: 'value' }
3
+
4
+ it "allows indifferent access" do
5
+ expect(subject[:key]).to eq('value')
6
+ expect(subject['key']).to eq('value')
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.describe MessagesDictionary::SpecialString do
2
+ subject { described_class.new('MyTestString') }
3
+
4
+ specify "#snake_case" do
5
+ expect(subject.snake_case).to eq('my_test_string')
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messages_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.rc2
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-11 00:00:00.000000000 Z
12
+ date: 2016-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -25,6 +25,62 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '3.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '11.1'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '11.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.4'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.4'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pry
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: codeclimate-test-reporter
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.5'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.5'
28
84
  description: This gem allows you to store messages in a simple-key value format and
29
85
  fetch whenever you need. Classes can have different messages and apply different
30
86
  fetching logic. Messages also support interpolation.
@@ -36,16 +92,26 @@ extra_rdoc_files:
36
92
  - README.md
37
93
  files:
38
94
  - ".gitignore"
95
+ - ".rspec"
96
+ - ".travis.yml"
39
97
  - Gemfile
40
98
  - Gemfile.lock
41
99
  - LICENSE
42
100
  - README.md
101
+ - Rakefile
43
102
  - lib/messages_dictionary.rb
44
103
  - lib/messages_dictionary/injector.rb
45
104
  - lib/messages_dictionary/utils/dict.rb
46
105
  - lib/messages_dictionary/utils/snake_case.rb
47
106
  - lib/messages_dictionary/version.rb
48
107
  - messages_dictionary.gemspec
108
+ - spec/injector_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/spec_files_setup.rb
111
+ - spec/support/spec_utils.rb
112
+ - spec/support/test_class.rb
113
+ - spec/utils/dict_spec.rb
114
+ - spec/utils/snake_case_spec.rb
49
115
  homepage: https://github.com/bodrovis-learning/messages_dictionary
50
116
  licenses:
51
117
  - MIT
@@ -61,13 +127,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
127
  version: '0'
62
128
  required_rubygems_version: !ruby/object:Gem::Requirement
63
129
  requirements:
64
- - - ">"
130
+ - - ">="
65
131
  - !ruby/object:Gem::Version
66
- version: 1.3.1
132
+ version: '0'
67
133
  requirements: []
68
134
  rubyforge_project:
69
- rubygems_version: 2.6.1
135
+ rubygems_version: 2.6.3
70
136
  signing_key:
71
137
  specification_version: 4
72
138
  summary: Store your messages anywhere and fetch them anytime.
73
139
  test_files: []
140
+ has_rdoc: