include_module 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -2
- data/README.md +13 -11
- data/include_module.gemspec +1 -1
- data/lib/include_module.rb +22 -38
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f098dae1198d8399acfe365f1d3ba26229306fc
|
4
|
+
data.tar.gz: 6e8948481ef0739e1d150210a0c8b3cb7e64a90c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c35479a55818b45f9218975927bc4ed7e42bcb38908ca2824503bb172d68d94e599ebd00b1c045de36470e983a9463615c7b0f0c86a8ed6a68db928ecbe890b
|
7
|
+
data.tar.gz: a7c6dbc6bc116a0f0f4bf87cbf2bddab021f32fe4f6a9d56877ac3902eac595601447de2f73b4f34fda97b0c4b7ca1602fe869b60d8cd8ca9079c2a5f2230cca
|
data/CHANGELOG.md
CHANGED
@@ -8,6 +8,30 @@ one of the following labels: `Added`, `Changed`, `Deprecated`,
|
|
8
8
|
Versioning](http://semver.org) to manage the versions of this gem so
|
9
9
|
that you can set version constraints properly.
|
10
10
|
|
11
|
-
|
11
|
+
#### [Unreleased](https://github.com/exAspArk/include_module/compare/v1.0.0...HEAD)
|
12
12
|
|
13
|
-
*
|
13
|
+
* WIP
|
14
|
+
|
15
|
+
#### [v1.0.0](https://github.com/exAspArk/include_module/compare/v0.1.0...v1.0.0) – 2016-09-16
|
16
|
+
|
17
|
+
* `Changed`: use `extend_module` for class methods:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class Foo
|
21
|
+
extend IncludeModule
|
22
|
+
extend_module Bar::ClassMethods, methods: :all
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
Instead of:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Foo
|
30
|
+
extend IncludeModule
|
31
|
+
include_module Bar, class_methods: :all
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
#### [v0.1.0](https://github.com/exAspArk/include_module/compare/9bfa492...v0.1.0) – 2016-08-16
|
36
|
+
|
37
|
+
* `Added`: initial functional version with docs.
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ them:
|
|
27
27
|
|
28
28
|
The **include_module** gem was created to help you to use mixing (aka `module` in Ruby)
|
29
29
|
explicitly.
|
30
|
-
It is a simple library with just
|
30
|
+
It is a simple library with just 90 LOC, with
|
31
31
|
zero dependencies, without any monkey patches and method overridings.
|
32
32
|
|
33
33
|
## Usage
|
@@ -103,16 +103,16 @@ Why can't we define explicitly which methods we would like to use from a mixin?
|
|
103
103
|
|
104
104
|
```ruby
|
105
105
|
module UserMixin
|
106
|
-
INCLUDED = proc do
|
107
|
-
belongs_to :account
|
108
|
-
end
|
109
|
-
|
110
106
|
module ClassMethods
|
111
107
|
def top_user
|
112
108
|
User.order(rating: :desc).first
|
113
109
|
end
|
114
110
|
end
|
115
111
|
|
112
|
+
INCLUDED = proc do
|
113
|
+
belongs_to :account
|
114
|
+
end
|
115
|
+
|
116
116
|
def name
|
117
117
|
"#{first_name} #{last_name}"
|
118
118
|
end
|
@@ -121,7 +121,8 @@ end
|
|
121
121
|
class User
|
122
122
|
extend IncludeModule
|
123
123
|
|
124
|
-
|
124
|
+
extend_module UserMixin::ClassMethods, methods: [:top_user]
|
125
|
+
include_module UserMixin, included: true, methods: [:name]
|
125
126
|
end
|
126
127
|
```
|
127
128
|
|
@@ -145,7 +146,8 @@ It is almost in-place replacement for `ActiveSupport::Concern`. Here is a diff:
|
|
145
146
|
+ extend IncludeModule
|
146
147
|
+
|
147
148
|
- include_module UserMixin
|
148
|
-
+
|
149
|
+
+ extend_module UserMixin::ClassMethods, methods: [:top_user]
|
150
|
+
+ include_module UserMixin, included: true, methods: [:name]
|
149
151
|
end
|
150
152
|
```
|
151
153
|
|
@@ -156,7 +158,7 @@ while including in order to avoid name clashes, etc. Please see more examples be
|
|
156
158
|
|
157
159
|
* Include no methods
|
158
160
|
|
159
|
-
When you add `extend IncludeModule`, you add just
|
161
|
+
When you add `extend IncludeModule`, you add just 2 public methods called `include_module` and `extend_module` in
|
160
162
|
your class.
|
161
163
|
|
162
164
|
```ruby
|
@@ -175,7 +177,7 @@ If you want to include everything from your module, you can pass the following o
|
|
175
177
|
```ruby
|
176
178
|
class User
|
177
179
|
extend IncludeModule
|
178
|
-
include_module UserMixin, included: true,
|
180
|
+
include_module UserMixin, included: true, methods: :all
|
179
181
|
end
|
180
182
|
```
|
181
183
|
|
@@ -199,7 +201,7 @@ end
|
|
199
201
|
|
200
202
|
class TestClass
|
201
203
|
extend IncludeModule
|
202
|
-
include_module TestModule,
|
204
|
+
include_module TestModule, methods: [:foo]
|
203
205
|
end
|
204
206
|
```
|
205
207
|
|
@@ -211,7 +213,7 @@ It basically creates a new anonymous module which contains only specified method
|
|
211
213
|
```ruby
|
212
214
|
class User
|
213
215
|
extend IncludeModule
|
214
|
-
include_module UserMixin,
|
216
|
+
include_module UserMixin, methods: [name: :full_name]
|
215
217
|
end
|
216
218
|
```
|
217
219
|
|
data/include_module.gemspec
CHANGED
data/lib/include_module.rb
CHANGED
@@ -1,40 +1,32 @@
|
|
1
1
|
module IncludeModule
|
2
|
-
def
|
2
|
+
def extend_module(new_module, methods: [])
|
3
|
+
return if methods.empty?
|
4
|
+
__extend_methods(new_module: new_module, method_names: methods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def include_module(new_module, methods: [], included: false)
|
3
8
|
if is_a?(Class)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
__include_class_methods_in_module(new_module: new_module, method_names: class_methods)
|
8
|
-
__store_included_block_in_module(new_module: new_module, included: included)
|
9
|
+
__include_included_blocks(new_module: new_module, included: included)
|
10
|
+
elsif is_a?(Module)
|
11
|
+
__store_included_block(new_module: new_module, included: included)
|
9
12
|
end
|
10
13
|
|
11
|
-
|
14
|
+
return if methods.empty?
|
15
|
+
__include_methods(new_module: new_module, method_names: methods)
|
12
16
|
end
|
13
17
|
|
14
18
|
private
|
15
19
|
|
16
|
-
def
|
17
|
-
return if method_names.empty?
|
18
|
-
class_methods_module = __class_methods_module(new_module)
|
19
|
-
|
20
|
+
def __extend_methods(new_module:, method_names:)
|
20
21
|
if method_names == :all
|
21
|
-
extend
|
22
|
+
extend(new_module)
|
22
23
|
else
|
23
|
-
|
24
|
+
new_mapped_module = __map_module_methods!(new_module: new_module.dup, method_names: method_names)
|
25
|
+
extend(new_mapped_module)
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
|
-
def
|
28
|
-
return if method_names.empty?
|
29
|
-
|
30
|
-
__include_instance_methods(
|
31
|
-
new_module: __class_methods_module(new_module),
|
32
|
-
method_names: method_names,
|
33
|
-
include_in: __class_methods_module(self)
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
def __include_included_blocks_in_class(new_module:, included:)
|
29
|
+
def __include_included_blocks(new_module:, included:)
|
38
30
|
return unless included
|
39
31
|
|
40
32
|
included_blocks = new_module.instance_variable_get(:@__included_blocks) || []
|
@@ -42,23 +34,23 @@ module IncludeModule
|
|
42
34
|
included_blocks.each { |included_block| class_eval(&included_block) }
|
43
35
|
end
|
44
36
|
|
45
|
-
def
|
37
|
+
def __store_included_block(new_module:, included:)
|
46
38
|
return unless included
|
47
39
|
|
48
40
|
@__included_blocks = new_module.instance_variable_get(:@__included_blocks) || []
|
49
41
|
@__included_blocks << new_module::INCLUDED if new_module.const_defined?(:INCLUDED)
|
50
42
|
end
|
51
43
|
|
52
|
-
def
|
44
|
+
def __include_methods(new_module:, method_names:)
|
53
45
|
if method_names == :all
|
54
|
-
|
46
|
+
include(new_module)
|
55
47
|
else
|
56
|
-
new_mapped_module =
|
57
|
-
|
48
|
+
new_mapped_module = __map_module_methods!(new_module: new_module.dup, method_names: method_names)
|
49
|
+
include(new_mapped_module)
|
58
50
|
end
|
59
51
|
end
|
60
52
|
|
61
|
-
def
|
53
|
+
def __map_module_methods!(new_module:, method_names:)
|
62
54
|
new_method_name_by_original_method_name = __new_method_name_by_original_method_name(method_names)
|
63
55
|
|
64
56
|
new_module.instance_methods.each do |original_method_name|
|
@@ -88,12 +80,4 @@ module IncludeModule
|
|
88
80
|
end
|
89
81
|
end
|
90
82
|
end
|
91
|
-
|
92
|
-
def __class_methods_module(new_module)
|
93
|
-
if new_module.const_defined?(:ClassMethods)
|
94
|
-
new_module.const_get(:ClassMethods)
|
95
|
-
else
|
96
|
-
new_module.const_set(:ClassMethods, Module.new)
|
97
|
-
end
|
98
|
-
end
|
99
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: include_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- exAspArk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
version: '0'
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project:
|
121
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.5.1
|
122
122
|
signing_key:
|
123
123
|
specification_version: 4
|
124
124
|
summary: Include your modules explicitly
|