fanli 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +19 -6
- data/fanli.gemspec +1 -1
- data/lib/fanli/base.rb +5 -5
- data/lib/fanli/version.rb +1 -1
- data/lib/fanli.rb +6 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c21e9111e908bd2f7678fccfce79032590b94757
|
4
|
+
data.tar.gz: 758209c675394a3154dc12ef00340116f2465904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a57f073dc1505d9fa828e207ab54fae8deb4d190ef45201d9d36da1f5fc967e79abb4ea7ae6e8b3675be241372422c3b7bd2511c5a1eb88bd78748a574e7c1a
|
7
|
+
data.tar.gz: 03a0a82fcade8bec83dc58df943bc4c92536515b938de3de892d6e7b6d52041816fb53a22b8d747bf614954b6bf95f13d78b6a794f836d8387a260dc6ded32f2
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -30,7 +30,7 @@ Or install it yourself as:
|
|
30
30
|
> 3. Give $5 cash if he's from Beijing
|
31
31
|
> 4. And many more business rules in the future...
|
32
32
|
|
33
|
-
|
33
|
+
In your rails code, you may implement these rules **ALL** in the `User` model like this:
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
class User < ActiveRecord::Base
|
@@ -57,6 +57,7 @@ class User < ActiveRecord::Base
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
```
|
60
|
+
|
60
61
|
This will result in "Fat Models" that violate the SRP("[Single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle)").
|
61
62
|
|
62
63
|
To solve this problem and prevent messy code, you should write code in "The Fanli Way":
|
@@ -73,7 +74,7 @@ To solve this problem and prevent messy code, you should write code in "The Fanl
|
|
73
74
|
```ruby
|
74
75
|
# app/business/create_user.rb
|
75
76
|
class CreateUser < Fanli::Base
|
76
|
-
def
|
77
|
+
def call(args)
|
77
78
|
User.create!(args)
|
78
79
|
end
|
79
80
|
end
|
@@ -99,7 +100,7 @@ To solve this problem and prevent messy code, you should write code in "The Fanl
|
|
99
100
|
end
|
100
101
|
```
|
101
102
|
|
102
|
-
```
|
103
|
+
```ruby
|
103
104
|
# app/business/give_initial_cash.rb
|
104
105
|
class GiveInitialCash
|
105
106
|
def self.on_create_user(args)
|
@@ -111,14 +112,26 @@ To solve this problem and prevent messy code, you should write code in "The Fanl
|
|
111
112
|
4. Register each business logic class:
|
112
113
|
|
113
114
|
```ruby
|
114
|
-
#
|
115
|
-
Fanli.
|
115
|
+
# app/business/registry.rb
|
116
|
+
Fanli.when(:create_user, exec: [
|
116
117
|
SendWelcomeEmail,
|
117
118
|
GiveCouponIfXmas,
|
118
119
|
GiveInitialCash
|
119
120
|
])
|
120
121
|
```
|
121
|
-
|
122
|
+
|
123
|
+
5. Use your business processing class in your controller:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
# app/controllers/users_controller.rb
|
127
|
+
class UsersController < ApplicationController
|
128
|
+
def create
|
129
|
+
CreateUser.with(params.require(:user).permit(:name, :email))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
6. Done! Enjoy!
|
122
135
|
|
123
136
|
## Development
|
124
137
|
|
data/fanli.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["David Zhang"]
|
10
10
|
spec.email = ["daqing1986@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{Fanli helps you doing business (logic)}
|
12
|
+
spec.summary = %q{Fanli helps you doing **complex** business (logic)}
|
13
13
|
spec.homepage = "http://github.com/daqing/fanli"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/fanli/base.rb
CHANGED
@@ -4,15 +4,15 @@ module Fanli
|
|
4
4
|
sub.extend(ClassMethods)
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
throw(:
|
7
|
+
def call(*_args)
|
8
|
+
throw(:please_create_your_own_call_method)
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def
|
12
|
+
def with(opts)
|
13
13
|
obj = new
|
14
|
-
obj.
|
15
|
-
Fanli.
|
14
|
+
obj.call(opts)
|
15
|
+
Fanli.trigger(snikize(name), with: opts)
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
data/lib/fanli/version.rb
CHANGED
data/lib/fanli.rb
CHANGED
@@ -5,18 +5,18 @@ require 'fanli/base'
|
|
5
5
|
module Fanli
|
6
6
|
@registry = {}
|
7
7
|
|
8
|
-
def self.
|
9
|
-
if
|
10
|
-
|
8
|
+
def self.when(ev, exec:)
|
9
|
+
if exec.respond_to?(:each)
|
10
|
+
exec.each do |listener|
|
11
11
|
add_listener(@registry, ev.to_sym, listener)
|
12
12
|
end
|
13
13
|
else
|
14
|
-
add_listener(@registry, ev.to_sym,
|
14
|
+
add_listener(@registry, ev.to_sym, exec)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.
|
19
|
-
apply_callback("on_#{ev}",
|
18
|
+
def self.trigger(ev, with:)
|
19
|
+
apply_callback("on_#{ev}", with, @registry[ev.to_sym])
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.reset
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fanli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Zhang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -112,5 +112,5 @@ rubyforge_project:
|
|
112
112
|
rubygems_version: 2.6.11
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
|
-
summary: Fanli helps you doing business (logic)
|
115
|
+
summary: Fanli helps you doing **complex** business (logic)
|
116
116
|
test_files: []
|