erlash 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3141fb9ca30266bcc7d0af47bd7eb3033f5a48e02aeae9bd0b4c7eb5aea38ced
4
+ data.tar.gz: c1bb0dc9ad4671409ec0fd0b8d31d45b0c97c8a6e1577f3b57af6df07b4c6aca
5
+ SHA512:
6
+ metadata.gz: 1da3b16b757c08eabecd637d6429a1d91dfcf1c525f1eeb2c10933a3bc0e60f8fa15d47f47b0925074b2bd7649c95f1a0a4320acb5f078e7612007c87e1b813a
7
+ data.tar.gz: c53b02164e363bbc1ceb2b8a319194586a370ce5372e4589f5b87e8e5087d88a804338feb1a14cd2985de9c4d575669fef3bf2ba515e3454a841bc7a20b951c5
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at arturictus@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in erlash.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ erlash (0.1.0)
5
+ fusu
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ fusu (0.2.0)
13
+ method_source (0.9.0)
14
+ pry (0.11.3)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ rake (10.5.0)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.0)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.16)
37
+ erlash!
38
+ pry
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+
42
+ BUNDLED WITH
43
+ 1.16.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Artur
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # Erlash
2
+
3
+ Erlash is a easy way to create meaningful errors.
4
+ The ruby implementation for errors is hard and tedious to create errors that explain
5
+ what exactly the error was.
6
+ In production you can find errors that do not explain the context and gives you no clue what's going on.
7
+
8
+ You can find errors like:
9
+
10
+ ```
11
+ ValidationError:
12
+ email already token
13
+ ```
14
+
15
+ Wouldn't be nice to have more information about the error?
16
+
17
+ example:
18
+ ```
19
+ RequestError:
20
+ Problem:
21
+ User is unable to update his email
22
+ Summary:
23
+ Validation errors: email already token
24
+ Context:
25
+ - request_id: `123`
26
+ - user: { id: 1, email: `mail@example.com` }
27
+ - endpoint: `PUT /users/1`
28
+ - params: { email: `another@email.com` }
29
+ ```
30
+
31
+ To build this kind of errors with the standard library is very tedious and not reusable.
32
+ With erlash creating meaningful errors is easy.
33
+
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'erlash'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install erlash
50
+
51
+ ## Usage
52
+
53
+ ```ruby
54
+ class MyError < Erlash::Base; end
55
+
56
+ raise Myerror, { user_id: 1, request_id: 120, controller: 'users_controller' }
57
+ # Myerror:
58
+ # - user_id: 1
59
+ # - request_id: 120
60
+ # - controller: `users_controller`
61
+ ```
62
+
63
+ ```ruby
64
+ class MyError < Erlash::Base;
65
+ problem -> { |context| "Please user `#{context[:user_id]}` be careful" }
66
+ summary "This error usually happens when user is desperate for the bug"
67
+ resolution -> { |context| "User.find(#{context[:user_id]}).fix" }
68
+ end
69
+
70
+ raise Myerror, { user_id: 1, request_id: 120, controller: 'users_controller' }
71
+ # Myerror:
72
+ # Problem:
73
+ # Please user `1` be careful
74
+ # Summary:
75
+ # This error usually happens when user is desperate for the bug
76
+ # Resolution:
77
+ # User.find(1).fix
78
+ # Context:
79
+ # - user_id: 1
80
+ # - request_id: 120
81
+ # - controller: `users_controller`
82
+ ```
83
+
84
+ ### Formatters
85
+ Erlash is fully customizable.
86
+ You can add formatters for your objects or override the default ones.
87
+
88
+ __Create your formatter:__
89
+
90
+ Create a class that inherits from `Erlash::TemplateFormatter` and add a `format`
91
+ method to it.
92
+
93
+ ```ruby
94
+ class MyUserFormatter < Erlash::TemplateFormatter
95
+ def format
96
+ "And the name is: #{object.name}"
97
+ end
98
+ end
99
+ ```
100
+
101
+ __Register your formatter:__
102
+
103
+ When registering a formatter you should provide the class is going to format as first argument.
104
+ Arguments:
105
+ - class to be formatted. ex: `User`
106
+ - formatter for given class. ex: `MyUserFormatter`
107
+
108
+ ```ruby
109
+ Erlash.formatters.register(User, MyUserFormatter)
110
+ ```
111
+
112
+ __Erlash::TemplateFormatter__
113
+
114
+ Accessible methods:
115
+ - `object`: is an instance of the registered class. in the example `User` instance
116
+ - `format_elem`: will try to find a formatter for given object if not will default `to_s`
117
+
118
+ * args: [object]
119
+
120
+ example:
121
+
122
+ ```ruby
123
+ class User
124
+ def email
125
+ "mail@example.com"
126
+ end
127
+ def id; 1 end
128
+ def errors
129
+ "email already token"
130
+ end
131
+ end
132
+
133
+ class Erlash::UserFormatter < Erlash::TemplateFormatter
134
+ def format
135
+ format_elem({
136
+ id: object.id,
137
+ email: object.email
138
+ }) # it will be formatted by the registered formatter for Hash
139
+ end
140
+ end
141
+
142
+ Erlash.formatters.register(User, Erlash::UserFormatter)
143
+
144
+ class RequestError < Erlash::Base
145
+ problem "User is unable to update his email"
146
+ summary do |context|
147
+ "Validation errors: #{context[:user].errors}"
148
+ end
149
+ end
150
+
151
+ raise RequestError.new(request_id: '123', user: User.new, endpoint: 'PUT /users/1', params: {email: "another@email.com"})
152
+ # RequestError:
153
+ # Problem:
154
+ # User is unable to update his email
155
+ # Summary:
156
+ # Validation errors: email already token
157
+ # Context:
158
+ # - request_id: `123`
159
+ # - user: { id: 1, email: `mail@example.com` }
160
+ # - endpoint: `PUT /users/1`
161
+ # - params: { email: `another@email.com` }
162
+ ```
163
+
164
+ Default formatters:
165
+
166
+ * `Array` => `Erlash::ArrayFormatter`
167
+ * `Hash` => `Erlash::HashFormatter`
168
+ * `String` => `Erlash::StringFormatter`
169
+
170
+ Custom Erlash classes:
171
+
172
+ * `Erlash::Tip` => `Erlash::TipFormatter`
173
+ * `Erlash::Context` => `Erlash::ContextFormatter`
174
+ * `Erlash::MainArray` => `Erlash::MainArrayFormatter`
175
+ * `Erlash::MainHash` => `Erlash::MainHashFormatter`
176
+
177
+ ## Development
178
+
179
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
180
+
181
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
182
+
183
+ ## Contributing
184
+
185
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/erlash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
186
+
187
+ ## License
188
+
189
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
190
+
191
+ ## Code of Conduct
192
+
193
+ Everyone interacting in the Erlash project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/erlash/blob/master/CODE_OF_CONDUCT.md).
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "erlash"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/erlash.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "erlash/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "erlash"
8
+ spec.version = Erlash::VERSION
9
+ spec.authors = ["Artur"]
10
+ spec.email = ["arturictus@gmail.com"]
11
+
12
+ spec.summary = %q{Handle your errors gracefully.}
13
+ spec.description = %q{Error abstraction to simplify the creation of descriptive errors.}
14
+ spec.homepage = "https://github.com/arturictus/erlash"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_dependency 'fusu'
30
+ end
@@ -0,0 +1,90 @@
1
+ module Erlash
2
+ class Base < StandardError
3
+ class << self
4
+ def problem(arg = nil, &block)
5
+ @problem = block || arg
6
+ end
7
+ def summary(arg = nil, &block)
8
+ @summary = block || arg
9
+ end
10
+ def resolution(arg = nil, &block)
11
+ @resolution = block || arg
12
+ end
13
+ def display_context(arg)
14
+ @display_context = arg
15
+ end
16
+ def _display_context
17
+ defined?(@display_context) ? @display_context : true
18
+ end
19
+ end
20
+ attr_reader :input, :opts
21
+
22
+ def initialize(input = nil, opts = {})
23
+ @input = input
24
+ @opts = default_opts.merge(opts)
25
+ set_formatter
26
+ super(formatter.to_s)
27
+ end
28
+
29
+ def problem
30
+ @problem ||= exec_config(self.class.instance_variable_get(:@problem))
31
+ end
32
+ def summary
33
+ @summary ||= exec_config(self.class.instance_variable_get(:@summary))
34
+ end
35
+ def resolution
36
+ @resolution ||= exec_config(self.class.instance_variable_get(:@resolution))
37
+ end
38
+
39
+ def hints?
40
+ !!(problem || summary || resolution)
41
+ end
42
+
43
+ private
44
+
45
+ def set_formatter
46
+ formatter.tap do |f|
47
+ f << Tip.new('Problem:', problem) if problem
48
+ f << Tip.new('Summary:', summary) if summary
49
+ f << Tip.new('Resolution:', resolution) if resolution
50
+ f << Context.new(self, context) if display_context?
51
+ end
52
+ end
53
+
54
+ def default_opts
55
+ {
56
+ display_context: self.class._display_context
57
+ }
58
+ end
59
+ def display_context?
60
+ opts[:display_context]
61
+ end
62
+
63
+ def registry
64
+ Erlash.formatters
65
+ end
66
+
67
+ def formatter
68
+ @formatter ||= Formatter.new(registry)
69
+ end
70
+
71
+ def context
72
+ @context ||= case input
73
+ when Array
74
+ Erlash::MainArray.build(input)
75
+ when Hash
76
+ Erlash::MainHash.build(input)
77
+ else
78
+ input
79
+ end
80
+ end
81
+
82
+ def exec_config(val)
83
+ if val.is_a?(Proc)
84
+ val.call(context)
85
+ else
86
+ val
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,9 @@
1
+ module Erlash
2
+ class Context
3
+ attr_reader :formatter, :value
4
+ def initialize(formatter, value)
5
+ @formatter = formatter
6
+ @value = value
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ module Erlash
2
+ class Formatter
3
+ # def self.call(inst, obj)
4
+ # registry = inst.registry
5
+ # formatter = registry.find(obj.class)
6
+ # if formatter
7
+ # formatter.call(inst, obj)
8
+ # elsif obj.respond_to?(:to_erlash)
9
+ # obj.to_erlash
10
+ # else
11
+ # obj.to_s
12
+ # end
13
+ # end
14
+
15
+ def self.format(inst, obj)
16
+ registry = inst.registry
17
+ formatter = registry.find(obj.class)
18
+ if formatter
19
+ formatter.format(inst, obj)
20
+ else
21
+ obj.to_s
22
+ end
23
+ end
24
+
25
+ attr_reader :registry, :output, :objs, :options
26
+ def initialize(registry, options = {})
27
+ @registry = registry
28
+ @output = StringIO.new
29
+ @objs = []
30
+ @options = options
31
+ end
32
+
33
+ def <<(elem)
34
+ objs << elem
35
+ end
36
+
37
+ def to_s
38
+ return @to_s if defined?(@to_s)
39
+ objs.each do |e|
40
+ output.puts format_elem(e)
41
+ end
42
+ @to_s = output.string
43
+ end
44
+ alias_method :string, :to_s
45
+
46
+ private
47
+
48
+ def format_elem(elem)
49
+ self.class.format(self, elem)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ module Erlash
2
+ class ArrayFormatter < TemplateFormatter
3
+ def format
4
+ elems = object.map{ |e| format_elem(e) }
5
+ "[ #{elems.join(", ")} ]"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ module Erlash
2
+ # Experimental
3
+ class ArrayRecursiveFormatter < TemplateFormatter
4
+ class Nested < Struct.new(:val, :offset); end
5
+
6
+ def format
7
+ recursive_format
8
+ end
9
+
10
+ private
11
+
12
+ def recursive_format
13
+ objs = recursive_nesting(object)
14
+ objs.each_with_object([]) do |n, s|
15
+ if n.val.is_a?(Hash)
16
+ format_elem(n.val).each do |fh|
17
+ s << "#{" "*n.offset}#{fh}"
18
+ end
19
+ else
20
+ s << " #{" "*n.offset} - #{format_elem(n.val)}"
21
+ end
22
+ end.flatten.join("\n")
23
+ end
24
+
25
+ def recursive_nesting(e, acc = [], offset = 0)
26
+ if e.is_a?(Array)
27
+ e.each{ |i| recursive_nesting(i, acc, offset + 1)}
28
+ else
29
+ acc << Nested.new(e, offset)
30
+ end
31
+ acc
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module Erlash
2
+ class ContextFormatter < TemplateFormatter
3
+ def format
4
+ [].tap do |acc|
5
+ acc << 'Context:' if object.formatter.hints?
6
+ acc << format_elem(object.value)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Erlash
2
+ # Experimental
3
+ class HashExperimentalFormatter < TemplateFormatter
4
+ def format
5
+ experimental_formatting
6
+ end
7
+
8
+ def experimental_formatting
9
+ object.each_with_object([]) do |(k, v), s|
10
+ if v.is_a?(Array)
11
+ s << " - #{k}:"
12
+ s << "#{format_elem(v)}"
13
+ else
14
+ s << " - #{k}: #{format_elem(v)}"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Erlash
2
+ # ex:
3
+ # {foo: "foo"} => { foo: `foo` }
4
+ class HashFormatter < TemplateFormatter
5
+ def format
6
+ elems = object.each_with_object([]) do |(k,v), o|
7
+ o << "#{k}: #{format_elem(v)}"
8
+ end
9
+ "{ #{elems.join(", ")} }"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Erlash
2
+ class MainArrayFormatter < TemplateFormatter
3
+ def format
4
+ object.each_with_object([]) do |e, s|
5
+ s << " - #{format_elem(e)}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Erlash
2
+ class MainHashFormatter < TemplateFormatter
3
+ def format
4
+ object.each_with_object([]) do |(k, v), s|
5
+ s << " - #{k}: #{format_elem(v)}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Erlash
2
+ # ex: `string`
3
+ class StringFormatter < TemplateFormatter
4
+ def format
5
+ "`#{object}`"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,49 @@
1
+ module Erlash
2
+ # ex:
3
+ # class HelloFormatter < TemplateFormatter
4
+ # def format
5
+ # object.greetings
6
+ # end
7
+ # end
8
+ class TemplateFormatter
9
+
10
+ def self.call(*args)
11
+ new(*args).safe_call; nil
12
+ end
13
+
14
+ def self.format(*args)
15
+ new(*args).format
16
+ end
17
+
18
+ attr_reader :object, :opts, :output, :formatter
19
+ def initialize(formatter, object, opts = {})
20
+ @object = object
21
+ @opts = opts
22
+ @formatter = formatter
23
+ @output = formatter.output
24
+ @executed = false
25
+ end
26
+
27
+ def format
28
+ raise NotImplementedMethod
29
+ end
30
+
31
+ def safe_call
32
+ return if @executed
33
+ call
34
+ @excuted = true
35
+ end
36
+
37
+ def format_elem(elem)
38
+ Formatter.format(formatter, elem)
39
+ end
40
+
41
+ protected
42
+
43
+ def call
44
+ @call ||= Fusu::Array.wrap(format).each do |e|
45
+ output.puts e
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,10 @@
1
+ module Erlash
2
+ class TipFormatter < TemplateFormatter
3
+ def format
4
+ [].tap do |acc|
5
+ acc << object.title
6
+ acc << " #{object.description}"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module Erlash
2
+ class Formatters
3
+ include Enumerable
4
+
5
+ def find(klass)
6
+ db[klass]
7
+ end
8
+
9
+ def register(klass, val, opts = {})
10
+ db[klass] = val
11
+ end
12
+
13
+ def each(&block)
14
+ db.each(&block)
15
+ end
16
+
17
+ private
18
+
19
+ def db
20
+ @db ||= Fusu::HashWithIndifferentAccess.new
21
+ end
22
+ end
23
+ end
24
+
25
+ # first require template
26
+ require 'erlash/formatters/template_formatter'
27
+ # then the rest of formatters
28
+ Dir[File.expand_path("./formatters", __dir__) + "**/*rb"].each do |f|
29
+ require f
30
+ end
@@ -0,0 +1,9 @@
1
+ module Erlash
2
+ class MainArray < Array
3
+ def self.build(ary)
4
+ ary.each_with_object(self.new) do |e, o|
5
+ o << e
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Erlash
2
+ class MainHash < Hash
3
+ def self.build(hash)
4
+ hash.each_with_object(self.new) do |(k, v), o|
5
+ o[k] = v
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,72 @@
1
+ module Erlash
2
+ module Testing
3
+ class FileAlreadyWrittenError < Erlash::Base
4
+ display_context false
5
+ problem { |context| "File written at: #{context[:filename]}" }
6
+ summary "Review the file and remove second parametter to start testing"
7
+ resolution "expected_message('name'){ |expected| expect(message).to eq(expected) }"
8
+ end
9
+
10
+ class MessageNotRegisteredError < Erlash::Base
11
+ display_context false
12
+ problem { |context| "File does not exists at: #{context[:filename]}" }
13
+ summary "Example file is not yet registered please register content as second parametter"
14
+ resolution "expected_message('name', 'content'){ |expected| expect(message).to eq(expected) }"
15
+ end
16
+ class OverrideFileNotAllowedError < Erlash::Base
17
+ display_context false
18
+ problem { |context| "File already exists at: #{context[:filename]}" }
19
+ summary "Example file already exists if you want to override it please remove the file to create a new one"
20
+ resolution { |context| "Remove second parametter Or rm #{context[:filename]}" }
21
+ end
22
+
23
+ def self.messages_path=(path)
24
+ @messages_path = path
25
+ end
26
+
27
+ def self.messages_path
28
+ @messages_path
29
+ end
30
+
31
+ def erlash_messages_path
32
+ @erlash_messages_path ||= Pathname.new(Erlash::Testing.messages_path)
33
+ end
34
+
35
+ def expected_message(name, message = nil, &block)
36
+ filename = erlash_filename(name)
37
+ file_exists = File.exists?(filename)
38
+ if message
39
+ if file_exists
40
+ raise OverrideFileNotAllowedError, { filename: filename }
41
+ else
42
+ File.open(filename, 'w') { |f| f.write(message) }
43
+ raise FileAlreadyWrittenError, { filename: filename }
44
+ end
45
+ else
46
+ if file_exists
47
+ block.call(File.read(filename))
48
+ else
49
+ raise MessageNotRegisteredError, { filename: filename }
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def erlash_filename(input)
57
+ erlash_messages_path.join("#{input}.error")
58
+ end
59
+
60
+ if defined?(RSpec)
61
+ undef :erlash_filename
62
+ def erlash_filename(input)
63
+ name = if input.is_a?(RSpec::Core::Example)
64
+ input.full_description.gsub("\s", "_")
65
+ else
66
+ input
67
+ end
68
+ erlash_messages_path.join("#{name}.error")
69
+ end
70
+ end
71
+ end
72
+ end
data/lib/erlash/tip.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Erlash
2
+ class Tip < Struct.new(:title, :description); end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Erlash
2
+ VERSION = "0.1.0"
3
+ end
data/lib/erlash.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "erlash/version"
2
+ require "fusu"
3
+
4
+ module Erlash
5
+ class << self
6
+ def formatters
7
+ @formatters ||= Formatters.new
8
+ end
9
+ end
10
+ end
11
+ require 'erlash/tip'
12
+ require 'erlash/context'
13
+ require 'erlash/base'
14
+ require 'erlash/main_array'
15
+ require 'erlash/main_hash'
16
+ require 'erlash/formatter'
17
+ require 'erlash/formatters'
18
+
19
+ Erlash.formatters.tap do |f|
20
+ f.register Array, Erlash::ArrayFormatter
21
+ f.register Hash, Erlash::HashFormatter
22
+ f.register String, Erlash::StringFormatter
23
+ f.register Erlash::Context, Erlash::ContextFormatter
24
+ f.register Erlash::MainArray, Erlash::MainArrayFormatter
25
+ f.register Erlash::MainHash, Erlash::MainHashFormatter
26
+ f.register Erlash::Tip, Erlash::TipFormatter
27
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erlash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fusu
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Error abstraction to simplify the creation of descriptive errors.
70
+ email:
71
+ - arturictus@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - erlash.gemspec
88
+ - lib/erlash.rb
89
+ - lib/erlash/base.rb
90
+ - lib/erlash/context.rb
91
+ - lib/erlash/formatter.rb
92
+ - lib/erlash/formatters.rb
93
+ - lib/erlash/formatters/array_formatter.rb
94
+ - lib/erlash/formatters/array_recursive_formatter.rb
95
+ - lib/erlash/formatters/context_formatter.rb
96
+ - lib/erlash/formatters/hash_experimental_formatter.rb
97
+ - lib/erlash/formatters/hash_formatter.rb
98
+ - lib/erlash/formatters/main_array_formatter.rb
99
+ - lib/erlash/formatters/main_hash_formatter.rb
100
+ - lib/erlash/formatters/string_formatter.rb
101
+ - lib/erlash/formatters/template_formatter.rb
102
+ - lib/erlash/formatters/tip_formatter.rb
103
+ - lib/erlash/main_array.rb
104
+ - lib/erlash/main_hash.rb
105
+ - lib/erlash/testing.rb
106
+ - lib/erlash/tip.rb
107
+ - lib/erlash/version.rb
108
+ homepage: https://github.com/arturictus/erlash
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Handle your errors gracefully.
132
+ test_files: []