service_packer 0.0.3 → 0.0.4

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: a48cadb9c06121cad55e7defce079a7eb8faabb5
4
- data.tar.gz: 256bb20740c5c0bc3cfc4d81cb9925f6bd90f9fa
3
+ metadata.gz: b5310639a234a333d6cd85d7b07b40a7ecf462df
4
+ data.tar.gz: 26b01d245ecaf5d445bbf57a9eafb98deec1a32f
5
5
  SHA512:
6
- metadata.gz: 8f86457756e5ad34df821b97b68484d9206a2e576aff7b7b3bc2cfff1a411317783aa92c5882911e21653af00f988378b2c9046c27cb526676afc0d7b8e893ac
7
- data.tar.gz: 3a15027ac465272fded1ff879e6f32971c133e331b26738ea3fb8ebc5b2b7edffc08477ff5f322dd90528b846f533c74fc60038672db016ef5999cb5ec4e8957
6
+ metadata.gz: 67e612e99136ea39627cd22ff7e724b4468de3f72f03d199b85be434d4277f260159539ee10c0af6e8a8896f09131d9ccebc2c09e9c0733e8c5597fd2b48e7b3
7
+ data.tar.gz: 89cb27a4a4847daa55dd2ecdcdcda52d1d505c038138b68cf5f87abca722ea657a63f1d642255632c1277af3f67a3b7d068d242ee906b47f6711099426750e24
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/FX-HAO/service_packer.svg?branch=master)](https://travis-ci.org/FX-HAO/service_packer)
4
4
  [![Gem Version](https://badge.fury.io/rb/service_packer.svg)](https://badge.fury.io/rb/service_packer)
5
+ [![Dependency Status](https://gemnasium.com/badges/github.com/FX-HAO/service_packer.svg)](https://gemnasium.com/github.com/FX-HAO/service_packer)
5
6
 
6
7
  This gem is for decoupling your logic from models, some actions in a system warrant a Service Object to encapsulate their operation(e.g. notifications, emails, feedbacks, etc).
7
8
  Service packer provides a series of methods to standardize the process of many common behaviors.
@@ -27,10 +28,12 @@ Or install it yourself as:
27
28
  Here is a list with all the available behaviors, listed in the same order in which they will get called during the respective operations.
28
29
 
29
30
  **The operation life cycle**
30
-
31
+
32
+ - common_validation
31
33
  - validation
32
34
  - action
33
35
  - callback
36
+ - common_callback
34
37
 
35
38
  A simple example:
36
39
 
@@ -40,7 +43,15 @@ class Service < ServicePacker::Base
40
43
  def initialize(ride)
41
44
  @ride = ride
42
45
  end
43
-
46
+
47
+ def setup_common_validation
48
+ ride.valid?
49
+ end
50
+
51
+ def setup_common_callback
52
+ logger.info ride.to_json
53
+ end
54
+
44
55
  setup :send, :params => [:driver] do
45
56
  validation_define do
46
57
  unless ride.may_send?
@@ -57,6 +68,19 @@ class Service < ServicePacker::Base
57
68
  Notifier.send_to(driver, "New ride")
58
69
  end
59
70
  end
71
+
72
+ setup :cancel do
73
+ validation_define do
74
+ unless ride.may_cancel?
75
+ ride.errors.add(:base, :cannot_cancel)
76
+ end
77
+ ride
78
+ end
79
+
80
+ action_define do
81
+ ride.cancel!
82
+ end
83
+ end
60
84
 
61
85
  attr_reader :ride
62
86
 
@@ -47,6 +47,16 @@ module ServicePacker
47
47
  service.invoke(self, method_name, params, *args)
48
48
  end
49
49
  end
50
+
51
+ def setup_common_validation(&block)
52
+ service = service_packer
53
+ service.common_validation_define(&block)
54
+ end
55
+
56
+ def setup_common_callback(&block)
57
+ service = service_packer
58
+ service.common_callback_define(&block)
59
+ end
50
60
  end
51
61
 
52
62
  extend ClassMethods
@@ -4,13 +4,18 @@ module ServicePacker::Core
4
4
  @name = name
5
5
  @args = args
6
6
  @block = block
7
+ @result = false
7
8
  end
8
9
 
9
10
  def invoke(record, *args)
10
- record.instance_exec(*args, &block)
11
+ result = record.instance_exec(*args, &block)
12
+ if (result.respond_to?(:errors) && result.errors.empty?) || result == true
13
+ @result = result
14
+ end
15
+ return result
11
16
  end
12
17
 
13
- attr_reader :name, :args, :block
18
+ attr_reader :name, :args, :block, :result
14
19
 
15
20
  end
16
21
  end
@@ -1,6 +1,8 @@
1
1
  module ServicePacker
2
2
  class Service
3
3
  def initialize
4
+ common_validation_define { true }
5
+ common_callback_define { true }
4
6
  @validations = {}
5
7
  @actions = {}
6
8
  @callbacks = {}
@@ -18,6 +20,14 @@ module ServicePacker
18
20
  @method_name = nil
19
21
  end
20
22
 
23
+ def common_validation_define(*args, &block)
24
+ @common_validation = ServicePacker::Core::Validation.new("common_validation", *args, &block)
25
+ end
26
+
27
+ def common_callback_define(*args, &block)
28
+ @common_callback = ServicePacker::Core::Callback.new("common_callback", *args, &block)
29
+ end
30
+
21
31
  def validation_define(*args, &block)
22
32
  add_validation(@method_name, *args, &block)
23
33
  end
@@ -39,11 +49,13 @@ module ServicePacker
39
49
  end
40
50
 
41
51
  def process(record, name, *args)
42
- validation_result = @validations[name].invoke(record, *args)
52
+ validation_result = @common_validation.invoke(record, *args) && @common_validation.result &&
53
+ @validations[name].invoke(record, *args) && @validations[name].result
43
54
 
44
- if (validation_result.respond_to?(:errors) && validation_result.errors.empty?) || validation_result == true
55
+ if validation_result
45
56
  action_result = @actions[name].invoke(record, *args)
46
57
  if (action_result.respond_to?(:errors) && action_result.errors.empty?) || action_result == true
58
+ @common_callback.invoke(record, *args)
47
59
  @callbacks[name].invoke(record, *args)
48
60
  true
49
61
  else
@@ -1,3 +1,3 @@
1
1
  module ServicePacker
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_packer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - fuxin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2017-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby