concerning 1.0.2 → 1.0.3
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
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46300e9d1f6336b137f30a7ce604b609af70949b
|
4
|
+
data.tar.gz: 5af204914956c879ab923da970083186eed8ea5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8134f6abaf203e3d1d3e8397ceb14e2868307a07051f0f5dbffb6f72c7f32997537b5ff39361ef58b6aaf180a2039c3166d1967dd410149f5cabea568bfe28b9
|
7
|
+
data.tar.gz: 99bd868353c2e2d28e5f2b7c22ef2f4c03da6dafd5fbfb58b63325640a0ef61fcaf3005784b2a7eff4b1b27fb6f1ce9f83341320fea565f10c23afb1e8462dd3
|
data/lib/concerning.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
1
3
|
class Module
|
2
4
|
# We often find ourselves with a medium-sized chunk of behavior that we'd
|
3
5
|
# like to extract, but only mix in to a single class.
|
@@ -113,35 +115,41 @@ class Module
|
|
113
115
|
# * clean up junk drawer classes by encapsulating their concerns
|
114
116
|
# * stop leaning on protected/private for "internal stuff" modularity
|
115
117
|
module Concerning
|
116
|
-
# Define a new concern and mix it in.
|
117
118
|
def concerning(topic, &block)
|
118
119
|
include concern(topic, &block)
|
119
120
|
end
|
121
|
+
end
|
122
|
+
include Concerning # We do this mixin just for nice RDoc.
|
123
|
+
end
|
120
124
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
+
module Kernel
|
126
|
+
# A low-cruft shortcut to define a concern.
|
127
|
+
#
|
128
|
+
# concern :EventTracking do
|
129
|
+
# ...
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# module EventTracking
|
133
|
+
# extend ActiveSupport::Concern
|
134
|
+
#
|
135
|
+
# ...
|
136
|
+
# end
|
137
|
+
def concern(topic, &module_definition)
|
138
|
+
const_set topic, Module.new {
|
139
|
+
extend ActiveSupport::Concern
|
140
|
+
module_eval(&module_definition)
|
141
|
+
}
|
142
|
+
end
|
143
|
+
end
|
125
144
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
# extend ActiveSupport::Concern
|
134
|
-
#
|
135
|
-
# ...
|
136
|
-
# end
|
137
|
-
def concern(topic, &module_definition)
|
138
|
-
require 'active_support/concern'
|
145
|
+
# Provide a class_methods alternative to ClassMethods since defining
|
146
|
+
# a constant within a block doesn't work as folks would expect.
|
147
|
+
ActiveSupport::Concern.module_eval do
|
148
|
+
def class_methods(&class_methods_module_definition)
|
149
|
+
mod = const_defined?(:ClassMethods) ?
|
150
|
+
const_get(:ClassMethods) :
|
151
|
+
const_set(:ClassMethods, Module.new)
|
139
152
|
|
140
|
-
|
141
|
-
|
142
|
-
module_eval(&module_definition)
|
143
|
-
}
|
144
|
-
end
|
145
|
-
end
|
146
|
-
include Concerning
|
153
|
+
mod.module_eval(&class_methods_module_definition)
|
154
|
+
end unless method_defined? :class_methods
|
147
155
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
CONCERNING_DEFERRED_TO_ACTIVE_SUPPORT = true
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
$VERBOSE = true
|
5
|
+
|
6
|
+
require 'concerning'
|
7
|
+
|
8
|
+
class ConcerningTest < Minitest::Test
|
9
|
+
def test_concerning_declares_a_concern_and_includes_it_immediately
|
10
|
+
klass = Class.new { concerning(:Foo) { } }
|
11
|
+
assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ConcernTest < Minitest::Test
|
16
|
+
def test_concern_creates_a_module_extended_with_active_support_concern
|
17
|
+
klass = Class.new do
|
18
|
+
concern :Foo do
|
19
|
+
included { @foo = 1 }
|
20
|
+
def should_be_public; end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Declares a concern but doesn't include it
|
25
|
+
assert_kind_of ActiveSupport::Concern, klass::Foo
|
26
|
+
assert !klass.ancestors.include?(klass::Foo), klass.ancestors.inspect
|
27
|
+
|
28
|
+
# Public method visibility by default
|
29
|
+
assert klass::Foo.public_instance_methods.map(&:to_s).include?('should_be_public')
|
30
|
+
|
31
|
+
# Calls included hook
|
32
|
+
assert_equal 1, Class.new { include klass::Foo }.instance_variable_get('@foo')
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_may_be_defined_at_toplevel
|
36
|
+
mod = Kernel.concern(:Foo) { }
|
37
|
+
assert_equal mod, ::Foo
|
38
|
+
assert_kind_of ActiveSupport::Concern, ::Foo
|
39
|
+
assert !Object.ancestors.include?(::Foo), mod.ancestors.inspect
|
40
|
+
end
|
41
|
+
|
42
|
+
class Foo
|
43
|
+
concerning :Bar do
|
44
|
+
module ClassMethods
|
45
|
+
def will_be_orphaned; end
|
46
|
+
end
|
47
|
+
|
48
|
+
const_set :ClassMethods, Module.new {
|
49
|
+
def hacked_on; end
|
50
|
+
}
|
51
|
+
|
52
|
+
# Doesn't overwrite existing ClassMethods module.
|
53
|
+
class_methods do
|
54
|
+
def nicer_dsl; end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Doesn't overwrite previous class_methods definitions.
|
58
|
+
class_methods do
|
59
|
+
def doesnt_clobber; end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_using_class_methods_blocks_instead_of_ClassMethods_module
|
65
|
+
assert !Foo.respond_to?(:will_be_orphaned)
|
66
|
+
assert Foo.respond_to?(:hacked_on)
|
67
|
+
assert Foo.respond_to?(:nicer_dsl)
|
68
|
+
assert Foo.respond_to?(:doesnt_clobber)
|
69
|
+
|
70
|
+
# Orphan in Foo::ClassMethods, not Bar::ClassMethods.
|
71
|
+
assert Foo.const_defined?(:ClassMethods)
|
72
|
+
assert Foo::ClassMethods.method_defined?(:will_be_orphaned)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Put a fake Active Support implementation in the load path to verify that
|
77
|
+
# we defer to it when we can.
|
78
|
+
class ForwardCompatibilityWithRails41Test < Minitest::Test
|
79
|
+
def setup; expunge_loaded_features end
|
80
|
+
def teardown; expunge_loaded_features end
|
81
|
+
|
82
|
+
def test_check_for_active_support_implementation_before_providing_our_own
|
83
|
+
with_stubbed_active_support_in_load_path do
|
84
|
+
require 'concerning'
|
85
|
+
end
|
86
|
+
assert defined?(::CONCERNING_DEFERRED_TO_ACTIVE_SUPPORT)
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
def expunge_loaded_features
|
91
|
+
$LOADED_FEATURES.delete_if { |feature| feature =~ /concerning/ }
|
92
|
+
end
|
93
|
+
|
94
|
+
def with_stubbed_active_support_in_load_path
|
95
|
+
$LOAD_PATH.unshift File.expand_path('../active_support_stub', __FILE__)
|
96
|
+
yield
|
97
|
+
ensure
|
98
|
+
$LOAD_PATH.shift
|
99
|
+
end
|
100
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: concerning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Kemper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,24 +28,26 @@ dependencies:
|
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '5.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '5.1'
|
41
41
|
description:
|
42
42
|
email: jeremy@basecamp.com
|
43
43
|
executables: []
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
-
- ./lib/concerning/
|
47
|
+
- ./lib/concerning/extension.rb
|
48
48
|
- ./lib/concerning.rb
|
49
|
+
- ./test/active_support_stub/active_support/core_ext/module/concerning.rb
|
50
|
+
- ./test/concerning_test.rb
|
49
51
|
homepage: https://github.com/basecamp/concerning
|
50
52
|
licenses:
|
51
53
|
- MIT
|
@@ -70,4 +72,6 @@ rubygems_version: 2.1.11
|
|
70
72
|
signing_key:
|
71
73
|
specification_version: 4
|
72
74
|
summary: Separating small concerns
|
73
|
-
test_files:
|
75
|
+
test_files:
|
76
|
+
- ./test/active_support_stub/active_support/core_ext/module/concerning.rb
|
77
|
+
- ./test/concerning_test.rb
|