messages_dictionary 0.0.1.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9c2443ad99393ac68dd66fd8311f93a22e6c990
4
+ data.tar.gz: 62ed879f27e7be5be35ce595b7e8d87498937fa0
5
+ SHA512:
6
+ metadata.gz: 482c06deceb1b6db8d5b21752400417dfa1f8c7ea6ae9f9eefa0d8d6349d133c01aa1dd16b28964800d5b4871b133c96109e1fed6823f1b11ff97e4d551a524e
7
+ data.tar.gz: 02a34c68b32170ba8172a089066273389f7690b84a06e379fadeab4d292a12234f8ab339df89e3cc2afbe529c8a3f0bdc1e111098549a700de1a8d2e3a170e29
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ # RubyMine config files
2
+ .idea/
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ messages_dictionary (0.0.1.rc2)
5
+ hashie (~> 3.4)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ hashie (3.4.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+ x86-mingw32
15
+
16
+ DEPENDENCIES
17
+ messages_dictionary!
18
+
19
+ BUNDLED WITH
20
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Ilya Bodrov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # MessagesDictionary
2
+
3
+ This gem was created as an educational project for my student. The idea behind this gem is to organize
4
+ various messages in a simple key-value format that can be fetched later. Messages support interpolation,
5
+ can be stored inside files or passed as hashes (nested hashes are supported as well). Custom fetching rules
6
+ can be specified as well.
7
+
8
+ Refer to the next section to see it in action.
9
+
10
+ ## Usage
11
+
12
+ ### Basic Example
13
+
14
+ Suppose you have the following program:
15
+
16
+ ```ruby
17
+ class MyClass
18
+ def calculate(a)
19
+ result = a ** 2
20
+ puts "The result is #{result}"
21
+ end
22
+ end
23
+
24
+ class MyOtherClass
25
+ def some_action(a, b)
26
+ puts "The first value is #{a}, the second is #{b}"
27
+ end
28
+
29
+ def greet
30
+ puts "Welcome!"
31
+ end
32
+ end
33
+ ```
34
+
35
+ With `messages_dictionary` you can transform it into
36
+
37
+ ```ruby
38
+ class MyClass
39
+ include MessagesDictionary
40
+ has_messages_dictionary
41
+
42
+ def calculate(a)
43
+ result = a ** 2
44
+ pretty_output(:show_result, result: result)
45
+ end
46
+ end
47
+
48
+ class MyOtherClass
49
+ include MessagesDictionary
50
+ has_messages_dictionary
51
+
52
+ def some_action(a, b)
53
+ pretty_output(:show_values, first: a, second: b)
54
+ end
55
+
56
+ def greet
57
+ pretty_output(:welcome)
58
+ end
59
+ end
60
+ ```
61
+
62
+ The only thing you have to do is create two *.yml* files named after your classes:
63
+
64
+ *my_class.yml*
65
+
66
+ ```yaml
67
+ show_result: "The result is {{result}}"
68
+ ```
69
+
70
+ *my_other_class.yml*
71
+
72
+ ```yaml
73
+ show_values: "The first value is {{a}}, the second is {{b}}"
74
+ welcome: "Welcome!"
75
+ ```
76
+
77
+ So by saying `pretty_output(:show_result, result: result)` you are fetching a message under the key
78
+ `show_result` and replace the `{{result}}` part with the value of the `result` variable. Simple, eh?
79
+
80
+ ### Further Customization
81
+
82
+ #### Specifying File Name and Directory
83
+
84
+ By default `messages_dictionary` will search for a *.yml* file named after your class (converted snake case)
85
+ inside the same directory. However, this behavior can be easily changed with the following options:
86
+
87
+ * `:file` (`string`) - specifies the file name to load messages from (extension has to be provided).
88
+ * `:dir` (`string`) - specifies the directory to load file from.
89
+
90
+ ```ruby
91
+ class MyClass
92
+ include MessagesDictionary
93
+ has_messages_dictionary file: 'some_file.yml', dir: 'C:\my_docs'
94
+ end
95
+ ```
96
+
97
+ #### Specifying Output and Display Method
98
+
99
+ By default all messages will be outputted to `STDOUT` using `puts` method, however this can be changed as well:
100
+
101
+ * `:output` (`object`) - specify your own output. The object you provide has to implement `puts` method
102
+ or any other method you provide for the `:method` option.
103
+ * `:method` (`symbol` or `string`) - specify method to use (like `warn` or `abort`, for example).
104
+
105
+ ```ruby
106
+ class MyClass
107
+ include MessagesDictionary
108
+ has_messages_dictionary output: STDOUT, method: :warn
109
+ end
110
+ ```
111
+
112
+ #### Providing Custom Transformation Logic
113
+
114
+ Suppose you want to transform your message somehow or even return it instead of printing on the screen.
115
+ `pretty_output` method accepts an optional block for this purpose:
116
+
117
+ ```ruby
118
+ class MyClass
119
+ include MessagesDictionary
120
+ has_messages_dictionary
121
+
122
+ def greet
123
+ pretty_output(:welcome) do |msg|
124
+ msg.upcase!
125
+ end
126
+ end
127
+ end
128
+
129
+ my_object = MyClass.new
130
+ my_object.greet # Will return "WELCOME", nothing will be put on the screen
131
+ ```
132
+
133
+ You can also specify transformation logic globally by assigning a procedure or lambda to the `:transform`
134
+ option:
135
+
136
+ ```ruby
137
+ class MyClass
138
+ include MessagesDictionary
139
+ has_messages_dictionary transform: ->(msg) {msg.upcase!}
140
+
141
+ def greet
142
+ pretty_output(:welcome)
143
+ end
144
+ end
145
+
146
+ my_object = MyClass.new
147
+ my_object.greet # Will return "WELCOME", nothing will be put on the screen
148
+ ```
149
+
150
+ ## License
151
+
152
+ Licensed under the [MIT License](https://github.com/bodrovis-learning/messages_dictionary/blob/master/LICENSE).
153
+
154
+ Copyright (c) 2016 [Ilya Bodrov](http://radiant-wind.com)
@@ -0,0 +1,39 @@
1
+ module MessagesDictionary
2
+ def self.included(klass)
3
+ klass.class_exec do
4
+ define_singleton_method :has_messages_dictionary do |opts = {}|
5
+ if opts[:messages]
6
+ messages = Dict.new(opts[:messages])
7
+ else
8
+ file = opts[:file] || "#{SpecialString.new(klass.name).snake_case}.yml"
9
+ file = File.expand_path(file, opts[:dir]) if opts[:dir]
10
+ begin
11
+ messages = Dict.new(YAML.load_file(file))
12
+ rescue Errno::ENOENT
13
+ abort "File #{file} does not exist..."
14
+ end
15
+ 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
+ end
21
+
22
+ define_method :pretty_output do |key, values = {}, &block|
23
+ msg = klass::DICTIONARY_CONF[:msgs].deep_fetch(*key.to_s.split('.')) do
24
+ raise KeyError, "#{key} cannot be found in the provided file..."
25
+ end
26
+ values.each do |k, v|
27
+ msg.gsub!(Regexp.new('\{\{' + k.to_s + '\}\}'), v.to_s)
28
+ end
29
+
30
+ transform = klass::DICTIONARY_CONF[:transform] || block
31
+ transform ?
32
+ transform.call(msg) :
33
+ klass::DICTIONARY_CONF[:output].send(klass::DICTIONARY_CONF[:method].to_sym, msg)
34
+ end
35
+
36
+ private :pretty_output
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ module MessagesDictionary
2
+ class Dict < Hash
3
+ include Hashie::Extensions::MergeInitializer
4
+ include Hashie::Extensions::IndifferentAccess
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ module MessagesDictionary
2
+ class SpecialString
3
+ attr_accessor :string
4
+
5
+ def initialize(string)
6
+ @string = string
7
+ end
8
+
9
+ def snake_case
10
+ string.gsub(/::/, '/').
11
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
12
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
13
+ tr("-", "_").
14
+ downcase
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module MessagesDictionary
2
+ VERSION = '0.0.1.rc2'
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+
4
+ require_relative 'messages_dictionary/utils/snake_case'
5
+ require_relative 'messages_dictionary/utils/dict'
6
+ require_relative 'messages_dictionary/injector'
7
+
8
+ module MessagesDictionary
9
+ end
10
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path("../lib/messages_dictionary/version", __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "messages_dictionary"
5
+ spec.version = MessagesDictionary::VERSION
6
+ spec.authors = ["Ilya Bodrov", "anilika"]
7
+ spec.email = ["golosizpru@gmail.com"]
8
+ spec.summary = %q{Store your messages anywhere and fetch them anytime.}
9
+ spec.description = %q{This gem allows you to store messages in a simple-key value format and fetch whenever you need. Classes can have different messages and apply different fetching logic. Messages also support interpolation.}
10
+ spec.homepage = "https://github.com/bodrovis-learning/messages_dictionary"
11
+ spec.license = "MIT"
12
+ spec.platform = Gem::Platform::RUBY
13
+
14
+ spec.files = `git ls-files`.split("\n")
15
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ spec.extra_rdoc_files = ["README.md"]
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency 'hashie', '~> 3.4'
20
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messages_dictionary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc2
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Bodrov
8
+ - anilika
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.4'
28
+ description: This gem allows you to store messages in a simple-key value format and
29
+ fetch whenever you need. Classes can have different messages and apply different
30
+ fetching logic. Messages also support interpolation.
31
+ email:
32
+ - golosizpru@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - README.md
37
+ files:
38
+ - ".gitignore"
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE
42
+ - README.md
43
+ - lib/messages_dictionary.rb
44
+ - lib/messages_dictionary/injector.rb
45
+ - lib/messages_dictionary/utils/dict.rb
46
+ - lib/messages_dictionary/utils/snake_case.rb
47
+ - lib/messages_dictionary/version.rb
48
+ - messages_dictionary.gemspec
49
+ homepage: https://github.com/bodrovis-learning/messages_dictionary
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">"
65
+ - !ruby/object:Gem::Version
66
+ version: 1.3.1
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.6.1
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Store your messages anywhere and fetch them anytime.
73
+ test_files: []