moosex 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog +4 -1
- data/Gemfile.lock +1 -1
- data/README.md +20 -1
- data/lib/moosex.rb +16 -0
- data/lib/moosex/version.rb +1 -1
- data/spec/moosex_spec.rb +40 -0
- 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: 4148ee310961686e2246bd4ce922984b2d5890f4
|
4
|
+
data.tar.gz: 5cd59e49ecd649fa8eec2e14949aa91b7ac53e78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fccc982b45dbf7bc095acb933df761ddacdddd202fe4afbda18e361e55d576209772aa734a01906e8acf37a83dff2ee8ba71a4f7528bf955c0986cce54f23839
|
7
|
+
data.tar.gz: ef15d0179cf5f72b49b7f0978162f8780f2dc5b3c4a6a346e0d4dcfbe56bbe3f32d9a5aa06148c77db1414ffcef397bb4a2f2b1d30301482608afb7c0b1bc893
|
data/Changelog
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -40,7 +40,7 @@ A postmodern object system for Ruby [![Build Status](https://travis-ci.org/pecze
|
|
40
40
|
include MooseX
|
41
41
|
|
42
42
|
has :bam, {
|
43
|
-
:is => :ro,
|
43
|
+
:is => :ro, # read-only, you should specify in new only
|
44
44
|
:isa => lambda {|x| # you should add your own validator
|
45
45
|
raise 'x should be less than 100' if x > 100
|
46
46
|
},
|
@@ -67,12 +67,31 @@ A postmodern object system for Ruby [![Build Status](https://travis-ci.org/pecze
|
|
67
67
|
}
|
68
68
|
end
|
69
69
|
|
70
|
+
class Target
|
71
|
+
def method_x
|
72
|
+
1024
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Proxy
|
77
|
+
include MooseX
|
78
|
+
|
79
|
+
has :target, {
|
80
|
+
:is => :ro,
|
81
|
+
:default => lambda { Target.new() },
|
82
|
+
:handles => {
|
83
|
+
:my_method_x => :method_x,# create my_method_x in Proxy
|
84
|
+
}, # this will delegate to @target.method_x
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
70
88
|
# now you have a generic constructor
|
71
89
|
p1 = Point.new # x and y will be 0
|
72
90
|
p2 = Point.new( :x => 5 ) # y will be 0
|
73
91
|
p3 = Point.new( :x => 5, :y => 4)
|
74
92
|
foo = Foo.new( :bar => 123 ) # without bar will raise exception
|
75
93
|
baz = Baz.new( :bam => 99 ) # if bam > 100 will raise exception
|
94
|
+
Proxy.new.my_method_x # will call method_x in target, return 1024
|
76
95
|
```
|
77
96
|
|
78
97
|
## Installation
|
data/lib/moosex.rb
CHANGED
@@ -70,6 +70,7 @@ module MooseX
|
|
70
70
|
:required => false,
|
71
71
|
:predicate => false,
|
72
72
|
:isa => lambda { |x| true },
|
73
|
+
:handles => {},
|
73
74
|
}
|
74
75
|
|
75
76
|
REQUIRED = [ :is ]
|
@@ -80,6 +81,7 @@ module MooseX
|
|
80
81
|
raise "invalid value for field '#{field_name}' is '#{is}', must be one of :rw, :rwp, :ro or :lazy"
|
81
82
|
end
|
82
83
|
end,
|
84
|
+
:handles => lambda {|handles, field_name| true }, # TODO: add implementation
|
83
85
|
};
|
84
86
|
|
85
87
|
COERCE = {
|
@@ -130,6 +132,12 @@ module MooseX
|
|
130
132
|
# create a nested exception here
|
131
133
|
raise "cannot coerce field clearer to a symbol for #{field_name}: #{e}"
|
132
134
|
end
|
135
|
+
end,
|
136
|
+
:handles => lambda do |handles, field_name|
|
137
|
+
# TODO:
|
138
|
+
# if single method
|
139
|
+
# if array of methods
|
140
|
+
handles
|
133
141
|
end,
|
134
142
|
};
|
135
143
|
|
@@ -160,6 +168,7 @@ module MooseX
|
|
160
168
|
@required = o[:required]
|
161
169
|
@predicate = o[:predicate]
|
162
170
|
@clearer = o[:clearer]
|
171
|
+
@handles = o[:handles]
|
163
172
|
end
|
164
173
|
|
165
174
|
def init(object, args)
|
@@ -168,6 +177,13 @@ module MooseX
|
|
168
177
|
setter = @attr_symbol.to_s.concat("=").to_sym
|
169
178
|
value = nil
|
170
179
|
|
180
|
+
attr_symbol = @attr_symbol
|
181
|
+
@handles.each_pair do | method, target_method |
|
182
|
+
object.define_singleton_method method do |*args|
|
183
|
+
self.send(attr_symbol).send(target_method, *args)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
171
187
|
if @predicate
|
172
188
|
object.define_singleton_method @predicate do
|
173
189
|
instance_variable_defined? inst_variable_name
|
data/lib/moosex/version.rb
CHANGED
data/spec/moosex_spec.rb
CHANGED
@@ -68,6 +68,46 @@ class Lol
|
|
68
68
|
}
|
69
69
|
end
|
70
70
|
|
71
|
+
class Target
|
72
|
+
def method_x
|
73
|
+
1024
|
74
|
+
end
|
75
|
+
|
76
|
+
def method_y(a,b,c)
|
77
|
+
a + b + c
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class Proxy
|
82
|
+
include MooseX
|
83
|
+
|
84
|
+
has :target, {
|
85
|
+
:is => :ro,
|
86
|
+
:isa => Target,
|
87
|
+
:default => lambda { Target.new() },
|
88
|
+
:handles => {
|
89
|
+
:my_method_x => :method_x,
|
90
|
+
:my_method_y => :method_y,
|
91
|
+
},
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "Proxy" do
|
96
|
+
it "should delegate method_x to the target" do
|
97
|
+
p = Proxy.new
|
98
|
+
|
99
|
+
p.target.method_x.should == 1024
|
100
|
+
p.my_method_x.should == 1024
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should delegate method_y to the target" do
|
104
|
+
p = Proxy.new
|
105
|
+
|
106
|
+
p.target.method_y(1,2,3).should == 6
|
107
|
+
p.my_method_y(1,2,3).should == 6
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
71
111
|
describe "Point" do
|
72
112
|
describe "should has an intelligent constructor" do
|
73
113
|
it "without arguments, should initialize with default values" do
|