confident_ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f02a5207d69e5ec3aeccccc3ef3984b474b62d51
4
- data.tar.gz: 9c5e15fed060781bb3389a48828de782b5756ed6
3
+ metadata.gz: 5541c0988788cd2a99c4b02e4e085b316b051e01
4
+ data.tar.gz: 1d1f94e86a664e9527608d3947ef05b8ac0be7c2
5
5
  SHA512:
6
- metadata.gz: a2d015875987eb01a9e87538b466714ff5917c9bc4d920ccc10d8e4141dfcdd48796fa64afbab1a03f5e6171be36d285058153ce9919857d066f9f80414435e3
7
- data.tar.gz: c477da18f1db3a8d3ea3bb14af91fc37e846bc4ea5813a052e113678ab6c45c53a8bdc36bfcb0d44380157e1565228c56e41ec71029bc489e957b59c0ba10251
6
+ metadata.gz: 66a53a390ccfde96427c2cc79816f6bb13c21c8658dc777e6a74eabf00ff3ff60e2f214a28fb5291c082f062a05da8b475bc1d1c129f4d66b8d039399e0504e0
7
+ data.tar.gz: 36ad69c4fffca4ee6e231988610370c24271bfe60dd7ec8c65c2145fdac273849329f9689e94a601da848adccebf081cdbb62fc51b7d8fc2f64269d5c63fc945
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
8
+ - jruby-18mode
9
+ - jruby-19mode
10
+ - rbx
11
+
12
+ script: bundle exec rspec
13
+ bundler_args: --without development
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in confident.ruby.gemspec
4
4
  gemspec
5
+
6
+ gem 'rspec'
data/README.md CHANGED
@@ -22,8 +22,14 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ ```ruby
26
+ require "confident"
27
+ ```
28
+
25
29
  ### Eliminating condition
26
30
 
31
+ *Not implemented*
32
+
27
33
  Given this code:
28
34
 
29
35
  ```ruby
@@ -75,6 +81,8 @@ end
75
81
 
76
82
  ### Eliminating switch
77
83
 
84
+ *Not implemented*
85
+
78
86
  Given this code:
79
87
 
80
88
  ```ruby
@@ -151,10 +159,46 @@ end
151
159
 
152
160
  Looks like we expanding our code and making it bigger at no time, but it is actually beneficial to us, because we don't need to duplicate knowledge on how to determine `kind` of product throughout a codebase, we just call `Product.lift(kind_string_value)`, and we are done with it. You can implement this stuff in raw ruby easily, but this library wants to cut of a boilerplate code involved in this usually.
153
161
 
162
+ ### Handling success/failure result
163
+
164
+ Just use `Confident::Result`:
165
+
166
+ ```ruby
167
+ def some_computation_that_may_fail
168
+ Confident::Result.ok(compute)
169
+ rescue => e
170
+ Confident::Result.error(e.to_s)
171
+ end
172
+
173
+ puts some_computation_that_may_fail.on_error { |e| report_error(e) }.unwrap
174
+ ```
175
+
176
+ If you have something that returns `true` in case of success and `false` in case of failure, you can use `Confident::Result.from_condition(boolean_value, failure_message=nil)`:
177
+
178
+ ```ruby
179
+ Confident::Result.from_condition(some_weird_external_api, "Weird external API returned unexpected error")
180
+ ```
181
+
182
+ ### Pure null object
183
+
184
+ Pure null object (`Confident::NullObject`) quacks to any method with itself.
185
+
186
+ If you have an input value that could be `nil`, you can just wrap it in `Confident::AutoNullObject` to get either original value or pure null object if it is `nil`.
187
+ Example usage with `AutoNullObject`:
188
+
189
+ ```ruby
190
+ def do_something_with(possibly_nil_value)
191
+ AutoNullObject(some_possibly_nil_value).say("hello")
192
+ end
193
+
194
+ do_something_with(fetch_me_a_value) # => "said: hello"
195
+ do_something_with(fetch_me_a_nil_value) # => #<Confident::NullObject:0x000001019f37f8>
196
+ ```
197
+
154
198
  ### TODO (to specify in this README)
155
199
 
156
200
  - lift & bind
157
- - null object
201
+ - null object with ability to define your own custom null object
158
202
  - barricade
159
203
 
160
204
  ## Contributing
data/lib/confident.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "confident/version"
2
+ require "confident/result"
3
+ require "confident/null_object"
2
4
 
3
5
  module Confident
4
6
  # Your code goes here...
@@ -0,0 +1,18 @@
1
+ module Confident
2
+ class NullObject
3
+ def method_missing(*)
4
+ self
5
+ end
6
+ end
7
+
8
+ module AutoNullObject
9
+ class << self
10
+ def wrap(value)
11
+ return value unless NilClass === value
12
+ NullObject.new
13
+ end
14
+
15
+ alias_method :[], :wrap
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,71 @@
1
+ module Confident
2
+ class Result
3
+ DEFAULT_MISSING_ERROR_HANDLER_MESSAGE = "You haven't specified error handler for Confident::Result, use #on_error for that"
4
+ DEFAULT_FAILURE_MESSAGE = "Condition should be true"
5
+
6
+ def initialize(value)
7
+ @value = value
8
+ end
9
+
10
+ def unwrap
11
+ unless error_handler
12
+ raise MissingErrorHandler, DEFAULT_MISSING_ERROR_HANDLER_MESSAGE
13
+ end
14
+ end
15
+
16
+ def on_error(&error_handler)
17
+ @error_handler = error_handler
18
+ self
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :value, :error_handler
24
+
25
+ class << self
26
+ def ok(value=nil)
27
+ Ok[value]
28
+ end
29
+
30
+ def error(value=nil)
31
+ Error[value]
32
+ end
33
+
34
+ def from_condition(condition_value, failure_message=DEFAULT_FAILURE_MESSAGE)
35
+ condition_value ? ok : error(failure_message)
36
+ end
37
+
38
+ def inherited(subclass)
39
+ class << subclass; public :new, :[] end
40
+ end
41
+
42
+ alias_method :[], :new
43
+
44
+ private :new, :[]
45
+ end
46
+
47
+ class Ok < self
48
+ def ok?
49
+ true
50
+ end
51
+
52
+ def unwrap
53
+ super
54
+ value
55
+ end
56
+ end
57
+
58
+ class Error < self
59
+ def ok?
60
+ false
61
+ end
62
+
63
+ def unwrap
64
+ super
65
+ error_handler.call(value)
66
+ end
67
+ end
68
+
69
+ class MissingErrorHandler < ArgumentError; end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module Confident
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,29 @@
1
+ module Confident
2
+ RSpec.describe NullObject do
3
+ it "quacks with self" do
4
+ expect(subject.quack).to be(subject)
5
+ end
6
+ end
7
+
8
+ RSpec.describe AutoNullObject do
9
+ describe "#[]" do
10
+ subject { AutoNullObject[value] }
11
+
12
+ context "when value is not nil" do
13
+ let(:value) { double("A value") }
14
+
15
+ it "returns original value" do
16
+ is_expected.to be(value)
17
+ end
18
+ end
19
+
20
+ context "when value is nil" do
21
+ let(:value) { nil }
22
+
23
+ it "returns NullObject" do
24
+ is_expected.to be_a(NullObject)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,114 @@
1
+ module Confident
2
+ RSpec.describe Result do
3
+ let(:value) { double("Value") }
4
+ let(:error) { double("Error object") }
5
+
6
+ describe ".new" do
7
+ it "can't be called" do
8
+ expect { Result.new(value) }.to raise_error(NoMethodError)
9
+ end
10
+
11
+ it "can't be called in :[] form" do
12
+ expect { Result[value] }.to raise_error(NoMethodError)
13
+ end
14
+ end
15
+
16
+ describe ".from_condition" do
17
+ subject { Result.from_condition(condition_value) }
18
+
19
+ context "when the condition is true" do
20
+ let(:condition_value) { true }
21
+
22
+ it "returns Result::Ok" do
23
+ expect(subject).to be_ok
24
+ end
25
+ end
26
+
27
+ context "when the condition is false" do
28
+ let(:condition_value) { false }
29
+
30
+ it "returns Result::Error" do
31
+ expect(subject.on_error { |e| [:report, e] }.unwrap).to eq([:report, Confident::Result::DEFAULT_FAILURE_MESSAGE])
32
+ end
33
+
34
+ context "when failure_message is provided" do
35
+ subject { Result.from_condition(condition_value, failure_message) }
36
+ let(:failure_message) { "Special failure message" }
37
+
38
+ it "return Result::Error with provided failure message" do
39
+ expect(subject.on_error { |e| [:report, e] }.unwrap).to eq([:report, failure_message])
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ describe ".ok" do
46
+ subject { Result.ok(value) }
47
+
48
+ it "returns instance of Result::Ok" do
49
+ is_expected.to be_a(Result::Ok)
50
+ end
51
+ end
52
+
53
+ describe ".error" do
54
+ subject { Result.error(error) }
55
+
56
+ it "returns instance of Result::Error" do
57
+ is_expected.to be_a(Result::Error)
58
+ end
59
+ end
60
+
61
+ describe Result::Ok do
62
+ subject { Result.ok(value) }
63
+
64
+ describe "#ok?" do
65
+ it "is true" do
66
+ expect(subject.ok?).to be_truthy
67
+ end
68
+ end
69
+
70
+ describe "#unwrap" do
71
+ context "when error callback was not provided" do
72
+ it "raises error" do
73
+ expect { subject.unwrap }.to raise_error(Result::MissingErrorHandler)
74
+ end
75
+ end
76
+
77
+ context "when error callback is provided" do
78
+ subject { super().on_error { |e| :do_nothing } }
79
+
80
+ it "returns a wrapped value" do
81
+ expect(subject.unwrap).to eq(value)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ describe Result::Error do
88
+ subject { Result.error(error) }
89
+
90
+ describe "#ok?" do
91
+ it "is false" do
92
+ expect(subject.ok?).to be_falsey
93
+ end
94
+ end
95
+
96
+ describe "#unwrap" do
97
+ context "when error callback is not provided" do
98
+ it "raises error" do
99
+ expect { subject.unwrap }.to raise_error(Result::MissingErrorHandler)
100
+ end
101
+ end
102
+
103
+ context "when error callback is provided" do
104
+ subject { super().on_error { |e| [:report, e] } }
105
+
106
+ it "executes callback and returns its result" do
107
+ expect(subject.unwrap).to eq([:report, error])
108
+ end
109
+ end
110
+ end
111
+
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,87 @@
1
+ require 'confident'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # These two settings work together to allow you to limit a spec run
44
+ # to individual examples or groups you care about by tagging them with
45
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
46
+ # get run.
47
+ config.filter_run :focus
48
+ config.run_all_when_everything_filtered = true
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
51
+ # For more details, see:
52
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
53
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
55
+ config.disable_monkey_patching!
56
+
57
+ # This setting enables warnings. It's recommended, but in some cases may
58
+ # be too noisy due to issues in dependencies.
59
+ config.warnings = true
60
+
61
+ # Many RSpec users commonly either run the entire suite or an individual
62
+ # file, and it's useful to allow more verbose output when running an
63
+ # individual spec file.
64
+ if config.files_to_run.one?
65
+ # Use the documentation formatter for detailed output,
66
+ # unless a formatter has already been configured
67
+ # (e.g. via a command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the
72
+ # end of the spec run, to help surface which specs are running
73
+ # particularly slow.
74
+ config.profile_examples = 10
75
+
76
+ # Run specs in random order to surface order dependencies. If you find an
77
+ # order dependency and want to debug it, you can fix the order by providing
78
+ # the seed, which is printed after each run.
79
+ # --seed 1234
80
+ config.order = :random
81
+
82
+ # Seed global randomization in this process using the `--seed` CLI option.
83
+ # Setting this allows you to use `--seed` to deterministically reproduce
84
+ # test failures related to randomization by passing the same `--seed` value
85
+ # as the one that triggered the failure.
86
+ Kernel.srand config.seed
87
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confident_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Fedorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-18 00:00:00.000000000 Z
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,13 +48,20 @@ extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - ".gitignore"
51
+ - ".rspec"
52
+ - ".travis.yml"
51
53
  - Gemfile
52
54
  - LICENSE.txt
53
55
  - README.md
54
56
  - Rakefile
55
57
  - confident_ruby.gemspec
56
58
  - lib/confident.rb
59
+ - lib/confident/null_object.rb
60
+ - lib/confident/result.rb
57
61
  - lib/confident/version.rb
62
+ - spec/confident/null_object_spec.rb
63
+ - spec/confident/result_spec.rb
64
+ - spec/spec_helper.rb
58
65
  homepage: https://github.com/waterlink/confident.ruby
59
66
  licenses:
60
67
  - MIT
@@ -79,4 +86,7 @@ rubygems_version: 2.2.2
79
86
  signing_key:
80
87
  specification_version: 4
81
88
  summary: Be confident and narrative when writing code in ruby.
82
- test_files: []
89
+ test_files:
90
+ - spec/confident/null_object_spec.rb
91
+ - spec/confident/result_spec.rb
92
+ - spec/spec_helper.rb