womb 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +5 -7
- data/lib/womb.rb +9 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d973d70e4c4c77d477490cea8c99b6974405f513
|
4
|
+
data.tar.gz: 14bcb775ea8c721f1c2b8a4fde9b218d356b37a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9d2a4c0fc3e2cf8b8beea28fe96f13766abcc9604e336b95af0f80367963a2875d6c0b1833331d55f7ec6d058b0c5996e47663624986469925ae84096e73c54
|
7
|
+
data.tar.gz: 93974489fe099aeb6c4727dbfc33756e895d28d41d5fd8ac9976337d306131ed882b079874ac9d80085b876be9807890bcbddfce54200727d0cea8e610c81481
|
data/README.md
CHANGED
@@ -6,11 +6,9 @@ reduce boiler plate.
|
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
|
-
Once wrapped around an object, `Womb`
|
10
|
-
|
11
|
-
|
12
|
-
used. After the object has been defined, the `birth` method returns the
|
13
|
-
object.
|
9
|
+
Once wrapped around an object, `Womb` stores all messages via `method_missing`
|
10
|
+
and then sends them to the object when the `birth` method is called, returning
|
11
|
+
the defined object.
|
14
12
|
|
15
13
|
``` ruby
|
16
14
|
require 'womb'
|
@@ -33,7 +31,7 @@ an `initialize` method that simply sets instance variables from it's arguments.
|
|
33
31
|
|
34
32
|
``` ruby
|
35
33
|
RegPolygon = Womb[Module.new]
|
36
|
-
.assign(:
|
34
|
+
.assign(:interior_angle) { |sides| (sides - 2) * Math::PI / sides }
|
37
35
|
.birth
|
38
36
|
|
39
37
|
Triangle = Womb[Module.new]
|
@@ -42,7 +40,7 @@ Triangle = Womb[Module.new]
|
|
42
40
|
|
43
41
|
RegTriangle = Womb[Class.new]
|
44
42
|
.init(:base)
|
45
|
-
.def(:height) { @base * Math.sin(RegPolygon.
|
43
|
+
.def(:height) { @base * Math.sin(RegPolygon.interior_angle(3)) }
|
46
44
|
.def(:area) { Triangle.area(@base, height) }
|
47
45
|
.birth
|
48
46
|
|
data/lib/womb.rb
CHANGED
@@ -5,10 +5,13 @@ class Womb < BasicObject
|
|
5
5
|
|
6
6
|
def initialize(obj)
|
7
7
|
@obj = obj
|
8
|
+
@messages = []
|
8
9
|
end
|
9
10
|
|
10
|
-
def method_missing(
|
11
|
-
|
11
|
+
def method_missing(method, *args, &b)
|
12
|
+
method = aliases[method] || method
|
13
|
+
return super unless obj.respond_to?(method, true)
|
14
|
+
@messages << [obj, [method, *args], b]
|
12
15
|
self
|
13
16
|
end
|
14
17
|
|
@@ -20,12 +23,14 @@ class Womb < BasicObject
|
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
23
|
-
def send_to_singleton(*args)
|
24
|
-
obj.singleton_class
|
26
|
+
def send_to_singleton(*args, &b)
|
27
|
+
@messages << [obj.singleton_class, args, b]
|
25
28
|
self
|
26
29
|
end
|
27
30
|
|
28
31
|
def birth
|
32
|
+
messages = @messages
|
33
|
+
@obj.class_eval { messages.each { |(target, args, b)| target.send(*args, &b) } }
|
29
34
|
@obj
|
30
35
|
end
|
31
36
|
|
@@ -37,5 +42,4 @@ class Womb < BasicObject
|
|
37
42
|
@aliases ||= { assign: :define_singleton_method,
|
38
43
|
def: :define_method }
|
39
44
|
end
|
40
|
-
|
41
45
|
end
|