riposte 0.0.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc4c3dbdd6f02ea0d480764a7506c80bf808716f
4
- data.tar.gz: b52d3a57a4803993543a76f0cce88d7187fd6f61
3
+ metadata.gz: 4e9cda507520daceedc0086ce4849f3295d6fa2e
4
+ data.tar.gz: 824567160f1b611ed44468ca7555514be2d6cd8c
5
5
  SHA512:
6
- metadata.gz: 7f5970083c0671f3f7023911ac7a6e96ceb20462c017dcaba5888d7b0e9d7088bafd1af3e70f864b076e138fb96cdbe988b3d9c7a72f60c86442ac27c86720ce
7
- data.tar.gz: f7815f999b43a870cdfecf71fe050d2d4605e2472270084126dc047d4f305c67901d1150c6c5d9c46cc1e817d793a0440bf9b86a18224276b2ff4ec4bb121261
6
+ metadata.gz: af45e32761319a78417c5946e140039d60c6d4d627e15ae4db8a7b5299e4d8179c6919f5017b0a6962e141b7bcbefe8deb47968becd6e9ccd540cf4c8dead25e
7
+ data.tar.gz: 3dfd8b6637d685e7ee0f60fed3fa4f0014eef8fb1467b3c95191228db660ff767382809def44cd0675126607680a562b586d93d4ecfc5ae93569eb5d416d3af6
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Riposte
2
2
 
3
- TODO: Write a gem description
3
+ Riposte was put together to scratch an itch that I had when working with
4
+ multiple outcomes of telling an object what to do and then responding
5
+ differently depending on the results of the action. This happens all the
6
+ time in the software that I right. I want to save an object to the
7
+ database and I can't because of the object not meeting the database
8
+ requirements, or the database is down. I might have a whole host of
9
+ different responses for the user. This is where Riposte is geared. It is
10
+ a very small gem, and I hope to keep it that way. Finally after years of
11
+ using similar ideas I've finally made a gem out of it.
4
12
 
5
13
  ## Installation
6
14
 
@@ -20,12 +28,50 @@ Or install it yourself as:
20
28
 
21
29
  ## Usage
22
30
 
23
- TODO: Write usage instructions here
31
+ I'm going to show an example usage within a basic Rails application
32
+ because that is where I've found this to be used the most.
33
+
34
+ ```ruby
35
+ class MyController < ApplicationController
36
+ def create
37
+ @article = Article.new
38
+
39
+ @article.publish { |on|
40
+ on.published { send_email_to_subscribers }
41
+ on.invalid { handle_invalid }
42
+ on.exception { database_down_handler }
43
+ }
44
+
45
+ respond_with @artcile
46
+ end
47
+ end
48
+
49
+ class Article < ActiveRecord::Base
50
+ include Riposte::Helper
51
+
52
+ def publish(&block)
53
+ if valid?
54
+ #publish logic
55
+ react_to :published, &block
56
+ else
57
+ #failure logic
58
+ react_to :invalid, &block
59
+ end
60
+ rescue SomeError => e
61
+ react_to :exception, &block
62
+ end
63
+ end
64
+ ```
65
+
24
66
 
25
67
  ## Contributing
26
68
 
27
- 1. Fork it ( https://github.com/[my-github-username]/riposte/fork )
69
+ 1. Fork it ( https://github.com/adkron/riposte/fork )
28
70
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
71
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
72
  4. Push to the branch (`git push origin my-new-feature`)
31
73
  5. Create a new Pull Request
74
+
75
+ ## License
76
+ Licensed under the MIT license. See the LICENSE.txt file for more
77
+ details.
data/lib/riposte.rb CHANGED
@@ -1,27 +1,6 @@
1
1
  require "riposte/version"
2
+ require "riposte/reaction"
3
+ require "riposte/helper"
2
4
 
3
5
  module Riposte
4
- class Reaction
5
- attr_accessor :type
6
-
7
- def initialize(type)
8
- self.type = type
9
- end
10
-
11
- def method_missing(name, *)
12
- if name == type
13
- yield if block_given?
14
- end
15
- end
16
-
17
- def respond_to_missing?(*)
18
- true
19
- end
20
-
21
- module Helper
22
- def react_to(type, with: ->() {})
23
- with.call Reaction.new(type)
24
- end
25
- end
26
- end
27
6
  end
@@ -0,0 +1,9 @@
1
+ module Riposte
2
+ module Helper
3
+ def react_to(type)
4
+ Reaction.new(type).tap { |reaction|
5
+ yield reaction if block_given?
6
+ }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module Riposte
2
+ class Reaction
3
+ attr_accessor :type
4
+
5
+ def initialize(type)
6
+ self.type = type
7
+ end
8
+
9
+ def method_missing(name, *)
10
+ if name == type
11
+ yield if block_given?
12
+ end
13
+ end
14
+
15
+ def respond_to_missing?(*)
16
+ true
17
+ end
18
+
19
+ def ==(other)
20
+ [self.class, self.type] == [other.class, other.type]
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Riposte
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require "riposte/helper"
2
+
3
+ module Riposte
4
+ describe Helper do
5
+ let(:described_class) {
6
+ Class.new {
7
+ include Riposte::Helper
8
+ }
9
+ }
10
+
11
+ subject { described_class.new }
12
+
13
+ context "#react_to with no block" do
14
+ it do
15
+ expect(subject.react_to(:foo)).to eq(Riposte::Reaction.new(:foo))
16
+ end
17
+ end
18
+
19
+ context "#react_to with a block" do
20
+ it do
21
+ expect {|b| subject.react_to(:foo, &b) }.to yield_with_args(Riposte::Reaction.new(:foo))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,4 +1,4 @@
1
- require "riposte"
1
+ require "riposte/reaction"
2
2
 
3
3
  module Riposte
4
4
  describe Reaction do
@@ -20,5 +20,9 @@ module Riposte
20
20
  subject.response_type { called = true }
21
21
  expect(called).to be_truthy
22
22
  end
23
+
24
+ it "is equal to another reaction with the same type" do
25
+ expect(subject).to eq(described_class.new(:response_type))
26
+ end
23
27
  end
24
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riposte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amos L King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,9 +65,12 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/riposte.rb
68
+ - lib/riposte/helper.rb
69
+ - lib/riposte/reaction.rb
68
70
  - lib/riposte/version.rb
69
71
  - riposte.gemspec
70
- - spec/riposte_spec.rb
72
+ - spec/helper_spec.rb
73
+ - spec/reaction_spec.rb
71
74
  homepage: https://github.com/BinaryNoggin/riposte
72
75
  licenses:
73
76
  - MIT
@@ -93,4 +96,5 @@ signing_key:
93
96
  specification_version: 4
94
97
  summary: A micro-gem for handling different outcomes of an action
95
98
  test_files:
96
- - spec/riposte_spec.rb
99
+ - spec/helper_spec.rb
100
+ - spec/reaction_spec.rb