cases 1.0.0

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: d1ff1b176f95aee6273a5c6430aeb8526556d86d
4
+ data.tar.gz: d000af86cf1971ae3eab07daa4eabc1d4efc9d47
5
+ SHA512:
6
+ metadata.gz: 012aa1a1164f252d9acb9005f3de8fb76cdf2f00cb019216ea3fa09f5ac561fcfaece33eb3efe3fbda85335a8eb8bbf23313ab138790a9292e236e5b4df49205
7
+ data.tar.gz: 3c3d4398e97b29311a030d305cb15a7c24924919e2e9670febdaf1d52743dbf346c06bf2b874c733ee9d59f142aed8cf944d95c556611b7019446fe02a7a7f1e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+
8
+ script: 'bundle exec rake'
9
+
10
+ notifications:
11
+ email:
12
+ recipients:
13
+ - morganshowman@gmail.com
14
+ on_failure: change
15
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cases.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ guard 'rspec' do
2
+ watch(%r{^lib/(.+).rb$}) do |m|
3
+ "spec/#{m[1]}_spec.rb"
4
+ end
5
+
6
+ watch(%r{^spec/(.+).rb$}) do |m|
7
+ "spec/#{m[1]}.rb"
8
+ end
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Morgan Showman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Cases
2
+
3
+ Define case, and caseable callbacks for your methods in ruby. By defining cases
4
+ you can add callbacks to your methods where different callbacks will happen
5
+ based on the result of the method.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'cases'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cases
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ class TestResponse
25
+ attr_reader :options
26
+
27
+ def initialize(options)
28
+ @options = options
29
+ end
30
+
31
+ def failed?
32
+ options[:failed]
33
+ end
34
+
35
+ def failed_message
36
+ "failed message from test response"
37
+ end
38
+
39
+ def success?
40
+ options[:success]
41
+ end
42
+
43
+ def success_message
44
+ "success message from test response"
45
+ end
46
+ end
47
+
48
+ class TestCases
49
+ include Cases
50
+
51
+ attr_reader :response
52
+
53
+ def initialize(response)
54
+ @response = response
55
+ end
56
+
57
+ def failed_message
58
+ "failed message from cased object"
59
+ end
60
+
61
+ def success_message
62
+ "success message from cased object"
63
+ end
64
+
65
+ def test_with_block
66
+ response
67
+ end
68
+ define_case(:test_with_block, :success?) { |response| response.success_message }
69
+ define_case(:test_with_block, :failed?) { |response| response.failed_message }
70
+
71
+ def test_without_block
72
+ response
73
+ end
74
+ define_case(:test_without_block, success?: :success_message)
75
+ define_case(:test_without_block, failed?: :failed_message)
76
+
77
+ def test_caseable
78
+ response
79
+ end
80
+ define_caseable(:test_caseable)
81
+ end
82
+
83
+ > # When the test response was a success
84
+ > test_response = TestResponse.new(success: true, failed: false)
85
+ > test_case = TestCases.new(test_response)
86
+ > test_case.test_with_block
87
+ => "success message from test response"
88
+ > test_case.test_without_block
89
+ => "success message from cased object"
90
+ > test_case.test_caseable do |on|
91
+ on.success? { |response| "#{response.success_message} through caseable"
92
+ on.failed? { |response| "#{response.failed_message} through caseable"
93
+ end
94
+ => "success message from test response through caseable"
95
+ >
96
+ > # When the test response failed
97
+ > test_response = TestResponse.new(success: false, failed: true)
98
+ > test_case = TestCases.new(test_response)
99
+ > test_case.test_with_block
100
+ => "failed message from test response"
101
+ > test_case.test_without_block
102
+ => "failed message from cased object"
103
+ > test_case.test_caseable do |on|
104
+ on.success? { |response| "#{response.success_message} through caseable"
105
+ on.failed? { |response| "#{response.failed_message} through caseable"
106
+ end
107
+ => "failed message from test response through caseable"
108
+ ```
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it ( http://github.com/MorganShowman/cases/fork )
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require "bundler/gem_tasks"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color', '--format', 'documentation']
6
+ end
7
+
8
+ task default: :spec
data/cases.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cases/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cases"
8
+ spec.version = Cases::VERSION
9
+ spec.authors = ["Morgan Showman"]
10
+ spec.email = ["mshowman@squaremouth.com"]
11
+ spec.summary = %q{Define cases on methods that execute different callbacks for different scenarios}
12
+ spec.description = %q{Define case, and caseable callbacks for your methods in ruby. By defining cases you can add callbacks
13
+ to your methods where different callbacks will happen based on the result of the method}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "method_callbacks"
28
+ end
data/lib/cases/case.rb ADDED
@@ -0,0 +1,44 @@
1
+ module Cases
2
+ class Case
3
+ attr_accessor :result
4
+ attr_reader :method, :event, :options, :block
5
+
6
+ def self.all
7
+ @_all ||= {}
8
+ end
9
+
10
+ def initialize(method, event, options = {}, &block)
11
+ @method = method
12
+ @event = event
13
+ @block = block
14
+ @options = options
15
+
16
+ register
17
+ end
18
+
19
+ def ==(other)
20
+ method == other.method &&
21
+ event == other.event
22
+ end
23
+
24
+ def execute(object, result)
25
+ return if !result.send(event)
26
+
27
+ self.result = block.call(options[:on_self] ? object : result)
28
+ end
29
+
30
+ private
31
+
32
+ def register
33
+ cases << self if !registered?
34
+ end
35
+
36
+ def registered?
37
+ cases.any? { |kase| kase == self }
38
+ end
39
+
40
+ def cases
41
+ self.class.all[method] ||= []
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ class Caseable
2
+ attr_reader :object
3
+
4
+ def self.execute(result, &block)
5
+ new(result).execute(result, &block)
6
+ end
7
+
8
+ def initialize(object)
9
+ @object = object
10
+ end
11
+
12
+ def execute(original_result, &block)
13
+ block.call(self)
14
+ case_blocks.reduce(original_result) { |result, case_block| case_block.call(result) }
15
+ end
16
+
17
+ def method_missing(method, &block)
18
+ case_blocks << block if object.send(method)
19
+ case_blocks
20
+ end
21
+
22
+ private
23
+
24
+ def case_blocks
25
+ @_case_blocks ||= []
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Cases
2
+ VERSION = "1.0.0"
3
+ end
data/lib/cases.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "method_callbacks"
2
+ require "cases/case"
3
+ require "cases/caseable"
4
+ require "cases/version"
5
+
6
+ module Cases
7
+ def self.included(base)
8
+ base.extend(MethodCallbacks::ClassMethods)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def define_case(method, cases)
14
+ if block_given?
15
+ define_case_with_block(method, cases, &Proc.new)
16
+ else
17
+ cases.each_pair do |event, action|
18
+ Case.new(method, event, { on_self: true }, &Proc.new { |object| object.send(action) })
19
+ end
20
+ end
21
+
22
+ define_execute_cases(method)
23
+ end
24
+
25
+ def define_caseable(method)
26
+ proxy_result(method) { |result, &block| Caseable.execute(result, &block) }
27
+ end
28
+
29
+ def cases
30
+ Case.all
31
+ end
32
+
33
+ private
34
+
35
+ def define_case_with_block(method, event, &block)
36
+ Case.new(method, event, &block)
37
+ end
38
+
39
+ def define_execute_cases(method)
40
+ define_method "execute_#{method}_cases" do |&block|
41
+ self.class.cases[method].find { |kase| kase.execute(self, block.call) }.result
42
+ end
43
+
44
+ around_method(method, "execute_#{method}_cases")
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,104 @@
1
+ require "spec_helper"
2
+
3
+ describe Cases do
4
+ context "when response is success" do
5
+ let(:message) { "success response" }
6
+ let(:response) { TestResponse.new(success: true, failed: false) }
7
+ let(:test_cases) { TestCases.new(response) }
8
+
9
+ it "should respond from test response" do
10
+ expect(test_cases.test_with_block).to eq("success message from test response")
11
+ end
12
+
13
+ it "should respond from cased object" do
14
+ expect(test_cases.test_without_block).to eq("success message from cased object")
15
+ end
16
+
17
+ it "should respond from the given block" do
18
+ expect(test_cases.test_caseable do |on|
19
+ on.success? { |object| "#{object.success_message} through caseable" }
20
+ on.failed? { |object| "#{object.failed_message} through caseable" }
21
+ end).to eq("success message from test response through caseable")
22
+ end
23
+ end
24
+
25
+ context "when response is failed" do
26
+ let(:message) { "failed response" }
27
+ let(:response) { TestResponse.new(success: false, failed: true) }
28
+ let(:test_cases) { TestCases.new(response) }
29
+
30
+ it "should respond from test response" do
31
+ expect(test_cases.test_with_block).to eq("failed message from test response")
32
+ end
33
+
34
+ it "should respond from cased object" do
35
+ expect(test_cases.test_without_block).to eq("failed message from cased object")
36
+ end
37
+
38
+ it "should respond from the given block" do
39
+ expect(test_cases.test_caseable do |on|
40
+ on.success? { |object| "#{object.success_message} through caseable" }
41
+ on.failed? { |object| "#{object.failed_message} through caseable" }
42
+ end).to eq("failed message from test response through caseable")
43
+ end
44
+ end
45
+ end
46
+
47
+ class TestResponse
48
+ attr_reader :options
49
+
50
+ def initialize(options)
51
+ @options = options
52
+ end
53
+
54
+ def failed?
55
+ options[:failed]
56
+ end
57
+
58
+ def failed_message
59
+ "failed message from test response"
60
+ end
61
+
62
+ def success?
63
+ options[:success]
64
+ end
65
+
66
+ def success_message
67
+ "success message from test response"
68
+ end
69
+ end
70
+
71
+ class TestCases
72
+ include Cases
73
+
74
+ attr_reader :response
75
+
76
+ def initialize(response)
77
+ @response = response
78
+ end
79
+
80
+ def failed_message
81
+ "failed message from cased object"
82
+ end
83
+
84
+ def success_message
85
+ "success message from cased object"
86
+ end
87
+
88
+ def test_with_block
89
+ response
90
+ end
91
+ define_case(:test_with_block, :success?) { |response| response.success_message }
92
+ define_case(:test_with_block, :failed?) { |response| response.failed_message }
93
+
94
+ def test_without_block
95
+ response
96
+ end
97
+ define_case(:test_without_block, success?: :success_message)
98
+ define_case(:test_without_block, failed?: :failed_message)
99
+
100
+ def test_caseable
101
+ response
102
+ end
103
+ define_caseable(:test_caseable)
104
+ end
@@ -0,0 +1 @@
1
+ require "cases"
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cases
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Morgan Showman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ version_requirements: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '1.5'
19
+ prerelease: false
20
+ name: bundler
21
+ requirement: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: '1.5'
26
+ type: :development
27
+ - !ruby/object:Gem::Dependency
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ prerelease: false
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ prerelease: false
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ prerelease: false
62
+ name: guard
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ prerelease: false
76
+ name: guard-rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ - !ruby/object:Gem::Dependency
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ prerelease: false
90
+ name: method_callbacks
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ description: |-
98
+ Define case, and caseable callbacks for your methods in ruby. By defining cases you can add callbacks
99
+ to your methods where different callbacks will happen based on the result of the method
100
+ email:
101
+ - mshowman@squaremouth.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - .travis.yml
108
+ - Gemfile
109
+ - Guardfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - cases.gemspec
114
+ - lib/cases.rb
115
+ - lib/cases/case.rb
116
+ - lib/cases/caseable.rb
117
+ - lib/cases/version.rb
118
+ - spec/cases_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Define cases on methods that execute different callbacks for different scenarios
144
+ test_files:
145
+ - spec/cases_spec.rb
146
+ - spec/spec_helper.rb