compound 0.0.2 → 0.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: f4f1025df16249c199852986752859e1182992dc
4
- data.tar.gz: 4cf21ac8e1adaf7c9b2d86f62437b6e73d5412e9
3
+ metadata.gz: 98be6fb9009b88f4ac28236382c858a5a837f67d
4
+ data.tar.gz: aebd4ba505f4fa915f9b5b133c80ecc87f5216aa
5
5
  SHA512:
6
- metadata.gz: f4a198d31d92fd0e91c46b8519d2592177d524e486e6df2f5481df9303cf6f5f3539d804c62a74f1c3ce8c3e40590ffe5d2e8137e422ea66f4676935205372d8
7
- data.tar.gz: 4566764d60336ee085f208c74bf5b1c5d63cbd8084ca5712902c94c69ff21c4d1c3d4d50f56b61dba77f8c3f7c4b97cf1d613d2727cadd6547593898e92b6384
6
+ metadata.gz: 7d2afc4bf916ff16a207545e27a2e5c6e8842edca0dbf5d58711f6c42bda320cb8657930d75bfa99b548fd7ad428dc189df4c68cd0b7eb7476884197ee1a3c04
7
+ data.tar.gz: fcdece2cbb55db2e6cccb7e054250af4b09639d19b92a6568ff4ed238d2479249ec89a4befb1e8a0cb5a92caa2af0de2f7ac59a81ff18ba9be1cf597574c3cdb
data/README.md CHANGED
@@ -215,10 +215,11 @@ can initialize its internal state. Traditional module composition will call
215
215
  the `extended` or `self.included` methods if they are defined by the module.
216
216
  However, modules intended for compounding should define a `compounded` method
217
217
  instead, to be called upon creation of the `Host`'s internal `Part` object.
218
+ The `compounded` method should accept a single argument: the `Host` object.
218
219
 
219
220
  ``` ruby
220
221
  module Paint
221
- def compounded
222
+ def compounded(host)
222
223
  @color = :royal_blue
223
224
  end
224
225
  attr_accessor :color
@@ -227,3 +228,46 @@ instead, to be called upon creation of the `Host`'s internal `Part` object.
227
228
  host.compound Paint
228
229
  host.color #=> :royal_blue
229
230
  ```
231
+
232
+ The `Host` will also pass itself to the `self.compounded` method on the module
233
+ passed to `compound` if the method is defined.
234
+
235
+ ``` ruby
236
+ module Paint
237
+ def self.compounded(host)
238
+ @latest = host
239
+ end
240
+ attr_accessor :latest
241
+ end
242
+
243
+ Paint.latest #=> nil
244
+ host.compound Paint
245
+ Paint.latest == host #=> true
246
+ ```
247
+
248
+ # Calling a Method on Each Part
249
+
250
+ Compound::Host has a private method for calling `send` on each of its `Part`
251
+ objects. Because it uses `send`, it can access both public and private methods
252
+ of each `Part`. The return values of the methods are collected into a hash
253
+ with the compounded modules as the keys.
254
+
255
+ ``` ruby
256
+ module RedPaint
257
+ private
258
+ def load_brush(intensity)
259
+ '0x%02x0000' % intensity
260
+ end
261
+ end
262
+
263
+ module BluePaint
264
+ private
265
+ def load_brush(intensity)
266
+ '0x0000%02x' % intensity
267
+ end
268
+ end
269
+
270
+ host.define_singleton_method(:load_brushes) { send_to_all :load_brush, 0xff }
271
+
272
+ host.load_brushes #=> { RedPaint=>'#ff0000', BluePaint=>'#0000ff' }
273
+ ```
data/lib/compound/host.rb CHANGED
@@ -45,6 +45,19 @@ module Compound
45
45
  raise(NameError, "undefined method `#{sym}' for object `#{self}'")
46
46
  end
47
47
 
48
+ private
49
+
50
+ # A private method to call send on each Compound::Part that defines the
51
+ # given method, whether publically or privately. The return values are
52
+ # collected into a hash with the compounded modules as the keys.
53
+ def send_to_parts sym, *args, &block
54
+ @_compound_parts.each_with_object({}) do |part, hash|
55
+ hash[part.instance_variable_get(:@_compound_component_module)] = \
56
+ part.send sym, *args, &block \
57
+ if part.respond_to? sym, true
58
+ end
59
+ end
60
+
48
61
  end
49
62
 
50
63
  end
data/lib/compound/part.rb CHANGED
@@ -8,6 +8,7 @@ module Compound
8
8
 
9
9
  def initialize parent, mod
10
10
  @_compound_component_parent = parent
11
+ @_compound_component_module = mod
11
12
  extend mod
12
13
  compounded(parent) if mod.instance_methods.include? :compounded
13
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compound
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-20 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -86,11 +86,11 @@ executables: []
86
86
  extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
+ - README.md
90
+ - lib/compound.rb
89
91
  - lib/compound/guard.rb
90
92
  - lib/compound/host.rb
91
93
  - lib/compound/part.rb
92
- - lib/compound.rb
93
- - README.md
94
94
  homepage: https://github.com/jemc/compound/
95
95
  licenses:
96
96
  - MIT License
@@ -111,8 +111,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 2.1.11
114
+ rubygems_version: 2.2.0
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: compound
118
118
  test_files: []
119
+ has_rdoc: