beholders 1.0.0

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: b9c40da88f21b467f2c08f4b42f5263cc139f124
4
+ data.tar.gz: 760eccd15ce7a1629d47fa24a10a2d209a374122
5
+ SHA512:
6
+ metadata.gz: 02f47a0450a1cbfb80c69fbc40521ba4e3382392e2686c7e0b5aa74aa0fed90cde467e4827f471752243b5aebdc356ba3bf96b68fb2758e70a83e6e309a772ed
7
+ data.tar.gz: f7bd366e5f947c575eae87cba5977cc099979cac3e3cb2c91d7ec7f72193d6001ef3ea2debc8492ec45330fdbe7309bbbd8e559940795c66d0bd2e415a0f0a2f
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2
5
+ - 2.3
6
+ - 2.4
7
+ gemfile:
8
+ - gemfiles/rails_4_0.gemfile
9
+ - gemfiles/rails_4_2.gemfile
10
+ - gemfiles/rails_5_0.gemfile
11
+ - Gemfile
12
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jfarey@yozu.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in beholders.gemspec
6
+ gemspec
7
+
8
+ gem 'rails', "~> 5.1"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Farey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,115 @@
1
+ [![Build Status](https://travis-ci.org/Bogadon/beholders.svg?branch=master)](https://travis-ci.org/Bogadon/beholders) [![Maintainability](https://api.codeclimate.com/v1/badges/68dc74886f0f54f3be5e/maintainability)](https://codeclimate.com/github/Bogadon/beholders/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/68dc74886f0f54f3be5e/test_coverage)](https://codeclimate.com/github/Bogadon/beholders/test_coverage)
2
+
3
+ # Beholders
4
+
5
+ A lightweight implementation of the [observer pattern] for rails active record models.
6
+
7
+ Originally developed as an alternative to [rails-observers] with the goal of:
8
+ - Better support for current versions of rails.
9
+ - Encourage the [single responsibility principle] in design- beholders are explicitly added by name to a model, rather than inferred based on its name. This lets you have multiple observers each named for their specific domain.
10
+
11
+ Supports rails 4.0+ (tested up to 5.1).
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'beholders'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install beholders
28
+
29
+ ## Usage
30
+
31
+ Getting started example:
32
+
33
+ ```ruby
34
+ # Observers are added to a model like:
35
+ class Train < ApplicationRecord
36
+ observed_by "TimelineManager"
37
+ observed_by "PlatformAnnouncer"
38
+ observed_by "Planner::RisksCacher"
39
+ end
40
+
41
+ # Your observers inherit from Beholder
42
+ class TimelineManager < Beholder
43
+ # Define public methods for each callback hook you want to trigger on
44
+ # They should accept 1 arg, the instance of an observed model
45
+ def after_create_commit(subject)
46
+ TimelineEntry.create!(subject: subject)
47
+ end
48
+ end
49
+
50
+ class PlatformAnnouncer < Beholder
51
+ def after_update_commit(subject)
52
+ return unless subject.previous_changes.include? :arrival_time
53
+ # broadcast an event to your action cable channel
54
+ end
55
+ end
56
+
57
+ ```
58
+
59
+ ActiveRecord::Base class methods:
60
+
61
+ Class names are passed as a string to prevent redundant autoloading.
62
+ ```ruby
63
+ observed_by "BeholderClassName"
64
+ ```
65
+
66
+ Callback hooks:
67
+
68
+ Define as public methods with a single argument (model instance) in your beholders to trigger during that callback stage of the model.
69
+ ```ruby
70
+ after_save
71
+ after_create
72
+ after_update
73
+ after_destroy
74
+
75
+ after_commit
76
+ after_create_commit
77
+ after_update_commit
78
+ after_destroy_commit
79
+ ```
80
+
81
+ Beholders are enabled by default and can be disabled/enabled individually and globally, which is probably most useful for test isolation. The following class methods are available:
82
+
83
+ ```ruby
84
+ class MyBeholder < Beholder; end
85
+
86
+ MyBeholder.disable! # disable MyBeholder
87
+ MyBeholder.disabled? # true
88
+ MyBeholder.enable! # enable MyBeholder
89
+ MyBeholder.disabled? # false
90
+
91
+ Beholder.disable_all! # disable Beholder and all descendant classes
92
+ Beholder.enable_all! # enable Beholder and all descendant classes
93
+ ```
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Bogadon/beholders. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the Beholders project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Bogadon/beholders/blob/master/CODE_OF_CONDUCT.md).
112
+
113
+ [observer pattern]: https://en.wikipedia.org/wiki/Observer_pattern
114
+ [rails-observers]: https://github.com/rails/rails-observers
115
+ [single responsibility principle]: https://en.wikipedia.org/wiki/Single_responsibility_principle
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "beholders/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "beholders"
8
+ spec.version = Beholders::VERSION
9
+ spec.authors = ["John Farey"]
10
+ spec.email = ["jfarey@yozu.co.uk", "kazimierzz@gmail.com"]
11
+
12
+ spec.summary = %q{Lightweight SRP encouraging observers for rails}
13
+ spec.description = %q{Add single responsibility observers to your rails models}
14
+ spec.homepage = "https://www.github.com/Bogadon"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activerecord", ">= 3"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "sqlite3", "~> 1.3"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+
31
+ spec.add_development_dependency "simplecov"
32
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "beholders"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'rails', "~> 4.0"
6
+
7
+ # Specify your gem's dependencies in beholders.gemspec
8
+ gemspec path: "../"
9
+
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'rails', "~> 4.2"
6
+
7
+ # Specify your gem's dependencies in beholders.gemspec
8
+ gemspec path: "../"
9
+
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'rails', "~> 5.0"
6
+
7
+ # Specify your gem's dependencies in beholders.gemspec
8
+ gemspec path: "../"
9
+
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "beholders/version"
4
+
5
+ require "beholders/railtie"
6
+
7
+ require "beholders/beholder"
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Beholder
4
+
5
+ def self.inherited(subclass)
6
+ super
7
+ subclass.disabled = subclass.superclass.disabled?
8
+ end
9
+
10
+ def self.disable!
11
+ @disabled = true
12
+ end
13
+
14
+ def self.enable!
15
+ @disabled = false
16
+ end
17
+
18
+ def self.disabled?
19
+ @disabled
20
+ end
21
+
22
+ def self.disabled=(value)
23
+ @disabled = value
24
+ end
25
+
26
+ def self.self_and_descendants
27
+ [self] + descendants
28
+ end
29
+
30
+ def self.enable_all!
31
+ self_and_descendants.each(&:enable!)
32
+ end
33
+
34
+ def self.disable_all!
35
+ self_and_descendants.each(&:disable!)
36
+ end
37
+
38
+ def self.trigger(action, model)
39
+ return if disabled?
40
+ new.public_send(action, model) if method_defined? action
41
+ end
42
+
43
+ end
44
+
@@ -0,0 +1,41 @@
1
+ module Beholders
2
+ module CallbackHooks
3
+ BEHOLDER_CB_RAILS_3 = %i[
4
+ after_save
5
+ after_create
6
+ after_update
7
+ after_destroy
8
+ after_commit
9
+ ].freeze
10
+
11
+ BEHOLDER_CB_RAILS_5 = [
12
+ *BEHOLDER_CB_RAILS_3,
13
+ :after_create_commit,
14
+ :after_update_commit,
15
+ :after_destroy_commit
16
+ ].freeze
17
+
18
+ # Pass class name as string not class, for same reason rails 5.1 deprecates
19
+ # the latter:
20
+ # https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md
21
+ def observed_by(observer)
22
+ instance_eval do
23
+ available_callbacks(self).each do |cb|
24
+ send cb, -> { observer.constantize.trigger(cb, self) }
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def available_callbacks(klass)
32
+ if klass.respond_to?(:after_create_commit)
33
+ BEHOLDER_CB_RAILS_5
34
+ else
35
+ BEHOLDER_CB_RAILS_3
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ require 'beholders/callback_hooks'
6
+
7
+ module Beholders
8
+
9
+ class Railtie < Rails::Railtie
10
+ initializer 'beholders.add_to_active_record' do
11
+ ActiveSupport.on_load :active_record do
12
+ ActiveRecord::Base.send(:extend, CallbackHooks)
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Beholders
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beholders
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Farey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
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.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
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
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Add single responsibility observers to your rails models
98
+ email:
99
+ - jfarey@yozu.co.uk
100
+ - kazimierzz@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - beholders.gemspec
114
+ - bin/console
115
+ - bin/setup
116
+ - gemfiles/rails_4_0.gemfile
117
+ - gemfiles/rails_4_2.gemfile
118
+ - gemfiles/rails_5_0.gemfile
119
+ - lib/beholders.rb
120
+ - lib/beholders/beholder.rb
121
+ - lib/beholders/callback_hooks.rb
122
+ - lib/beholders/railtie.rb
123
+ - lib/beholders/version.rb
124
+ homepage: https://www.github.com/Bogadon
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.6.11
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Lightweight SRP encouraging observers for rails
148
+ test_files: []