redispatcher 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 035ad73b6e6ec3bae5ad47e52d49531ab105531d
4
+ data.tar.gz: 2efec77011f4e680e69f4c211be376de2c4ea514
5
+ SHA512:
6
+ metadata.gz: 1f81363de16fe3809e7b988d540e7798644195f95b81a9c78094c339e2ff0623d068e59dfe16a8caf6a8f57a7ec716aa138ed9930d9a28d0c9b25e47dbc25119
7
+ data.tar.gz: 207d86dd6343136842e8d363f203c292510245cc10f45e013553b9ef1726e1e589badbcd2a0b12a49990aecca52882982cbe87f3ad7a98573aa328a175bf04c5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redispatcher.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sergey Besedin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ ![This is really cool dispatcher. However es6-dispatcher is really piece of crap](https://zhf.io/raw/p8IRct)
2
+
3
+ Dispatch ActiveRecord objects to any structures with ease.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'redispatcher'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install redispatcher
20
+
21
+ ## Using
22
+
23
+ ### Writing Dispatchers
24
+
25
+ Dispatchers inherit from `Redispatcher::Dispatcher`, live in your `app/dispatchers` directory, and are named for the model that they dispatch:
26
+
27
+ ```ruby
28
+ # app/dispatchers/topic_dispatcher.rb
29
+ class TopicDispatcher < Redispatcher::Dispatcher
30
+ end
31
+ ```
32
+
33
+ ### Enable dispatcher for your model
34
+
35
+ ```ruby
36
+ class Topic < ActiveRecord::Base
37
+ dispatchable
38
+ end
39
+ ```
40
+
41
+ ### Dispatch!
42
+
43
+ Just call dispatch method on object you going to dispatch.
44
+
45
+ ```ruby
46
+ dispatched_topic = Topic.first.dispatch
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/rambler-digital-solutions/redispatcher/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,35 @@
1
+ require 'active_support/callbacks'
2
+ require 'active_support/concern'
3
+ require_relative 'redispatcher/exceptions'
4
+ require_relative 'redispatcher/dispatchable'
5
+ require_relative 'redispatcher/callbacks'
6
+ require_relative 'redispatcher/logger'
7
+ require_relative 'redispatcher/dispatcher'
8
+
9
+ 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
21
+ end
22
+
23
+ include Redispatcher::Callbacks
24
+ define_dispatcher_callbacks
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
+ end
34
+ end
35
+
@@ -0,0 +1,30 @@
1
+ module Redispatcher
2
+ module Callbacks
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def define_dispatcher_callbacks
10
+ define_callbacks :dispatch
11
+
12
+ eval <<-end_callbacks
13
+ def before_dispatch(*args, &block)
14
+ set_callback(:dispatch, :before, *args, &block)
15
+ end
16
+
17
+ def after_dispatch(*args, &block)
18
+ set_callback(:dispatch, :after, *args, &block)
19
+ end
20
+ end_callbacks
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+ def run_dispatcher_callbacks(&block)
26
+ run_callbacks(:dispatch, &block)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ module Redispatcher
2
+ module Dispatchable
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module InstanceMethods
9
+
10
+ # Decorates the object using the inferred {#dispatcher_class}.
11
+ # @param [Hash] options
12
+ # see {Redispatcher::Dispatcher#initialize}
13
+ def dispatch(options = {})
14
+ run_dispatcher_callbacks do
15
+ dispatcher_class.dispatch(self, options)
16
+ end
17
+ end
18
+
19
+ # (see Dispatchable::ClassMethods#dispatcher_class)
20
+ def dispatcher_class
21
+ self.class.dispatcher_class
22
+ end
23
+
24
+ def dispatcher_class?
25
+ self.class.dispatcher_class?
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def dispatcher_class?
31
+ dispatcher_class
32
+ rescue Redispatcher::UninferrableDispatcherError
33
+ false
34
+ end
35
+
36
+ # Infers the dispatcher class `Topic` maps to `TopicRedispatcher`).
37
+ # @return [Class] the inferred dispatcher class.
38
+ def dispatcher_class
39
+ prefix = respond_to?(:model_name) ? model_name : name
40
+ dispatcher_name = "#{prefix}Dispatcher"
41
+ dispatcher_name.constantize
42
+ rescue NameError => error
43
+ if superclass.respond_to?(:dispatcher_class)
44
+ superclass.dispatcher_class
45
+ else
46
+ raise unless error.missing_name?(dispatcher_name)
47
+ raise Redispatcher::UninferrableDispatcherError.new(self)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,66 @@
1
+ module Redispatcher
2
+ class Dispatcher
3
+ include ActiveSupport::Callbacks
4
+ include Logger
5
+
6
+ define_callbacks :initialize, :process, :commit, :rollback
7
+
8
+ set_callback :commit, :after do
9
+ log "successfully dispatched"
10
+ end
11
+
12
+ set_callback :rollback, :after do
13
+ log "dispatching failed"
14
+ end
15
+
16
+ attr_accessor :options, :object, :processed_object
17
+
18
+ def initialize(object, options={})
19
+ @object = object
20
+ @options = options
21
+ run_callbacks :initialize do
22
+ log "initialize callback"
23
+ end
24
+ end
25
+
26
+ def process
27
+ run_callbacks :process do
28
+ log "process callback"
29
+ end
30
+ end
31
+
32
+ def commit
33
+ run_callbacks :commit do
34
+ log "commit callback"
35
+ end
36
+ processed_object
37
+ end
38
+
39
+ def rollback
40
+ run_callbacks :rollback do
41
+ log "rollback callback"
42
+ end
43
+ end
44
+
45
+ class << self
46
+ def dispatch(object, options={})
47
+ @dispatcher = new(object, options)
48
+
49
+ begin
50
+ @dispatcher.process
51
+ rescue DispatcherSuppressedError => e
52
+ log e
53
+ return nil
54
+ end
55
+
56
+ begin
57
+ return @dispatcher.commit
58
+ rescue Exception => e
59
+ log e
60
+ @dispatcher.rollback
61
+ raise e
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module Redispatcher
2
+ class DispatcherError < RuntimeError; end
3
+ class DispatcherSuppressedError < DispatcherError; end
4
+ class UninferrableDispatcherError < DispatcherError; end
5
+ end
@@ -0,0 +1,22 @@
1
+ module Redispatcher
2
+ module Logger
3
+ # Log a dispatcher-specific line. This will log to STDOUT
4
+ # by default.
5
+ # Set Dispatcher.options[:log] to false to turn off.
6
+ def log message
7
+ logger.info("[redispatcher] #{message}") if logging?
8
+ end
9
+
10
+ def logger #:nodoc:
11
+ @logger ||= options[:logger] || ::Logger.new(STDOUT)
12
+ end
13
+
14
+ def logger=(logger)
15
+ @logger = logger
16
+ end
17
+
18
+ def logging? #:nodoc:
19
+ options[:log]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'redispatcher'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['Sergey Besedin']
9
+ spec.email = ['kr3ssh@gmail.com']
10
+ spec.summary = %q{Gem to dispatch ActiveRecord objects to anywhere}
11
+ spec.description = %q{}
12
+ spec.homepage = 'https://github.com/rambler-digital-solutions/redispatcher'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = %w(
16
+ Gemfile
17
+ LICENSE.txt
18
+ README.md
19
+ Rakefile
20
+ lib/redispatcher.rb
21
+ lib/redispatcher/callbacks.rb
22
+ lib/redispatcher/dispatchable.rb
23
+ lib/redispatcher/dispatcher.rb
24
+ lib/redispatcher/exceptions.rb
25
+ lib/redispatcher/logger.rb
26
+ redispatcher.gemspec
27
+ )
28
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'rails'
32
+ spec.add_development_dependency 'bundler', '~> 1.7'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redispatcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Besedin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: ''
56
+ email:
57
+ - kr3ssh@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - lib/redispatcher.rb
67
+ - lib/redispatcher/callbacks.rb
68
+ - lib/redispatcher/dispatchable.rb
69
+ - lib/redispatcher/dispatcher.rb
70
+ - lib/redispatcher/exceptions.rb
71
+ - lib/redispatcher/logger.rb
72
+ - redispatcher.gemspec
73
+ homepage: https://github.com/rambler-digital-solutions/redispatcher
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Gem to dispatch ActiveRecord objects to anywhere
97
+ test_files: []