much-plugin 0.0.1 → 0.1.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.
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  gem 'rake', "~> 10.4.0"
6
- gem 'pry', "~> 0.9.0"
6
+ gem 'pry', "~> 0.9.0"
File without changes
data/README.md CHANGED
@@ -1,10 +1,30 @@
1
1
  # MuchPlugin
2
2
 
3
- TODO: Write a gem description
3
+ An API to ensure mixin included logic (the "plugin") only runs once.
4
4
 
5
5
  ## Usage
6
6
 
7
- TODO: Write code samples and usage instructions here
7
+ ```ruby
8
+ requre 'much-plugin'
9
+
10
+ module MyPluginMixin
11
+ include MuchPlugin
12
+
13
+ plugin_included do
14
+
15
+ # ... do some stuff ...
16
+ # - will be class eval'd in the scope of the receiver of `MyPluginMixin`
17
+ # - will only be executed once per receiver, no matter how many times
18
+ # `MyPluginMixin` is included in that receiver
19
+
20
+ end
21
+
22
+ end
23
+ ```
24
+
25
+ Mix `MuchPlugin` in on other mixins that act as "plugins" to other components. Define included hooks using `plugin_included` that will be class eval'd in the scope of the receiver.
26
+
27
+ This allows you to define multiple hooks separately and ensures each hook will only be executed once - even if your plugin is mixed in multiple times on the same receiver.
8
28
 
9
29
  ## Installation
10
30
 
@@ -1,5 +1,36 @@
1
1
  require "much-plugin/version"
2
2
 
3
3
  module MuchPlugin
4
- # TODO: your code goes here...
4
+
5
+ def self.included(receiver)
6
+ receiver.class_eval do
7
+ extend ClassMethods
8
+
9
+ # install an included hook that first checks if this plugin has
10
+ # already been installed on the reciever. If it has not been,
11
+ # class eval each callback on the receiver.
12
+
13
+ def self.included(plugin_receiver)
14
+ return if self.much_plugin_receivers.include?(plugin_receiver)
15
+
16
+ self.much_plugin_receivers.push(plugin_receiver)
17
+ self.much_plugin_included_hooks.each do |hook|
18
+ plugin_receiver.class_eval(&hook)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ module ClassMethods
26
+
27
+ def much_plugin_receivers; @much_plugin_receivers ||= []; end
28
+ def much_plugin_included_hooks; @much_plugin_included_hooks ||= []; end
29
+
30
+ def plugin_included(&hook)
31
+ self.much_plugin_included_hooks << hook
32
+ end
33
+
34
+ end
35
+
5
36
  end
@@ -1,3 +1,3 @@
1
1
  module MuchPlugin
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = MuchPlugin::VERSION
9
9
  gem.authors = ["Kelly Redding", "Collin Redding"]
10
10
  gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
- gem.description = %q{Much plugin.}
12
- gem.summary = %q{Much plugin.}
11
+ gem.description = %q{An API to ensure mixin included logic (the "plugin") only runs once.}
12
+ gem.summary = %q{An API to ensure mixin included logic (the "plugin") only runs once.}
13
13
  gem.homepage = "http://github.com/redding/much-plugin"
14
14
  gem.license = 'MIT'
15
15
 
@@ -18,6 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_development_dependency("assert", ["~> 2.14"])
21
+ gem.add_development_dependency("assert", ["~> 2.15"])
22
22
 
23
23
  end
@@ -8,5 +8,3 @@ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
8
8
  require 'pry'
9
9
 
10
10
  require 'test/support/factory'
11
-
12
- # TODO: put test helpers here...
@@ -0,0 +1,101 @@
1
+ require 'assert'
2
+ require 'much-plugin'
3
+
4
+ module MuchPlugin
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "MuchPlugin"
8
+ setup do
9
+ @hook1 = proc{ 1 }
10
+ @hook2 = proc{ 2 }
11
+
12
+ @plugin = Module.new{ include MuchPlugin }
13
+ end
14
+ subject{ @plugin }
15
+
16
+ should have_imeths :much_plugin_included_hooks, :much_plugin_receivers
17
+ should have_imeths :plugin_included
18
+
19
+ should "have no plugin included hooks by default" do
20
+ assert_empty subject.much_plugin_included_hooks
21
+ end
22
+
23
+ should "have no plugin receivers by default" do
24
+ assert_empty subject.much_plugin_receivers
25
+ end
26
+
27
+ should "append hooks" do
28
+ subject.plugin_included(&@hook1)
29
+ subject.plugin_included(&@hook2)
30
+
31
+ assert_equal @hook1, subject.much_plugin_included_hooks.first
32
+ assert_equal @hook2, subject.much_plugin_included_hooks.last
33
+ end
34
+
35
+ end
36
+
37
+ class MixedInTests < UnitTests
38
+ desc "when mixed in"
39
+ setup do
40
+ @receiver = Class.new do
41
+ def self.inc_hook1; @hook1_count ||= 0; @hook1_count += 1; end
42
+ def self.hook1_count; @hook1_count ||= 0; end
43
+ def self.inc_hook2; @hook2_count ||= 0; @hook2_count += 1; end
44
+ def self.hook2_count; @hook2_count ||= 0; end
45
+ end
46
+ end
47
+
48
+ should "call the plugin included hooks" do
49
+ assert_equal 0, @receiver.hook1_count
50
+ assert_equal 0, @receiver.hook2_count
51
+
52
+ @receiver.send(:include, TestPlugin)
53
+
54
+ assert_equal 1, @receiver.hook1_count
55
+ assert_equal 1, @receiver.hook2_count
56
+ end
57
+
58
+ should "call hooks only once no matter even if previously mixed in" do
59
+ @receiver.send(:include, TestPlugin)
60
+
61
+ assert_equal 1, @receiver.hook1_count
62
+ assert_equal 1, @receiver.hook2_count
63
+
64
+ @receiver.send(:include, TestPlugin)
65
+
66
+ assert_equal 1, @receiver.hook1_count
67
+ assert_equal 1, @receiver.hook2_count
68
+ end
69
+
70
+ should "call hooks only once even if mixed in by a 3rd party" do
71
+ third_party = Module.new do
72
+ def self.included(receiver)
73
+ receiver.send(:include, TestPlugin)
74
+ end
75
+ end
76
+ @receiver.send(:include, third_party)
77
+
78
+ assert_equal 1, @receiver.hook1_count
79
+ assert_equal 1, @receiver.hook2_count
80
+
81
+ @receiver.send(:include, TestPlugin)
82
+
83
+ assert_equal 1, @receiver.hook1_count
84
+ assert_equal 1, @receiver.hook2_count
85
+
86
+ @receiver.send(:include, third_party)
87
+
88
+ assert_equal 1, @receiver.hook1_count
89
+ assert_equal 1, @receiver.hook2_count
90
+ end
91
+
92
+ TestPlugin = Module.new do
93
+ include MuchPlugin
94
+
95
+ plugin_included{ inc_hook1 }
96
+ plugin_included{ inc_hook2 }
97
+ end
98
+
99
+ end
100
+
101
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: much-plugin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2015-07-17 00:00:00 Z
19
+ date: 2015-11-24 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -24,16 +24,16 @@ dependencies:
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
- hash: 31
27
+ hash: 29
28
28
  segments:
29
29
  - 2
30
- - 14
31
- version: "2.14"
30
+ - 15
31
+ version: "2.15"
32
32
  type: :development
33
33
  name: assert
34
34
  version_requirements: *id001
35
35
  prerelease: false
36
- description: Much plugin.
36
+ description: An API to ensure mixin included logic (the "plugin") only runs once.
37
37
  email:
38
38
  - kelly@kellyredding.com
39
39
  - collin.redding@me.com
@@ -46,7 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - .gitignore
48
48
  - Gemfile
49
- - LICENSE.txt
49
+ - LICENSE
50
50
  - README.md
51
51
  - Rakefile
52
52
  - lib/much-plugin.rb
@@ -55,6 +55,7 @@ files:
55
55
  - much-plugin.gemspec
56
56
  - test/helper.rb
57
57
  - test/support/factory.rb
58
+ - test/unit/much-plugin_tests.rb
58
59
  - tmp/.gitkeep
59
60
  homepage: http://github.com/redding/much-plugin
60
61
  licenses:
@@ -88,7 +89,8 @@ rubyforge_project:
88
89
  rubygems_version: 1.8.29
89
90
  signing_key:
90
91
  specification_version: 3
91
- summary: Much plugin.
92
+ summary: An API to ensure mixin included logic (the "plugin") only runs once.
92
93
  test_files:
93
94
  - test/helper.rb
94
95
  - test/support/factory.rb
96
+ - test/unit/much-plugin_tests.rb