redispatcher 0.0.2 → 0.0.3

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: 4a1dba3c146a45fc5d21299e409df5d8b860aa9b
4
- data.tar.gz: ad10a86219e6f1fc18b7513f24d20f3256e088f9
3
+ metadata.gz: ac321b3304cf0535c40a2bc21d20c23b0981e574
4
+ data.tar.gz: e04b15f34501d6ed5167b8a7ba5651f68f15548f
5
5
  SHA512:
6
- metadata.gz: 3b99d6b2df35ce4196a748ef9a39d65c901377008d31d6b7479004b1968c5d2fa8cc29d4bc3905a4a209e01865a660112431cdfda4e86da00ebed801b341d180
7
- data.tar.gz: 29e17215b38690bc68d37171d61f18dc2f07fa9b27bcc577de76ab81e76f998d72b922db821c3dd3674edbdb649cfdc8e481bdc4bf562d698586241244390cb0
6
+ metadata.gz: 318f94952b648b182984ca067c673d1607bbe9404411ec046911f2936cfdb4cd6eceb67c66d0038af11e136e31ab67a076d17342a5be74331e24f15abce3299d
7
+ data.tar.gz: 1a88a9a9a5b43f80d0edd15e8ae65638d5cd596626f49d2e68763befcf63d84ebba93ebcfba6af126a6934a52ba166c6466ef20248e731d7c47d30968a1c123e
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
- ![This is really cool dispatcher. However es6-dispatcher is really piece of crap](https://zhf.io/raw/p8IRct)
1
+ ![Redispatcher](https://zhf.io/raw/p8IRct)
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/redispatcher.svg)](http://badge.fury.io/rb/redispatcher)
4
4
  [![Code Climate](https://codeclimate.com/github/rambler-digital-solutions/redispatcher/badges/gpa.svg)](https://codeclimate.com/github/rambler-digital-solutions/redispatcher)
5
5
  [![Test Coverage](https://codeclimate.com/github/rambler-digital-solutions/redispatcher/badges/coverage.svg)](https://codeclimate.com/github/rambler-digital-solutions/redispatcher)
6
6
 
7
- Dispatch ActiveRecord objects to any structures with ease.
7
+ This gem provides delivery of objects from database to any structure with power of callbacks.
8
+
9
+ Supporting ORM is ActiveRecord and Sequel (through [sequel-rails](https://github.com/TalentBox/sequel-rails) gem)
8
10
 
9
11
  ## Installation
10
12
 
@@ -26,16 +28,24 @@ Or install it yourself as:
26
28
 
27
29
  ### Writing Dispatchers
28
30
 
29
- Dispatchers inherit from `Redispatcher::Dispatcher`, live in your `app/dispatchers` directory, and are named for the model that they dispatch:
31
+ Dispatchers inherit from `Redispatcher::Dispatcher`, live in your `app/dispatchers` directory, and are named for the model that they dispatch.
32
+
33
+ You can use rails generator to generate dispatcher:
34
+
35
+ ```
36
+ rails g dispatcher Topic
37
+ ```
38
+
39
+ This will create TopicDispatcher:
30
40
 
31
41
  ```ruby
32
42
  # app/dispatchers/topic_dispatcher.rb
33
43
  class TopicDispatcher < Redispatcher::Dispatcher
44
+ # ...
34
45
  end
35
46
  ```
36
47
 
37
-
38
- Do not hesitate to use dispatscher's callbacks `before_` and `after_` `initialize`, `process`, `commit`, `rollback` just like that:
48
+ Do not hesitate to use dispatscher's callbacks `before_initialize`, `after_initialize`, `before_process`, `after_process`, `before_commit`, `after_commit`, `before_rollback`, `after_rollback` just like that:
39
49
 
40
50
  ```ruby
41
51
  # app/dispatchers/topic_dispatcher.rb
@@ -59,12 +69,22 @@ end
59
69
 
60
70
  ### Enable dispatcher for your model
61
71
 
72
+ With ActiveRecord:
73
+
62
74
  ```ruby
63
75
  class Topic < ActiveRecord::Base
64
76
  dispatchable
65
77
  end
66
78
  ```
67
79
 
80
+ With Sequel:
81
+
82
+ ```ruby
83
+ class Topic < Sequel::Model
84
+ dispatchable
85
+ end
86
+ ```
87
+
68
88
  ### Dispatch!
69
89
 
70
90
  Just call dispatch method on object you going to dispatch.
@@ -73,6 +93,10 @@ Just call dispatch method on object you going to dispatch.
73
93
  dispatched_topic = Topic.first.dispatch
74
94
  ```
75
95
 
96
+ ## License
97
+
98
+ Redispatcher is licensed under the MIT license. See LICENSE for full license text.
99
+
76
100
  ## Contributing
77
101
 
78
102
  1. Fork it ( https://github.com/rambler-digital-solutions/redispatcher/fork )
@@ -0,0 +1,24 @@
1
+ module Rails
2
+ module Generators
3
+ class DispatcherGenerator < NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ check_class_collision suffix: "Dispatcher"
6
+
7
+ class_option :parent, type: :string, desc: "The parent class for the generated dispatcher"
8
+
9
+ def create_dispatcher_file
10
+ template 'dispatcher.rb', File.join('app/dispatchers', class_path, "#{file_name}_dispatcher.rb")
11
+ end
12
+
13
+ hook_for :test_framework
14
+
15
+ private
16
+
17
+ def parent_class_name
18
+ options.fetch("parent") do
19
+ "Redispatcher::Dispatcher"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ <%- module_namespacing do -%>
2
+ <%- if parent_class_name.present? -%>
3
+ class <%= class_name %>Dispatcher < <%= parent_class_name %>
4
+ <%- else -%>
5
+ class <%= class_name %>
6
+ <%- end -%>
7
+ # Now define some callbacks, for example:
8
+ #
9
+ # after_initialize do
10
+ # @processed_object = {}
11
+ # end
12
+ #
13
+ # before_process do
14
+ # @processed_object.merge!(title: object.title, created_at: object.created_at)
15
+ # end
16
+ #
17
+ # after_commit do
18
+ # File.open('/tmp/dispatcher.json', 'w') do
19
+ # f.write(JSON.pretty_generate(processed_object))
20
+ # f.close
21
+ # end
22
+ # object.update_attributes(dispatched_at: Time.now)
23
+ # end
24
+ #
25
+ # after_rollback do
26
+ # puts 'Oh snap!'
27
+ # end
28
+ end
29
+ <% end -%>
@@ -5,31 +5,13 @@ require_relative 'redispatcher/dispatchable'
5
5
  require_relative 'redispatcher/callbacks'
6
6
  require_relative 'redispatcher/logger'
7
7
  require_relative 'redispatcher/dispatcher'
8
+ require_relative 'redispatcher/dispatchable_method'
9
+ require_relative 'redispatcher/railtie' if defined? Rails
8
10
 
9
11
  module Redispatcher
10
- end
11
-
12
- class << ActiveRecord::Base
13
- def dispatchable(options = {})
14
- # Check options
15
- raise Redispatcher::DispatcherError.new("Options for dispatchable must be in a hash.") unless options.is_a? Hash
16
- options.each do |key, value|
17
- # No options yet
18
- unless [:dispatcher_class].include? key
19
- raise Redispatcher::DispatcherError.new("Unknown option for dispatchable: #{key.inspect} => #{value.inspect}.")
20
- end
12
+ def self.setup_orm(base)
13
+ base.class_eval do
14
+ extend Redispatcher::DispatchableMethod
21
15
  end
22
-
23
- include Redispatcher::Callbacks
24
- define_dispatcher_callbacks :dispatch
25
-
26
- include Redispatcher::Dispatchable
27
-
28
- # Create dispatcher class accessor and set to option or default
29
- #cattr_accessor :dispatcher_class
30
- #self.dispatcher_class = option[:dispatcher_class]
31
-
32
- after_commit :dispatch
33
16
  end
34
17
  end
35
-
@@ -1,6 +1,7 @@
1
1
  module Redispatcher
2
2
  module Callbacks
3
3
  def self.included(base)
4
+ base.send(:include, ActiveSupport::Callbacks)
4
5
  base.extend(ClassMethods)
5
6
  base.send(:include, InstanceMethods)
6
7
  end
@@ -0,0 +1,23 @@
1
+ module Redispatcher
2
+ module DispatchableMethod
3
+ def dispatchable(options = {})
4
+ # Check options
5
+ raise Redispatcher::DispatcherError.new("Options for dispatchable must be in a hash.") unless options.is_a? Hash
6
+ options.each do |key, value|
7
+ # No options yet
8
+ unless [:dispatcher_class].include? key
9
+ raise Redispatcher::DispatcherError.new("Unknown option for dispatchable: #{key.inspect} => #{value.inspect}.")
10
+ end
11
+ end
12
+
13
+ include Redispatcher::Callbacks
14
+ define_dispatcher_callbacks :dispatch
15
+
16
+ include Redispatcher::Dispatchable
17
+
18
+ # Create dispatcher class accessor and set to option or default
19
+ #cattr_accessor :dispatcher_class
20
+ #self.dispatcher_class = option[:dispatcher_class]
21
+ end
22
+ end
23
+ end
@@ -44,21 +44,23 @@ module Redispatcher
44
44
  end
45
45
 
46
46
  class << self
47
+ attr_accessor :instance
48
+
47
49
  def dispatch(object, options={})
48
- @dispatcher = new(object, options)
50
+ @instance = new(object, options)
49
51
 
50
52
  begin
51
- @dispatcher.process
53
+ instance.process
52
54
  rescue DispatcherSuppressedError => e
53
- log e
55
+ instance.log e
54
56
  return nil
55
57
  end
56
58
 
57
59
  begin
58
- return @dispatcher.commit
60
+ return instance.commit
59
61
  rescue Exception => e
60
- log e
61
- @dispatcher.rollback
62
+ instance.log e
63
+ instance.rollback
62
64
  raise e
63
65
  end
64
66
  end
@@ -3,7 +3,7 @@ module Redispatcher
3
3
  # Log a dispatcher-specific line. This will log to STDOUT
4
4
  # by default.
5
5
  # Set Dispatcher.options[:log] to false to turn off.
6
- def log message
6
+ def log(message)
7
7
  logger.info("[redispatcher] #{message}") if logging?
8
8
  end
9
9
 
@@ -0,0 +1,18 @@
1
+ require 'rails/railtie'
2
+
3
+ module Redispatcher
4
+ class Railtie < Rails::Railtie
5
+
6
+ config.after_initialize do |app|
7
+ app.config.paths.add 'app/dispatcher', eager_load: true
8
+ end
9
+
10
+ initializer "redispatcher.setup_orm" do |app|
11
+ [:active_record, :sequel].each do |orm|
12
+ ActiveSupport.on_load orm do
13
+ Redispatcher.setup_orm self
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'redispatcher'
7
- spec.version = '0.0.2'
7
+ spec.version = '0.0.3'
8
8
  spec.authors = ['Sergey Besedin']
9
9
  spec.email = ['kr3ssh@gmail.com']
10
10
  spec.summary = %q{Gem to dispatch ActiveRecord objects to anywhere}
@@ -12,23 +12,29 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = 'https://github.com/rambler-digital-solutions/redispatcher'
13
13
  spec.license = 'MIT'
14
14
 
15
+ spec.required_ruby_version = '>= 1.9'
16
+
15
17
  spec.files = %w(
16
18
  Gemfile
17
19
  LICENSE.txt
18
20
  README.md
19
21
  Rakefile
22
+ lib/generators/rails/dispatcher_generator.rb
23
+ lib/generators/rails/templates/dispatcher.rb
20
24
  lib/redispatcher.rb
21
25
  lib/redispatcher/callbacks.rb
22
26
  lib/redispatcher/dispatchable.rb
27
+ lib/redispatcher/dispatchable_method.rb
23
28
  lib/redispatcher/dispatcher.rb
24
29
  lib/redispatcher/exceptions.rb
25
30
  lib/redispatcher/logger.rb
31
+ lib/redispatcher/railtie.rb
26
32
  redispatcher.gemspec
27
33
  )
28
34
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
29
35
  spec.require_paths = ['lib']
30
36
 
31
- spec.add_dependency 'rails'
37
+ spec.add_dependency 'rails', '>= 4.0'
32
38
  spec.add_development_dependency 'bundler', '~> 1.7'
33
39
  spec.add_development_dependency 'rake', '~> 10.0'
34
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redispatcher
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
  - Sergey Besedin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-27 00:00:00.000000000 Z
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -63,12 +63,16 @@ files:
63
63
  - LICENSE.txt
64
64
  - README.md
65
65
  - Rakefile
66
+ - lib/generators/rails/dispatcher_generator.rb
67
+ - lib/generators/rails/templates/dispatcher.rb
66
68
  - lib/redispatcher.rb
67
69
  - lib/redispatcher/callbacks.rb
68
70
  - lib/redispatcher/dispatchable.rb
71
+ - lib/redispatcher/dispatchable_method.rb
69
72
  - lib/redispatcher/dispatcher.rb
70
73
  - lib/redispatcher/exceptions.rb
71
74
  - lib/redispatcher/logger.rb
75
+ - lib/redispatcher/railtie.rb
72
76
  - redispatcher.gemspec
73
77
  homepage: https://github.com/rambler-digital-solutions/redispatcher
74
78
  licenses:
@@ -82,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
86
  requirements:
83
87
  - - ">="
84
88
  - !ruby/object:Gem::Version
85
- version: '0'
89
+ version: '1.9'
86
90
  required_rubygems_version: !ruby/object:Gem::Requirement
87
91
  requirements:
88
92
  - - ">="
@@ -90,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
94
  version: '0'
91
95
  requirements: []
92
96
  rubyforge_project:
93
- rubygems_version: 2.2.2
97
+ rubygems_version: 2.4.5
94
98
  signing_key:
95
99
  specification_version: 4
96
100
  summary: Gem to dispatch ActiveRecord objects to anywhere