concerning 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/concerning.rb +126 -0
  3. metadata +71 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ae95d221d4682a4bb83d1b53f3df44e91c37660
4
+ data.tar.gz: 85c22eb4fc7222dcc4bf3fc6893c144b22c1e8b0
5
+ SHA512:
6
+ metadata.gz: ac20b05333437b935bec553252b173249e4b482b41765d74558dda792b1d4c386ab4766764d80b3a0b0212078f049130085d2dea49155bab599a2a029101d401
7
+ data.tar.gz: 14c9f905e06c003a3a39af3d34a78bd373a2b9641db0727fbbac77a7f122ff691d813cf7731713ab3aedda72a6f255cc2fd3b582d66a88d5d7d63eccc31a2435
@@ -0,0 +1,126 @@
1
+ class Module
2
+ # We often find ourselves with a medium-sized chunk of behavior that we'd
3
+ # like to extract, but only mix in to a single class.
4
+ #
5
+ # We typically choose to leave the implementation directly in the class,
6
+ # perhaps with a comment, because the mental and visual overhead of defining
7
+ # a module, making it a Concern, and including it is just too great.
8
+ #
9
+ #
10
+ # Using comments as lightweight modularity:
11
+ #
12
+ # class Todo
13
+ # # Other todo implementation
14
+ # # ...
15
+ #
16
+ # ## Event tracking
17
+ # has_many :events
18
+ #
19
+ # before_create :track_creation
20
+ # after_destroy :track_deletion
21
+ #
22
+ # private
23
+ # def track_creation
24
+ # # ...
25
+ # end
26
+ # end
27
+ #
28
+ #
29
+ # Trying on a noisy embedded module:
30
+ #
31
+ # class Todo
32
+ # # Other todo implementation
33
+ # # ...
34
+ #
35
+ # module EventTracking
36
+ # extend ActiveSupport::Concern
37
+ #
38
+ # included do
39
+ # has_many :events
40
+ # before_create :track_creation
41
+ # after_destroy :track_deletion
42
+ # end
43
+ #
44
+ # private
45
+ # def track_creation
46
+ # # ...
47
+ # end
48
+ # end
49
+ # include EventTracking
50
+ # end
51
+ #
52
+ #
53
+ # Once our chunk of behavior starts pushing the scroll-to-understand it
54
+ # boundary, we give in and move it to a separate file. At this size, the
55
+ # overhead feels in good proportion to the size of our extraction, despite
56
+ # diluting our at-a-glance sense of how things really work.
57
+ #
58
+ # Mix-in noise exiled to its own file:
59
+ #
60
+ # class Todo
61
+ # # Other todo implementation
62
+ # # ...
63
+ #
64
+ # include TodoEventTracking
65
+ # end
66
+ #
67
+ #
68
+ # Introducing Module#concerning.
69
+ #
70
+ # By quieting the mix-in noise, we arrive at a natural, low-ceremony way to
71
+ # do bite-sized modularity:
72
+ #
73
+ # class Todo
74
+ # # Other todo implementation
75
+ # # ...
76
+ #
77
+ # concerning :EventTracking do
78
+ # included do
79
+ # has_many :events
80
+ # before_create :track_creation
81
+ # after_destroy :track_deletion
82
+ # end
83
+ #
84
+ # private
85
+ # def track_creation
86
+ # # ...
87
+ # end
88
+ # end
89
+ # end
90
+ #
91
+ # Todo.ancestors
92
+ # # => Todo, Todo::EventTracking, Object
93
+ #
94
+ #
95
+ # This small step forward has some wonderful ripple effects. We can:
96
+ # * grok the behaviors that compose our class in one glance
97
+ # * clean up junk drawer classes by encapsulating their concerns
98
+ # * stop leaning on protected/private for "internal stuff" modularity
99
+ module Concerning
100
+ # Define a new concern and mix it in.
101
+ def concerning(topic, &block)
102
+ include concern(topic, &block)
103
+ end
104
+
105
+ # A low-cruft shortcut to define a concern.
106
+ #
107
+ # concern :EventTracking do
108
+ # ...
109
+ # end
110
+ #
111
+ # module EventTracking
112
+ # extend ActiveSupport::Concern
113
+ #
114
+ # ...
115
+ # end
116
+ def concern(topic, &module_definition)
117
+ require 'active_support/concern'
118
+
119
+ const_set topic, Module.new {
120
+ extend ActiveSupport::Concern
121
+ module_eval(&module_definition)
122
+ }
123
+ end
124
+ end
125
+ include Concerning
126
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: concerning
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Kemper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: jeremy@37signals.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ./lib/concerning.rb
48
+ homepage: https://github.com/37signals/concerning
49
+ licenses: []
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.0.14
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Separating small concerns
71
+ test_files: []