moosex 0.0.18 → 0.0.19

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.
@@ -6,7 +6,7 @@ class CoerceTest
6
6
  has attribute_ro: {
7
7
  is: :ro,
8
8
  isa: Integer,
9
- coerce: lambda {|value| value.to_i },
9
+ coerce: ->(value) { value.to_i },
10
10
  }
11
11
 
12
12
  has attribute_rw: {
@@ -18,8 +18,8 @@ class CoerceTest
18
18
  has attribute_lazy: {
19
19
  is: :lazy,
20
20
  isa: Integer,
21
- coerce: lambda {|value| value.to_i },
22
- builder: lambda{|object| "2048" },
21
+ coerce: ->(value) { value.to_i },
22
+ builder: ->(object) { "2048" },
23
23
  }
24
24
  end
25
25
 
@@ -45,37 +45,4 @@ describe "CoerceTest" do
45
45
  ct.attribute_lazy.should == 2048
46
46
  end
47
47
  end
48
- =begin
49
- require 'moosex/types'
50
48
 
51
- class CoerceTest2
52
- include MooseX
53
- include MooseX::Types
54
-
55
- has a: { is: :rw, coerce: :to_i } # if respond_to? then coerce
56
- has b: { is: :rw, coerce: { to_i: true } } # always coerce
57
- has c: { is: :rw, coerce: { to_i: false} } # if respond_to? then coerce
58
- has d: { is: :rw, coerce: [ :to_s, :to_string ] } # try to apply one of
59
- has e: { is: :rw, coerce: [ # if respond_to? then coerce via method/lambda
60
- { to_s: lambda{|x| x.to_s.to_sym } }, # can add more, will be evaluated in order
61
- { to_sym: :to_sym },
62
- ]
63
- }
64
- has f: { is: :rw, coerce: [ # should accept one array of
65
- { String => lambda{|x| x.to_sym } }, # type => coerce
66
- { Object => lambda{|x| x.to_s } },
67
- ]
68
- }
69
- has g: { is: :rw, coerce: { String => :to_sym } } # should accept one pair
70
- has h: { is: :rw, coerce: { # should accept one type validator
71
- isArray(String) => lambda{|obj| obj.join(",") }
72
- }
73
- }
74
- has i: {is: :rw, isa: isString(format: /\w+:\w+/) } # validator with autocoerce!
75
-
76
- end
77
-
78
- describe CoerceTest2 do
79
-
80
- end
81
- =end
@@ -17,14 +17,14 @@ class LazyFox
17
17
 
18
18
  has lazy_attr_who_accepts_lambda: {
19
19
  is: :lazy,
20
- builder: lambda{ |object| object.something }
20
+ builder: ->(this){ this.something }
21
21
  }
22
22
 
23
23
  has lazy_with_default: {
24
24
  is: :lazy,
25
25
  default: 10,
26
26
  clearer: true,
27
- builder: lambda {|o| 1 },
27
+ builder: ->(this) { 1 },
28
28
  }
29
29
 
30
30
  has last_lazy_attr: {
@@ -22,6 +22,12 @@ class TestMeta2
22
22
  }
23
23
  end
24
24
 
25
+ class TestMeta3 < TestMeta
26
+
27
+ has :lol, { doc: "lol"}
28
+ end
29
+
30
+
25
31
  describe TestMeta do
26
32
  it "should has 'meta'" do
27
33
  TestMeta.respond_to?(:meta).should be_true
@@ -87,4 +93,12 @@ describe TestMeta2 do
87
93
  docs[:bar].should == "etc"
88
94
  end
89
95
 
96
+ end
97
+
98
+ describe TestMeta3 do
99
+ it "should inherit meta" do
100
+ TestMeta3.meta.info[:foo].should == ""
101
+ TestMeta3.meta.info[:bar].should == "etc"
102
+ TestMeta3.meta.info[:lol].should == "lol"
103
+ end
90
104
  end
@@ -1,27 +1,27 @@
1
1
  require 'moosex/attribute/modifiers'
2
2
 
3
- describe MooseX::Attribute::Is do
3
+ describe MooseX::AttributeModifiers::Is do
4
4
  it "should accept only valid parameters" do
5
5
  expect {
6
- MooseX::Attribute::Is.new.process({is: :forbidden}, :foo)
6
+ MooseX::AttributeModifiers::Is.new.process({is: :forbidden}, :foo)
7
7
  }.to raise_error(MooseX::InvalidAttributeError,
8
8
  "invalid value for field 'foo' is 'forbidden', must be one of :private, :rw, :rwp, :ro or :lazy")
9
9
  end
10
10
  end
11
11
 
12
- describe MooseX::Attribute::Predicate do
12
+ describe MooseX::AttributeModifiers::Predicate do
13
13
  it "should accept only valid parameters" do
14
14
  expect {
15
- MooseX::Attribute::Predicate.new.process({predicate: 0}, :foo)
15
+ MooseX::AttributeModifiers::Predicate.new.process({predicate: 0}, :foo)
16
16
  }.to raise_error(MooseX::InvalidAttributeError,
17
17
  "cannot coerce field predicate to a symbol for foo: undefined method `to_sym' for 0:Fixnum")
18
18
  end
19
19
  end
20
20
 
21
- describe MooseX::Attribute::Handles do
21
+ describe MooseX::AttributeModifiers::Handles do
22
22
  it "should accept only valid parameters" do
23
23
  expect {
24
- MooseX::Attribute::Handles.new.process({handles: BasicObject}, :foo)
24
+ MooseX::AttributeModifiers::Handles.new.process({handles: BasicObject}, :foo)
25
25
  }.to raise_error(MooseX::InvalidAttributeError,
26
26
  "ops, should not use BasicObject for handles in foo")
27
27
  end
@@ -7,11 +7,11 @@ module Logabble
7
7
  has logger: {
8
8
  is: :ro,
9
9
  #isa: Logger,
10
- default: lambda{
10
+ default: -> do
11
11
  logger = Logger.new(STDOUT)
12
12
  logger.level = Logger::INFO
13
13
  logger
14
- },
14
+ end,
15
15
  handles: {
16
16
  log_info: :info,
17
17
  log_warn: :warn,
@@ -0,0 +1,74 @@
1
+ require 'moosex'
2
+ require 'moosex/attribute'
3
+ require 'moosex/attribute/modifiers'
4
+
5
+ module MooseX
6
+ module AttributeModifiers
7
+ module ThirdParty
8
+ class Bar
9
+ def process(options, attr_symbol)
10
+ !! options.delete(:bar)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module MyPlugin
18
+ def self.included(x)
19
+ x.meta.add_plugin(:bar)
20
+ end
21
+ end
22
+
23
+ module TestAddAttribute
24
+ class A
25
+ include MooseX.init(meta: true)
26
+ include MyPlugin
27
+
28
+ has :foo, {
29
+ bar: true
30
+ }
31
+ end
32
+
33
+ class B < A
34
+
35
+ has :foo2, {
36
+ bar: true
37
+ }
38
+ end
39
+
40
+ class C
41
+ include MooseX.init(meta: true)
42
+
43
+ has :foo, {
44
+ bar: true
45
+ }
46
+ end
47
+ end
48
+
49
+ describe TestAddAttribute do
50
+ it "A should support the new attribute" do
51
+ TestAddAttribute::A.new(foo: 1)
52
+ end
53
+
54
+ it "A should support the new attribute in meta" do
55
+ TestAddAttribute::A.meta.attrs[:foo].attribute_map[:bar].should be_true
56
+ end
57
+
58
+ it "B should support the new attribute" do
59
+ TestAddAttribute::B.new(foo: 1, foo2: 2)
60
+ end
61
+
62
+ it "B should support the new attribute in meta" do
63
+ TestAddAttribute::B.meta.attrs[:foo].attribute_map[:bar].should be_true
64
+ TestAddAttribute::B.meta.attrs[:foo2].attribute_map[:bar].should be_true
65
+ end
66
+
67
+ it "C should support the new attribute" do
68
+ TestAddAttribute::C.new(foo: 1)
69
+ end
70
+
71
+ it "C should support the new attribute in meta" do
72
+ TestAddAttribute::C.meta.attrs[:foo].attribute_map[:bar].should be_nil
73
+ end
74
+ end
@@ -13,7 +13,7 @@ class Point
13
13
  has y: {
14
14
  is: :rw,
15
15
  isa: Integer,
16
- default: lambda { 0 }, # you should specify a lambda
16
+ default: -> { 0 }, # you should specify a lambda
17
17
  }
18
18
 
19
19
  has secret: {
@@ -5,7 +5,7 @@ class ProxyToTarget
5
5
 
6
6
  has target: {
7
7
  is: :ro,
8
- default: lambda { Target.new }, # default, new instace of Target
8
+ default: -> { Target.new }, # default, new instace of Target
9
9
  handles: { # handles is for delegation,
10
10
  my_method_x: :method_x, # inject methods with new names
11
11
  my_method_y: :method_y, # old => obj.target.method_x , now => obj.my_method_x
@@ -13,19 +13,19 @@ class ProxyToTarget
13
13
  method_y: 1, # call obj.mymethod_z(2,3) is the equivalent to
14
14
  }, # call obj.target.method_z(1,2,3)
15
15
  my_method_y_with_lambda: { # currying!!!
16
- method_y: lambda{ 1 }, # call obj.mymethod_z(2,3) is the equivalent to
16
+ method_y: ->{ 1 }, # call obj.mymethod_z(2,3) is the equivalent to
17
17
  }, # call obj.target.method_z(1,2,3)
18
18
  my_method_z_with_array: {
19
- method_z: [1,lambda{ 2 } ,3]
19
+ method_z: [1,->{ 2 } ,3]
20
20
  },
21
21
  my_method_k_with_literal_array: {
22
22
  method_k: [[1,2,3]]
23
23
  },
24
24
  my_method_k_with_literal_array2: {
25
- method_k: [ lambda{ [1,2,3] } ]
25
+ method_k: [ ->{ [1,2,3] } ]
26
26
  },
27
27
  my_method_k_with_literal_array3: {
28
- method_k: lambda{ [[1,2,3]] }
28
+ method_k: ->{ [[1,2,3]] }
29
29
  }
30
30
  }
31
31
  }
@@ -111,7 +111,7 @@ class ProxyToTargetUsingArrayOfMethods
111
111
 
112
112
  has targetz: {
113
113
  is: :ro,
114
- default: lambda { Target.new },
114
+ default: -> { Target.new },
115
115
  handles: [
116
116
  :method_x, :method_y # will inject all methods with same name
117
117
  ]
@@ -139,7 +139,7 @@ class ProxyToTargetUsingSingleMethod
139
139
 
140
140
  has target: {
141
141
  is: :ro,
142
- default: lambda { Target.new },
142
+ default: -> { Target.new },
143
143
  handles: "method_x" # coerce to an array of symbols
144
144
  }
145
145
  end
@@ -158,7 +158,7 @@ class ProxyToTargetUsingModule
158
158
 
159
159
  has target: {
160
160
  is: :ro,
161
- default: lambda { Target.new },
161
+ default: -> { Target.new },
162
162
  handles: TargetModule # will import all methods from module
163
163
  }
164
164
  end
@@ -184,7 +184,7 @@ class ProxyToTargetUsingClass
184
184
 
185
185
  has target: {
186
186
  is: :ro,
187
- default: lambda { Target.new },
187
+ default: -> { Target.new },
188
188
  handles: Target # will use only public methods on Target class
189
189
  } # exclude methods from superclass
190
190
  end
@@ -0,0 +1,324 @@
1
+ require 'moosex'
2
+ require 'moosex/types'
3
+
4
+ module TestTrait
5
+ class MyHomePage
6
+ include MooseX
7
+ include MooseX::Types
8
+
9
+ has :counter, {
10
+ is: :ro,
11
+ isa: Integer,
12
+ default: 0,
13
+ traits: MooseX::Traits::Counter,
14
+ handles: {
15
+ inc_counter: :inc,
16
+ dec_counter: :dec,
17
+ reset_counter!: :reset,
18
+ }
19
+ }
20
+
21
+ has :counter_rw, {
22
+ is: :rw,
23
+ isa: Integer,
24
+ default: 0,
25
+ traits: [ MooseX::Traits::Counter ],
26
+ handles: {
27
+ inc_counter_rw: :inc,
28
+ dec_counter_rw: :dec,
29
+ reset_counter_rw!: :reset,
30
+ }
31
+ }
32
+
33
+ has :lazy_counter, {
34
+ is: :lazy,
35
+ isa: Integer,
36
+ traits: [ MooseX::Traits::Counter ],
37
+ handles: {
38
+ lazy_inc_counter: :inc,
39
+ lazy_dec_counter: :dec,
40
+ lazy_reset_counter!: :reset,
41
+ },
42
+ clearer: true,
43
+ }
44
+
45
+ has surname_name: {
46
+ is: :private,
47
+ isa: isTuple(String, String),
48
+ traits: [ MooseX::Traits::Pair ],
49
+ handles: {
50
+ surname: :first,
51
+ name: :second,
52
+ :surname= => :first=,
53
+ :name= => :second=,
54
+ surname_and_name: { join: ->{","} }
55
+ }
56
+ }
57
+
58
+ has bit: {
59
+ is: :ro,
60
+ default: true,
61
+ traits: MooseX::Traits::Bool,
62
+ handles: [ :toggle!, :not, :set!, :unset!, :value ],
63
+ }
64
+
65
+ has important: {
66
+ is: :rw,
67
+ default: nil,
68
+ traits: MooseX::Traits::RescueToNil,
69
+ handles: {
70
+ plus: :+,
71
+ minus: :-,
72
+ }
73
+ }
74
+
75
+ has important2: {
76
+ is: :rw,
77
+ default: nil,
78
+ traits: MooseX::Traits::RescueToZero,
79
+ handles: {
80
+ plus2: :+,
81
+ minus2: :-,
82
+ }
83
+ }
84
+
85
+ has phrase: {
86
+ is: :rw,
87
+ default: nil,
88
+ traits: MooseX::Traits::RescueToEmptyString,
89
+ handles: {
90
+ upcase_phrase: :upcase
91
+ }
92
+ }
93
+
94
+ def build_lazy_counter
95
+ 0
96
+ end
97
+ end
98
+
99
+ class ComplexExample
100
+ include MooseX
101
+ include MooseX::Types
102
+
103
+ has surname_name: {
104
+ is: :rw,
105
+ isa: isMaybe(isTuple(String, String)),
106
+ default: nil,
107
+ traits: [ MooseX::Traits::RescueToEmptyString, MooseX::Traits::Pair ],
108
+ handles: {
109
+ surname: :first,
110
+ name: :second,
111
+ :surname= => :first=,
112
+ :name= => :second=,
113
+ surname_and_name: { join: ->{", "} }
114
+ }
115
+ }
116
+ end
117
+ end
118
+
119
+ describe TestTrait::ComplexExample do
120
+ it "should be possible call surname_and_name" do
121
+ ce = TestTrait::ComplexExample.new(surname_name: ["Asimov", "Isaac"])
122
+ ce.name.should == "Isaac"
123
+ ce.surname_and_name.should == "Asimov, Isaac"
124
+ end
125
+
126
+ it "should be possible call surname_and_name if nil" do
127
+ ce = TestTrait::ComplexExample.new(surname_name: nil)
128
+ ce.name.should == ""
129
+ ce.surname_and_name.should == ", "
130
+ end
131
+
132
+ it "should be possible call surname_and_name if nil 2" do
133
+ ce = TestTrait::ComplexExample.new(surname_name: nil)
134
+ ce.name.should == ""
135
+ ce.surname_and_name.should == ", "
136
+
137
+ ce.name= "Isaac"
138
+ ce.surname_and_name.should == ", Isaac"
139
+ ce.surname= "Asimov"
140
+ ce.surname_and_name.should == "Asimov, Isaac"
141
+
142
+ ce.surname_name= nil
143
+ ce.name.should == ""
144
+ ce.surname_and_name.should == ", "
145
+ end
146
+ end
147
+
148
+ describe TestTrait::MyHomePage do
149
+ it "should increase counter" do
150
+ page = TestTrait::MyHomePage.new(counter: 0)
151
+ page.counter.should be_zero
152
+ page.inc_counter
153
+ page.counter.should == 1
154
+
155
+ page.inc_counter(3)
156
+ page.counter.should == 4
157
+
158
+ page.dec_counter
159
+ page.counter.should == 3
160
+
161
+ page.dec_counter(2)
162
+ page.counter.should == 1
163
+
164
+ page.reset_counter!
165
+ page.counter.should be_zero
166
+
167
+ page.inc_counter(5)
168
+ (page.counter * 8).should == 40
169
+ end
170
+
171
+ it "should increase counter by default value" do
172
+ page = TestTrait::MyHomePage.new
173
+ page.counter.should be_zero
174
+ page.inc_counter
175
+ page.counter.should == 1
176
+
177
+ page.inc_counter(3)
178
+ page.counter.should == 4
179
+
180
+ page.dec_counter
181
+ page.counter.should == 3
182
+
183
+ page.dec_counter(2)
184
+ page.counter.should == 1
185
+
186
+ page.reset_counter!
187
+ page.inc_counter
188
+ page.counter.should == 1
189
+ end
190
+
191
+ it "should increase counter_rw by default value" do
192
+ page = TestTrait::MyHomePage.new
193
+ page.counter_rw.should be_zero
194
+ page.inc_counter_rw
195
+ page.counter_rw.should == 1
196
+
197
+ page.counter_rw = 4
198
+ page.counter_rw.should == 4
199
+
200
+ page.dec_counter_rw
201
+ page.counter_rw.should == 3
202
+
203
+ page.dec_counter_rw(2)
204
+ page.counter_rw.should == 1
205
+
206
+ page.reset_counter_rw!
207
+ page.counter_rw.should be_zero
208
+ end
209
+
210
+ it "should increase lazy_counter if lazy" do
211
+ page = TestTrait::MyHomePage.new
212
+ page.lazy_counter.should be_zero
213
+ page.lazy_inc_counter
214
+ page.lazy_counter.should == 1
215
+
216
+ page.lazy_inc_counter(3)
217
+ page.lazy_counter.should == 4
218
+
219
+ page.lazy_dec_counter
220
+ page.lazy_counter.should == 3
221
+
222
+ page.lazy_dec_counter(2)
223
+ page.lazy_counter.should == 1
224
+
225
+ page.lazy_reset_counter!
226
+ page.lazy_counter.should be_zero
227
+
228
+ page.clear_lazy_counter!
229
+ page.lazy_counter.should be_zero
230
+ page.lazy_inc_counter
231
+ page.lazy_counter.should == 1
232
+ end
233
+
234
+ it "should store name, surname " do
235
+ page = TestTrait::MyHomePage.new(surname_name: ["Smith", "John"])
236
+
237
+ page.name.should == "John"
238
+ page.surname.should == "Smith"
239
+ page.surname_and_name.should == "Smith,John"
240
+
241
+ page.name= "Karl"
242
+ page.surname="Popper"
243
+
244
+ page.name.should == "Karl"
245
+ page.surname.should == "Popper"
246
+ page.surname_and_name.should == "Popper,Karl"
247
+
248
+ expect {
249
+ page.surname_name.count.should == 2
250
+ }.to raise_error(NoMethodError)
251
+ end
252
+
253
+ it "bit should act as a boolean" do
254
+ page = TestTrait::MyHomePage.new
255
+ page.bit.should == true
256
+ page.toggle!
257
+ page.bit.should == false
258
+ page.not.should == true
259
+
260
+ page.set!
261
+ page.bit.should == true
262
+
263
+ unless page.bit
264
+ raise "should act as a true value"
265
+ end
266
+ page.unset!
267
+ page.bit.should == false
268
+
269
+ if !! page.bit # necessary!!!
270
+ raise "should act as a false value"
271
+ end
272
+
273
+ if page.value
274
+ raise "should act as a false value"
275
+ end
276
+ end
277
+
278
+ it "important should be converted to integer" do
279
+ page = TestTrait::MyHomePage.new(important: 1)
280
+ page.important.should == 1
281
+ page.plus(1).should == 2
282
+ page.minus(4).should == -3
283
+ (page.important + 5).should == 6
284
+ end
285
+
286
+ it "important should be converted to integer returning nil" do
287
+ page = TestTrait::MyHomePage.new(important: nil)
288
+ page.important.should == nil
289
+ page.plus(1).should == nil
290
+ page.minus(4).should == nil
291
+ (page.important + 5).should == nil
292
+ end
293
+
294
+ it "important2 should be converted to integer" do
295
+ page = TestTrait::MyHomePage.new(important2: 1)
296
+ page.important2.should == 1
297
+ page.plus2(1).should == 2
298
+ page.minus2(4).should == -3
299
+ (page.important2 + 5).should == 6
300
+ end
301
+
302
+ it "important2 should be converted to integer returning 0" do
303
+ page = TestTrait::MyHomePage.new(important2: nil)
304
+ page.important2.should be_zero
305
+ page.plus2(1).should == 0
306
+ page.minus2(4).should == 0
307
+ (page.important2 + 5).should == 0
308
+ end
309
+
310
+ it "phrase should be converted to String" do
311
+ page = TestTrait::MyHomePage.new(phrase: "hello")
312
+ page.phrase.should == "hello"
313
+ page.upcase_phrase == "HELLO"
314
+ (page.upcase_phrase.concat ", WORLD").should == "HELLO, WORLD"
315
+ end
316
+
317
+ it "phrase should returning empty string" do
318
+ page = TestTrait::MyHomePage.new(phrase: nil)
319
+ page.phrase.should be_eql? ""
320
+ page.upcase_phrase == ""
321
+ (page.upcase_phrase.concat ", WORLD").should == ", WORLD"
322
+ end
323
+ end
324
+