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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a443bfa0dd9e8f717bf346d5d27579c26c217454
4
- data.tar.gz: e89db1278142a0005795374d4c0f68dbe12cb9a2
3
+ metadata.gz: c21e9111e908bd2f7678fccfce79032590b94757
4
+ data.tar.gz: 758209c675394a3154dc12ef00340116f2465904
5
5
  SHA512:
6
- metadata.gz: 79f34296eafc6cccd14438dcb09c00f28249cafb06775323ce785865334e44474ec0f2195bfdb4bc00134dc18982410c748e85787a087b22758ee6ecb9c85136
7
- data.tar.gz: d415c741ac7d660435bd0c3bb550a497e3f9c02ec334398313d51ac0c9f1ab7935279876be495e6bc4ac2be3a9235c2bfef8f56a0b934a5916f8e1d20d39308b
6
+ metadata.gz: 8a57f073dc1505d9fa828e207ab54fae8deb4d190ef45201d9d36da1f5fc967e79abb4ea7ae6e8b3675be241372422c3b7bd2511c5a1eb88bd78748a574e7c1a
7
+ data.tar.gz: 03a0a82fcade8bec83dc58df943bc4c92536515b938de3de892d6e7b6d52041816fb53a22b8d747bf614954b6bf95f13d78b6a794f836d8387a260dc6ded32f2
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ ruby '2.4.1'
4
+
3
5
  # Specify your gem's dependencies in fanli.gemspec
4
6
  gemspec
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
- Following "The Rails Way", you may implement these rules **ALL** in the `User` model like this:
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 perform(args)
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
- # config/initializers/business.rb
115
- Fanli.on(:create_user, [
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
- 5. Done! Enjoy!
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 perform(*_args)
8
- throw(:please_create_your_own_perform_method)
7
+ def call(*_args)
8
+ throw(:please_create_your_own_call_method)
9
9
  end
10
10
 
11
11
  module ClassMethods
12
- def do(opts)
12
+ def with(opts)
13
13
  obj = new
14
- obj.perform(opts)
15
- Fanli.notify(snikize(name), opts)
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
@@ -1,3 +1,3 @@
1
1
  module Fanli
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/fanli.rb CHANGED
@@ -5,18 +5,18 @@ require 'fanli/base'
5
5
  module Fanli
6
6
  @registry = {}
7
7
 
8
- def self.on(ev, listener_or_collection)
9
- if listener_or_collection.respond_to?(:each)
10
- listener_or_collection.map do |listener|
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, listener_or_collection)
14
+ add_listener(@registry, ev.to_sym, exec)
15
15
  end
16
16
  end
17
17
 
18
- def self.notify(ev, args)
19
- apply_callback("on_#{ev}", args, @registry[ev.to_sym])
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.1.2
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-05 00:00:00.000000000 Z
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: []