state_machines-activemodel-observers 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 421f37b35ee734f194f8dce74087808e4a23daa6
4
+ data.tar.gz: c44bbfa0a5bddd3ba52631e71cbee1fc141f6d71
5
+ SHA512:
6
+ metadata.gz: d6a75ef16b999f667d30062320133b8a2ab43d707e11ffe2d06a3672ab3f54b5fa6529bc501a881ef6a655906112838c194e35a6255d06e60f8087f4e7099295
7
+ data.tar.gz: cf2d6ba7999a194438e76196cc30bd3489f699fcc1db74c81d125ce37832f18c847bfbcee9321a4d66a00231324e27e026ecfe81dcef25853a2e4a743f3c6c36
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ *.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /.idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ - bundle install
5
+ script: bundle exec rake
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.2
9
+ - jruby-19mode
10
+ - rbx-2
11
+
12
+ gemfile:
13
+ - gemfiles/active_model_4.1.gemfile
14
+ - gemfiles/active_model_4.2.gemfile
data/Appraisals ADDED
@@ -0,0 +1,9 @@
1
+ appraise 'active_model_4.1' do
2
+ gem 'activemodel', '~> 4.1.0'
3
+ gem 'rails-observers'
4
+ end
5
+
6
+ appraise "active_model_4.2" do
7
+ gem "activemodel", "~> 4.2.0"
8
+ gem 'rails-observers'
9
+ end
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2006-2012 Aaron Pfeifer
2
+ Copyright (c) 2014-2015 Abdelkader Boudih
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ [![Build Status](https://travis-ci.org/state-machines/state_machines-activemodel-observers.svg?branch=master)](https://travis-ci.org/state-machines/state_machines-activemodel-observers)
2
+ [![Code Climate](https://codeclimate.com/github/state-machines/state_machines-activemodel-observers.png)](https://codeclimate.com/github/state-machines/state_machines-activemodel-observers)
3
+ # StateMachines: ActiveModel Observers
4
+
5
+ ActiveModel Observers integration for State Machines
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'state_machines-activemodel-observers'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install state_machines-activemodel-observers
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+ TODO: Import/Write tests
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/state-machines/state_machines-activemodel-observers/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'bundler/gem_tasks'
5
+ require 'rake/testtask'
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.test_files = FileList['test/*_test.rb']
9
+ end
10
+
11
+ desc 'Default: run all tests.'
12
+ task default: :test
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 4.1.0"
6
+ gem "rails-observers"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 4.2.0"
6
+ gem "rails-observers"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1 @@
1
+ require 'state_machines/integrations/active_model/observers'
@@ -0,0 +1,102 @@
1
+ require 'state_machines/integrations/active_model'
2
+ require 'rails/observers/active_model'
3
+ require_relative 'observers/version'
4
+ require_relative 'observers/observer_update'
5
+
6
+ module StateMachines
7
+ module Integrations
8
+ module ActiveModel
9
+ # Classes that include ActiveModel::Observing
10
+ # will automatically use the ActiveModel integration.
11
+ def self.matching_ancestors
12
+ %w(ActiveModel ActiveModel::Observing ActiveModel::Validations)
13
+ end
14
+
15
+ # Adds a set of default callbacks that utilize the Observer extensions
16
+ def add_default_callbacks
17
+ callbacks[:before] << StateMachines::Callback.new(:before) { |object, transition| notify(:before, object, transition) }
18
+ callbacks[:after] << StateMachines::Callback.new(:after) { |object, transition| notify(:after, object, transition) }
19
+ callbacks[:failure] << StateMachines::Callback.new(:failure) { |object, transition| notify(:after_failure_to, object, transition) }
20
+ end
21
+
22
+ # Notifies observers on the given object that a callback occurred
23
+ # involving the given transition. This will attempt to call the
24
+ # following methods on observers:
25
+ # * <tt>#{type}_#{qualified_event}_from_#{from}_to_#{to}</tt>
26
+ # * <tt>#{type}_#{qualified_event}_from_#{from}</tt>
27
+ # * <tt>#{type}_#{qualified_event}_to_#{to}</tt>
28
+ # * <tt>#{type}_#{qualified_event}</tt>
29
+ # * <tt>#{type}_transition_#{machine_name}_from_#{from}_to_#{to}</tt>
30
+ # * <tt>#{type}_transition_#{machine_name}_from_#{from}</tt>
31
+ # * <tt>#{type}_transition_#{machine_name}_to_#{to}</tt>
32
+ # * <tt>#{type}_transition_#{machine_name}</tt>
33
+ # * <tt>#{type}_transition</tt>
34
+ #
35
+ # This will always return true regardless of the results of the
36
+ # callbacks.
37
+ def notify(type, object, transition)
38
+ name = self.name
39
+ event = transition.qualified_event
40
+ from = transition.from_name || 'nil'
41
+ to = transition.to_name || 'nil'
42
+
43
+ # Machine-specific updates
44
+ ["#{type}_#{event}", "#{type}_transition_#{name}"].each do |event_segment|
45
+ ["_from_#{from}", nil].each do |from_segment|
46
+ ["_to_#{to}", nil].each do |to_segment|
47
+ object.class.changed if object.class.respond_to?(:changed)
48
+ object.class.notify_observers('update_with_transition', ObserverUpdate.new([event_segment, from_segment, to_segment].join, object, transition))
49
+ end
50
+ end
51
+ end
52
+
53
+ # Generic updates
54
+ object.class.changed if object.class.respond_to?(:changed)
55
+ object.class.notify_observers('update_with_transition', ObserverUpdate.new("#{type}_transition", object, transition))
56
+
57
+ true
58
+ end
59
+
60
+ def add_callback(type, options, &block)
61
+ options[:terminator] = callback_terminator
62
+ @callbacks[type == :around ? :before : type].insert(-2, callback = StateMachines::Callback.new(type, options, &block))
63
+ add_states(callback.known_states)
64
+ callback
65
+ end
66
+
67
+ # Initializes class-level extensions and defaults for this machine
68
+ def after_initialize
69
+ super()
70
+ add_default_callbacks
71
+ end
72
+
73
+ # Adds support for invoking callbacks on ActiveModel observers with more
74
+ # than one argument (e.g. the record *and* the state transition). By
75
+ # default, ActiveModel only supports passing the record into the
76
+ # callbacks.
77
+ #
78
+ # For example:
79
+ #
80
+ # class VehicleObserver < ActiveModel::Observer
81
+ # # The default behavior: only pass in the record
82
+ # def after_save(vehicle)
83
+ # end
84
+ #
85
+ # # Custom behavior: allow the transition to be passed in as well
86
+ # def after_transition(vehicle, transition)
87
+ # Audit.log(vehicle, transition)
88
+ # end
89
+ # end
90
+ module Observer
91
+ def update_with_transition(observer_update)
92
+ method = observer_update.method
93
+ send(method, *observer_update.args) if respond_to?(method)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ ActiveModel::Observer.class_eval do
101
+ include StateMachines::Integrations::ActiveModel::Observer
102
+ end
@@ -0,0 +1,42 @@
1
+ module StateMachines
2
+ module Integrations #:nodoc:
3
+ module ActiveModel
4
+ # Represents the encapsulation of all of the details to be included in an
5
+ # update to state machine observers. This allows multiple arguments to
6
+ # get passed to an observer method (instead of just a single +object+)
7
+ # while still respecting the way in which ActiveModel checks for the
8
+ # object's list of observers.
9
+ class ObserverUpdate
10
+ # The method to invoke on the observer
11
+ attr_reader :method
12
+
13
+ # The object being transitioned
14
+ attr_reader :object
15
+
16
+ # The transition being run
17
+ attr_reader :transition
18
+
19
+ def initialize(method, object, transition) #:nodoc:
20
+ @method, @object, @transition = method, object, transition
21
+ end
22
+
23
+ # The arguments to pass into the method
24
+ def args
25
+ [object, transition]
26
+ end
27
+
28
+ # The class of the object being transitioned. Normally the object
29
+ # getting passed into observer methods is the actual instance of the
30
+ # ActiveModel class. ActiveModel uses that instance's class to check
31
+ # for enabled / disabled observers.
32
+ #
33
+ # Since state_machine is passing an ObserverUpdate instance into observer
34
+ # methods, +class+ needs to be overridden so that ActiveModel can still
35
+ # get access to the enabled / disabled observers.
36
+ def class
37
+ object.class
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ module StateMachines
2
+ module Integrations
3
+ module ActiveModel
4
+ module Observers
5
+ VERSION = '0.0.2'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'state_machines/integrations/active_model/observers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'state_machines-activemodel-observers'
8
+ spec.version = StateMachines::Integrations::ActiveModel::Observers::VERSION
9
+ spec.authors = ['Abdelkader Boudih', 'Aaron Pfeifer']
10
+ spec.email = %w(terminale@gmail.com aaron@pluginaweek.org)
11
+ spec.summary = %q(ActiveModel Observers integration for State Machines)
12
+ spec.description = %q(Adds support for ActiveModel Observers)
13
+ spec.homepage = 'https://github.com/state-machines/state_machines-activemodel-observers'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = spec.files.grep(%r{^test/})
18
+ spec.require_paths = ['lib']
19
+ spec.required_ruby_version = '>= 1.9.3'
20
+
21
+ spec.add_dependency 'state_machines-activemodel' , '>= 0.2.0'
22
+ spec.add_dependency 'rails-observers'
23
+
24
+ spec.add_development_dependency 'bundler', '>= 1.6'
25
+ spec.add_development_dependency 'rake', '>= 10'
26
+ spec.add_development_dependency 'appraisal', '>= 2'
27
+ spec.add_development_dependency 'minitest'
28
+ spec.add_development_dependency 'minitest-reporters'
29
+ end
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'pry-byebug'
3
+ rescue LoadError
4
+ end
5
+
6
+ require 'state_machines-activemodel'
7
+ require 'minitest/autorun'
8
+ require 'minitest/reporters'
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_machines-activemodel-observers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Abdelkader Boudih
8
+ - Aaron Pfeifer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: state_machines-activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 0.2.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 0.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rails-observers
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.6'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '1.6'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '10'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '10'
70
+ - !ruby/object:Gem::Dependency
71
+ name: appraisal
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '2'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '2'
84
+ - !ruby/object:Gem::Dependency
85
+ name: minitest
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: minitest-reporters
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: Adds support for ActiveModel Observers
113
+ email:
114
+ - terminale@gmail.com
115
+ - aaron@pluginaweek.org
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Appraisals
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - gemfiles/active_model_4.1.gemfile
129
+ - gemfiles/active_model_4.2.gemfile
130
+ - lib/state_machines-activemodel-observers.rb
131
+ - lib/state_machines/integrations/active_model/observers.rb
132
+ - lib/state_machines/integrations/active_model/observers/observer_update.rb
133
+ - lib/state_machines/integrations/active_model/observers/version.rb
134
+ - state_machines-activemodel-observers.gemspec
135
+ - test/test_helper.rb
136
+ homepage: https://github.com/state-machines/state_machines-activemodel-observers
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 1.9.3
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: ActiveModel Observers integration for State Machines
160
+ test_files:
161
+ - test/test_helper.rb