shout_out 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72b4e70e107e6a99ac9a190cc173e024305acb04
4
+ data.tar.gz: cef0924b356ebea3dccd0f74d1010200418c4f4e
5
+ SHA512:
6
+ metadata.gz: 3311721b678fb178eebd807449d37567399f7ade73fd5ef6b6cbfb0f1e227cb38370955e99f21821cf013552d676e80ed36b3560c6dda20279e79b76f041e74e
7
+ data.tar.gz: 4c4336e39871b088688f900b41f1a2513fa92688069a90f978086620c9a90b2348a79642a37d3e4cbae7984350b8d97e7b30c5a1c150874f11ce761099e50eb1
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shout_out.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kevin Pheasey
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.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Shout Out
2
+
3
+ Shout Out allows for the abstraction of complex ActiveRecord callbacks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shout_out'
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install shout_out
17
+
18
+ Then run the install generator
19
+
20
+ $ rails g shout_out:install
21
+
22
+ This will create ApplicationShout in /app/shouts/application_shout.rb
23
+
24
+ ## Usage
25
+
26
+ To generate a shout class for a model, just use the generator.
27
+
28
+ $ rails g shout_out:shout My::Model::Name
29
+
30
+ This will create the appropriate class in the /app/shouts directory, i.e. ```/app/shouts/my/model/name_shout.rb```.
31
+
32
+ Now you can define methods for any callback.
33
+
34
+ ```
35
+ before_validation
36
+ validate
37
+ after_validation
38
+ before_save
39
+ after_save
40
+ before_create
41
+ after_create
42
+ after_commit
43
+ ```
44
+
45
+ ## Example
46
+
47
+ The below is an example of using shouts to handle cache invalidation on interdependent models.
48
+
49
+ ```ruby
50
+ # /app/shouts/invoice_shout.rb
51
+ class InvoiceShout < ApplicationShout
52
+
53
+ def after_save
54
+ touch_project
55
+ touch_report_ffa_efficiency
56
+ touch_report_ffa_hour_tracker
57
+ end
58
+
59
+ private
60
+
61
+ def touch_report_ffa_efficiency
62
+ if record.total_ffa_changed? && calculable? && !record.report_ffa_efficiency.new_record?
63
+ record.report_ffa_efficiency.touch
64
+ end
65
+ end
66
+
67
+ def touch_report_ffa_hour_tracker
68
+ changed = record.total_ffa_changed? || work_type_changed?(WorkType.codes[:ffa])
69
+
70
+ if changed && calculable? && record.report_ffa_hour_tracker
71
+ record.report_ffa_hour_tracker.touch
72
+ end
73
+ end
74
+
75
+ def touch_project
76
+ changed = record.product_comp_changed? || record.product_cost_changed? || record.labor_comp_changed?
77
+
78
+ if changed && calculable? && record.project
79
+ record.project.touch
80
+ end
81
+ end
82
+
83
+ end
84
+ ```
85
+
86
+
87
+ You can define common methods in ApplicationShout.
88
+
89
+ ```ruby
90
+ # /app/shouts/application_shout.rb
91
+ class ApplicationShout
92
+
93
+ ...
94
+
95
+ def calculable?
96
+ record.calculable? || record.calculable_changed?
97
+ end
98
+
99
+ def work_type_changed?(codes)
100
+ codes = [codes] unless code.is_a?(Array)
101
+ record.work_type_code_changed? && ([record.work_type_code, record.work_type_code_was] & codes).any?
102
+ end
103
+
104
+ end
105
+ ```
106
+
107
+ ## Contributing
108
+
109
+ - Fork it ( https://github.com/kpheasey/shout_out/fork )
110
+ - Create your feature branch (git checkout -b my-new-feature)
111
+ - Commit your changes (git commit -am 'Add some feature')
112
+ - Push to the branch (git push origin my-new-feature)
113
+ - Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,12 @@
1
+ module ShoutOut
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def copy_file
7
+ template 'application_shout.rb', 'app/shouts/application_shout.rb'
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ class ApplicationShout
2
+ attr_reader :record
3
+
4
+ def initialize(record)
5
+ @record = record
6
+ end
7
+
8
+ end
@@ -0,0 +1,19 @@
1
+ module ShoutOut
2
+ module Generators
3
+ class ShoutGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ argument :model_name, type: :string
6
+
7
+ def copy_file
8
+ if model_name.underscore.include? '_shout'
9
+ file = "app/shouts/#{model_name.underscore}.rb"
10
+ else
11
+ file = "app/shouts/#{model_name.underscore}_shout.rb"
12
+ end
13
+
14
+ template 'shout.rb', file
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ class <%= model_name.underscore.include?('_shout') ? model_name : "#{model_name}Shout" %> < ApplicationShout
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module ShoutOut
2
+ VERSION = "0.0.1"
3
+ end
data/lib/shout_out.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'shout_out/version'
2
+ require 'active_support/concern'
3
+
4
+ module ShoutOut
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_validation -> { shout(:before_validation, self) }
9
+ validate -> { shout(:validate, self) }
10
+ after_validation -> { shout(:after_validation, self) }
11
+ before_save -> { shout(:before_save, self) }
12
+ after_save -> { shout(:after_save, self) }
13
+ before_create -> { shout(:before_create, self) }
14
+ after_create -> { shout(:after_create, self) }
15
+ after_commit -> { shout(:after_commit, self) }
16
+ end
17
+
18
+ def shout(action, record)
19
+ begin
20
+ klass = "#{record.class.model_name}Shout".constantize
21
+ rescue NameError
22
+ return
23
+ end
24
+
25
+ klass.new(record).try(action)
26
+ end
27
+ end
28
+
29
+ class ActiveRecord::Base
30
+ include ShoutOut
31
+ end
data/shout_out.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shout_out/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'shout_out'
8
+ spec.version = ShoutOut::VERSION
9
+ spec.authors = ['Kevin Pheasey']
10
+ spec.email = ['kevin.pheasey@gmail.com']
11
+ spec.summary = %q{Complex ActiveRecord callback abstraction.}
12
+ spec.description = %q{Abstract complex ActiveRecord callbacks into Shouts.}
13
+ spec.homepage = 'https://github.com/kpheasey/shout_out'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+
24
+ spec.add_dependency 'rails', '>= 4.0', '< 5'
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shout_out
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Pheasey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '5'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '4.0'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '5'
61
+ description: Abstract complex ActiveRecord callbacks into Shouts.
62
+ email:
63
+ - kevin.pheasey@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - lib/generators/shout_out/install/install_generator.rb
74
+ - lib/generators/shout_out/install/templates/application_shout.rb
75
+ - lib/generators/shout_out/shout/shout_generator.rb
76
+ - lib/generators/shout_out/shout/templates/shout.rb
77
+ - lib/shout_out.rb
78
+ - lib/shout_out/version.rb
79
+ - shout_out.gemspec
80
+ homepage: https://github.com/kpheasey/shout_out
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Complex ActiveRecord callback abstraction.
104
+ test_files: []