service_packer 0.0.2 → 0.0.3

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: bfc85d2aec7af6ef5fdbcc3ca1bc4aec7b5fe650
4
- data.tar.gz: 1af314b27ac45e92b48b8417db599255f0ded9e2
3
+ metadata.gz: a48cadb9c06121cad55e7defce079a7eb8faabb5
4
+ data.tar.gz: 256bb20740c5c0bc3cfc4d81cb9925f6bd90f9fa
5
5
  SHA512:
6
- metadata.gz: da1d49ab76ffabde6366dada3a566027c875457fd73b8d2568f6234db7c9cc4e07bbb821d4e8a2608b66c0e1481df64053ae59146dac520df288229a9eb5f6f8
7
- data.tar.gz: c1724348cf5dff4bb3632f84da8b4c709de1e7dbcfc5c4535266b077c7dd3df43740b138282d95542b9c653483fca47f1e608b33e6a8f298164d3643e70a9470
6
+ metadata.gz: 8f86457756e5ad34df821b97b68484d9206a2e576aff7b7b3bc2cfff1a411317783aa92c5882911e21653af00f988378b2c9046c27cb526676afc0d7b8e893ac
7
+ data.tar.gz: 3a15027ac465272fded1ff879e6f32971c133e331b26738ea3fb8ebc5b2b7edffc08477ff5f322dd90528b846f533c74fc60038672db016ef5999cb5ec4e8957
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: required
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ - 2.3.3
6
+ install: "bundle install"
7
+ script:
8
+ - bundle exec rspec
9
+ cache: bundler
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # ServicePacker
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
+ [![Gem Version](https://badge.fury.io/rb/service_packer.svg)](https://badge.fury.io/rb/service_packer)
4
5
 
5
6
  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).
6
7
  Service packer provides a series of methods to standardize the process of many common behaviors.
@@ -41,14 +42,14 @@ class Service < ServicePacker::Base
41
42
  end
42
43
 
43
44
  setup :send, :params => [:driver] do
44
- validation_define do |driver|
45
+ validation_define do
45
46
  unless ride.may_send?
46
47
  ride.errors.add(:base, :cannot_be_assigned)
47
48
  end
48
49
  ride
49
50
  end
50
51
 
51
- action_define do |driver|
52
+ action_define do
52
53
  ride.send(driver)
53
54
  end
54
55
 
@@ -2,9 +2,33 @@ require 'logger'
2
2
 
3
3
  module ServicePacker
4
4
  class Base
5
+ module InstanceMethods
6
+ def initialize(*args)
7
+ @_pass_in_arguments = {}
8
+
9
+ super(*args)
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ super(name, *args, &block) unless @_pass_in_arguments.include? name
14
+ @_pass_in_arguments[name.to_sym]
15
+ end
16
+
17
+ # DO NOT CALL THIS METHODS UNLESS IF YOU ARE DEFINITELY KNOWN WHAT YOU DID
18
+ # This method is for pass-in-arguments temporarily
19
+ def __set_arguments(kwargs)
20
+ @_pass_in_arguments.merge!(kwargs)
21
+ end
22
+
23
+ def __clear_arguments
24
+ @_pass_in_arguments = {}
25
+ end
26
+ end
27
+
5
28
  module ClassMethods
6
29
  def inherited(base)
7
30
  ServicePackerStore.register(base)
31
+ base.prepend InstanceMethods
8
32
 
9
33
  super
10
34
  end
@@ -17,10 +41,10 @@ module ServicePacker
17
41
  kwargs = args[-1].is_a?(Hash) ? args[-1] : {}
18
42
  params = kwargs[:params] || []
19
43
  service = service_packer
20
- service.set_method_name(method_name) { service.instance_exec(&block) }
44
+ service.setup_method(method_name) { service.instance_exec(&block) }
21
45
  define_method(method_name) do |*args|
22
46
  raise ArgumentError, "wrong number of arguments (given #{args.size}, expected #{params.size})" if args.size != params.size
23
- service.invoke(self, method_name, *args)
47
+ service.invoke(self, method_name, params, *args)
24
48
  end
25
49
  end
26
50
  end
@@ -9,7 +9,7 @@ module ServicePacker
9
9
  @method_name = nil
10
10
  end
11
11
 
12
- def set_method_name(name, &block)
12
+ def setup_method(name, &block)
13
13
  @method_name = name
14
14
  validation_define { true }
15
15
  action_define { true }
@@ -30,7 +30,15 @@ module ServicePacker
30
30
  add_callback(@method_name, *args, &block)
31
31
  end
32
32
 
33
- def invoke(record, name, *args)
33
+ def invoke(record, name, params, *args)
34
+ record.__set_arguments(params.zip(args).to_h)
35
+ result = process(record, name, *args)
36
+ record.__clear_arguments
37
+
38
+ result
39
+ end
40
+
41
+ def process(record, name, *args)
34
42
  validation_result = @validations[name].invoke(record, *args)
35
43
 
36
44
  if (validation_result.respond_to?(:errors) && validation_result.errors.empty?) || validation_result == true
@@ -1,3 +1,3 @@
1
1
  module ServicePacker
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
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-21 00:00:00.000000000 Z
11
+ date: 2017-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
78
  - ".rspec"
79
+ - ".travis.yml"
79
80
  - CODE_OF_CONDUCT.md
80
81
  - Gemfile
81
82
  - LICENSE.txt