include_module 0.1.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de37cd68ed47d17ca31835c5e3e413a66f4a9b13
4
- data.tar.gz: 163712749ec437588906a13f530ff4978c02918a
3
+ metadata.gz: 3f098dae1198d8399acfe365f1d3ba26229306fc
4
+ data.tar.gz: 6e8948481ef0739e1d150210a0c8b3cb7e64a90c
5
5
  SHA512:
6
- metadata.gz: 29cb5b888d56f91b354662b3e62df476ef39932bced2a5f67a060e2d68a53e8c2d80b87ee324fb966b7e8cf5d761f860853efa57cd0d019d1be2bceaed740ea3
7
- data.tar.gz: 775acf4c01dcf37fb824412839259cf0cb1cd4c986bbb39daba8af0fd6e4f1b9f58b971e546443f1487f9baae4d8f7f01edaa97855c33d90f4307f823cd82d58
6
+ metadata.gz: 0c35479a55818b45f9218975927bc4ed7e42bcb38908ca2824503bb172d68d94e599ebd00b1c045de36470e983a9463615c7b0f0c86a8ed6a68db928ecbe890b
7
+ data.tar.gz: a7c6dbc6bc116a0f0f4bf87cbf2bddab021f32fe4f6a9d56877ac3902eac595601447de2f73b4f34fda97b0c4b7ca1602fe869b60d8cd8ca9079c2a5f2230cca
@@ -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
- ### [Unreleased](https://github.com/exAspArk/include_module/compare/9bfa492...HEAD)
11
+ #### [Unreleased](https://github.com/exAspArk/include_module/compare/v1.0.0...HEAD)
12
12
 
13
- * `Added`: initial functional version with docs
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 100 LOC, with
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
- include_module UserMixin, included: true, class_methods: [:top_user], instance_methods: [:name]
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
- + include_module UserMixin, included: true, class_methods: [:top_user], instance_methods: [:name]
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 one public method called `include_module` in
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, class_methods: :all, instance_methods: :all
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, instance_methods: [:foo]
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, instance_methods: [name: :full_name]
216
+ include_module UserMixin, methods: [name: :full_name]
215
217
  end
216
218
  ```
217
219
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "include_module"
7
- spec.version = "0.1.0"
7
+ spec.version = "1.0.0"
8
8
  spec.authors = ["exAspArk"]
9
9
  spec.email = ["exaspark@gmail.com"]
10
10
 
@@ -1,40 +1,32 @@
1
1
  module IncludeModule
2
- def include_module(new_module, instance_methods: [], class_methods: [], included: false)
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
- __include_class_methods_in_class(new_module: new_module, method_names: class_methods)
5
- __include_included_blocks_in_class(new_module: new_module, included: included)
6
- else
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
- __include_instance_methods(new_module: new_module, method_names: instance_methods)
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 __include_class_methods_in_class(new_module:, method_names:)
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 class_methods_module
22
+ extend(new_module)
22
23
  else
23
- extend __map_module_instance_methods!(new_module: class_methods_module.dup, method_names: method_names)
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 __include_class_methods_in_module(new_module:, method_names:)
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 __store_included_block_in_module(new_module:, included:)
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 __include_instance_methods(new_module:, method_names:, include_in: self)
44
+ def __include_methods(new_module:, method_names:)
53
45
  if method_names == :all
54
- include_in.include(new_module)
46
+ include(new_module)
55
47
  else
56
- new_mapped_module = __map_module_instance_methods!(new_module: new_module.dup, method_names: method_names)
57
- include_in.include(new_mapped_module)
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 __map_module_instance_methods!(new_module:, method_names:)
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: 0.1.0
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-08-21 00:00:00.000000000 Z
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.4.5.1
121
+ rubygems_version: 2.5.1
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: Include your modules explicitly