much-plugin 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- data.tar.gz: 84f275cfa293994f90ba140878ad4ed41eb9d858
4
- metadata.gz: 260ffbd14617020d9552de0c32cc715e31e40964
3
+ metadata.gz: 7f75f3177a2f12db73711551a7ae1c1f8676addf
4
+ data.tar.gz: 5fe30d9b7dc34dcb998a1a47b7ea37e1f3b0de80
5
5
  SHA512:
6
- data.tar.gz: 07044deac53c6a4bc29ff6fcc7e58a099e404f1dff5bef1195be897d03e916e8da6f56f76f61ebfdf953dc90d0e67befcad1d08080936e9a9114105255f5dc1d
7
- metadata.gz: c5459f146e1162ba23cb234bdedcc2e3126985eddf1be2308da30a099f4453fd836f502684483b48c73c04727d7b79681badb1d80545a1c405d40e80eca9539a
6
+ metadata.gz: 9792123b848b0fb119f42d7fc13ba15ec2d7af55eab316137f3b4a782912f830668859bcc6683ace7827920ee72000178133b6f92534d2448788f0a73b4c77a6
7
+ data.tar.gz: cb9a718201fa65522e4570ba8cee508b2c7818ccab7e99c50aa52c52ea666b016f9330202b5ca28bf33b7d1aca5a6f0f77e74cd3a8cb762949ecc136184315eb
@@ -0,0 +1,27 @@
1
+ require 'much-plugin'
2
+ require 'benchmark'
3
+
4
+ module Methods; end
5
+
6
+ module MyMixin
7
+ def self.included(receiver)
8
+ receiver.class_eval{ include Methods }
9
+ end
10
+ end
11
+
12
+ module MyPlugin
13
+ include MuchPlugin
14
+
15
+ plugin_included do
16
+ include Methods
17
+ end
18
+ end
19
+
20
+ Benchmark.bmbm do |x|
21
+ x.report("MyMixin") do
22
+ 10_000.times{ Class.new{ include MyMixin } }
23
+ end
24
+ x.report("MyPlugin") do
25
+ 10_000.times{ Class.new{ include MyPlugin } }
26
+ end
27
+ end
@@ -3,28 +3,34 @@ require "much-plugin/version"
3
3
  module MuchPlugin
4
4
 
5
5
  def self.included(receiver)
6
- receiver.class_eval do
7
- extend ClassMethods
6
+ receiver.class_eval{ extend ClassMethods }
7
+ end
8
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.
9
+ module ClassMethods
12
10
 
13
- def self.included(plugin_receiver)
14
- return if self.much_plugin_receivers.include?(plugin_receiver)
11
+ # install an included hook that first checks if this plugin's receiver mixin
12
+ # has already been included. If it has not been, include the receiver mixin
13
+ # and run all of the `plugin_included` hooks
14
+ def included(plugin_receiver)
15
+ return if plugin_receiver.include?(self.much_plugin_included_detector)
16
+ plugin_receiver.send(:include, self.much_plugin_included_detector)
15
17
 
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
18
+ self.much_plugin_included_hooks.each do |hook|
19
+ plugin_receiver.class_eval(&hook)
20
20
  end
21
-
22
21
  end
23
- end
24
22
 
25
- module ClassMethods
23
+ # the included detector is an empty module that is only used to detect if
24
+ # the plugin has been included or not, it doesn't add any behavior or
25
+ # methods to the object receiving the plugin; we use `const_set` to name the
26
+ # module so if its seen in the ancestors it doesn't look like some random
27
+ # module and it can be tracked back to much-plugin
28
+ def much_plugin_included_detector
29
+ @much_plugin_included_detector ||= Module.new.tap do |m|
30
+ self.const_set("MuchPluginIncludedDetector", m)
31
+ end
32
+ end
26
33
 
27
- def much_plugin_receivers; @much_plugin_receivers ||= []; end
28
34
  def much_plugin_included_hooks; @much_plugin_included_hooks ||= []; end
29
35
 
30
36
  def plugin_included(&hook)
@@ -1,3 +1,3 @@
1
1
  module MuchPlugin
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,11 +1,11 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require File.expand_path("../lib/much-plugin/version", __FILE__)
4
+ require "much-plugin/version"
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "much-plugin"
8
- gem.version = MuchPlugin::VERSION
8
+ gem.version = "0.2.0" #MuchPlugin::VERSION
9
9
  gem.authors = ["Kelly Redding", "Collin Redding"]
10
10
  gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
11
  gem.summary = %q{An API to ensure mixin included logic (the "plugin") only runs once.}
@@ -13,15 +13,19 @@ module MuchPlugin
13
13
  end
14
14
  subject{ @plugin }
15
15
 
16
- should have_imeths :much_plugin_included_hooks, :much_plugin_receivers
16
+ should have_imeths :much_plugin_included_detector, :much_plugin_included_hooks
17
17
  should have_imeths :plugin_included
18
18
 
19
- should "have no plugin included hooks by default" do
20
- assert_empty subject.much_plugin_included_hooks
19
+ should "know its included detector" do
20
+ mixin = subject.much_plugin_included_detector
21
+ assert_instance_of Module, mixin
22
+ assert_same mixin, subject.much_plugin_included_detector
23
+ exp = subject::MuchPluginIncludedDetector
24
+ assert_same exp, subject.much_plugin_included_detector
21
25
  end
22
26
 
23
- should "have no plugin receivers by default" do
24
- assert_empty subject.much_plugin_receivers
27
+ should "have no plugin included hooks by default" do
28
+ assert_empty subject.much_plugin_included_hooks
25
29
  end
26
30
 
27
31
  should "append hooks" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: much-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2016-06-01 00:00:00 Z
13
+ date: 2016-06-03 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: assert
@@ -37,6 +37,7 @@ files:
37
37
  - Gemfile
38
38
  - LICENSE
39
39
  - README.md
40
+ - bench/script.rb
40
41
  - lib/much-plugin.rb
41
42
  - lib/much-plugin/version.rb
42
43
  - log/.gitkeep
@@ -67,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  requirements: []
68
69
 
69
70
  rubyforge_project:
70
- rubygems_version: 2.6.4
71
+ rubygems_version: 2.5.1
71
72
  signing_key:
72
73
  specification_version: 4
73
74
  summary: An API to ensure mixin included logic (the "plugin") only runs once.