moosex 0.0.8 → 0.0.9

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: 762256a8cbd9378b2e80924fda5e278861f38f7e
4
- data.tar.gz: fdbd36add8e84286e0094a907f5b24830d1d0e80
3
+ metadata.gz: bf755e4f809015aa27587ea4009cb8e15d082154
4
+ data.tar.gz: ec5ee2722ed7fe31bc88050722a803cc366f5958
5
5
  SHA512:
6
- metadata.gz: 2215ab98afd2f0b15faa905e8de8dfc2c436814efb16bcd62c6f29474ccb7b371b96a463d45845d3e868adfb7a17df1ff2a06c7d3da1ec3e82138670fe7a8df8
7
- data.tar.gz: 7474bb350e05b1734aec6257ec83b2a279e34839592ca48a907c6d4730c74f1fc0fe66fb4cd43cdc1d5cea5635525c08ca00013d6367d066ec7929cf7942052b
6
+ metadata.gz: 0b5c7d36b5ef46c18cdb83893ce74da0b500c0d11706c08e3f15438c0f3e486d937f73bc2a860993fe1b365559b0c651c84060875f443a04cd1d6c49f07ef9bc
7
+ data.tar.gz: 4cf80c9f428fe865904ad8c8e79d37460ac1ca4a8cbaf5305ab26a962fefade283ef10ecabf262a58a30e1e852c9c026e933139c09248d0017dc83c31e07baed
data/Changelog CHANGED
@@ -1,3 +1,8 @@
1
+ 0.0.9 - 2014-02-02
2
+ - support to BUILD and BUILDARGS #5 and #6
3
+ - support to trigger #7
4
+ - support to coerce #8
5
+
1
6
  0.0.8 - 2014-02-01
2
7
  - should support init_arg option in attr #14
3
8
  - support lazy attributes / builder #4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- moosex (0.0.8)
4
+ moosex (0.0.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -18,9 +18,18 @@ module MooseX
18
18
  end
19
19
 
20
20
  def initialize(args={})
21
+ args = BUILDARGS(args)
21
22
 
22
23
  self.class.__meta().init(self, args)
23
24
 
25
+ BUILD()
26
+ end
27
+
28
+ def BUILDARGS(args)
29
+ args
30
+ end
31
+
32
+ def BUILD
24
33
  end
25
34
 
26
35
  def c.inherited(subclass)
@@ -94,8 +103,10 @@ module MooseX
94
103
  clearer: false,
95
104
  required: false,
96
105
  predicate: false,
97
- isa: lambda { |x| true },
106
+ isa: lambda { |value| true },
98
107
  handles: {},
108
+ trigger: lambda {|object,value|}, # TODO: implement
109
+ coerce: lambda {|object| object}, # TODO: implement
99
110
  }
100
111
 
101
112
  REQUIRED = [ :is ]
@@ -218,11 +229,30 @@ module MooseX
218
229
  init_arg: lambda do |init_arg, field_name|
219
230
  init_arg.to_sym
220
231
  end,
232
+ trigger: lambda do |trigger, field_name|
233
+ unless trigger.is_a? Proc
234
+ trigger_method_name = trigger.to_sym
235
+ trigger = lambda do |object, value|
236
+ object.send(trigger_method_name,value)
237
+ end
238
+ end
239
+
240
+ trigger
241
+ end,
242
+ coerce: lambda do |coerce, field_name|
243
+ unless coerce.is_a? Proc
244
+ coerce_method_name = coerce.to_sym
245
+ coerce = lambda do |object|
246
+ object.send(coerce_method_name)
247
+ end
248
+ end
249
+
250
+ coerce
251
+ end,
221
252
  };
222
253
 
223
254
  def initialize(a, o)
224
255
  # todo extract this to a framework, see issue #21 on facebook
225
-
226
256
  o = DEFAULTS.merge({
227
257
  reader: a,
228
258
  writter: a.to_s.concat("=").to_sym,
@@ -267,6 +297,8 @@ module MooseX
267
297
  @writter = o[:writter]
268
298
  @builder = o[:builder]
269
299
  @init_arg = o[:init_arg]
300
+ @trigger = o[:trigger]
301
+ @coerce = o[:coerce]
270
302
  end
271
303
 
272
304
  def init(object, args)
@@ -295,30 +327,28 @@ module MooseX
295
327
  end
296
328
  end
297
329
 
330
+ value_from_default = false
298
331
  if args.has_key? @init_arg
299
332
  value = args[ @init_arg ]
300
333
  elsif @default
301
334
  value = @default.call
335
+ value_from_default = true
302
336
  elsif @required
303
337
  raise "attr \"#{@attr_symbol}\" is required"
304
338
  else
305
339
  return
306
340
  end
307
341
 
308
- if @is.eql?(:ro) || @is.eql?(:lazy)
309
-
310
- # TODO: remove redundancy
311
342
 
312
- type_check = @isa
313
- type_check.call(value)
343
+ value = @coerce.call(value)
314
344
 
315
- object.instance_variable_set inst_variable_name, value
345
+ @isa.call( value )
346
+ unless value_from_default
347
+ @trigger.call(object, value)
348
+ end
349
+
350
+ object.instance_variable_set inst_variable_name, value
316
351
 
317
- else
318
-
319
- object.send( @writter, value )
320
-
321
- end
322
352
  end
323
353
 
324
354
  def generate_getter
@@ -329,13 +359,15 @@ module MooseX
329
359
 
330
360
  if @lazy
331
361
  type_check = @isa
332
-
362
+ coerce = @coerce
363
+ trigger = @trigger
333
364
  before_get = lambda do |object|
334
365
  return if object.instance_variable_defined? inst_variable_name
335
366
 
336
367
  value = builder.call(object)
368
+ value = coerce.call(value)
337
369
  type_check.call(value)
338
-
370
+ trigger.call(object, value)
339
371
  object.instance_variable_set(inst_variable_name, value)
340
372
  end
341
373
  end
@@ -348,9 +380,13 @@ module MooseX
348
380
 
349
381
  def generate_setter
350
382
  inst_variable_name = "@#{@attr_symbol}".to_sym
383
+ coerce = @coerce
351
384
  type_check = @isa
385
+ trigger = @trigger
352
386
  Proc.new do |value|
387
+ value = coerce.call(value)
353
388
  type_check.call(value)
389
+ trigger.call(self,value)
354
390
  instance_variable_set inst_variable_name, value
355
391
  end
356
392
  end
@@ -1,3 +1,3 @@
1
1
  module Moosex
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -575,4 +575,181 @@ describe "LazyFox" do
575
575
  end
576
576
  l.last_lazy_attr.should be_zero
577
577
  end
578
+ end
579
+
580
+ class CoerceTest
581
+ include MooseX
582
+
583
+ has attribute_ro: {
584
+ is: :ro,
585
+ isa: Integer,
586
+ coerce: lambda {|value| value.to_i },
587
+ }
588
+
589
+ has attribute_rw: {
590
+ is: :rw,
591
+ isa: Integer,
592
+ coerce: lambda {|value| value.to_i },
593
+ }
594
+
595
+ has attribute_lazy: {
596
+ is: :lazy,
597
+ isa: Integer,
598
+ coerce: lambda {|value| value.to_i },
599
+ builder: lambda{|object| "2048" },
600
+ }
601
+
602
+ def trigger_attr(new_value)
603
+ puts "change value of attribute to #{new_value}"
604
+ end
605
+ end
606
+
607
+ describe "CoerceTest" do
608
+ it "should coerce the argument using to_i on constructor" do
609
+ ct = CoerceTest.new(attribute_ro: "12")
610
+ ct.attribute_ro.should == 12
611
+ end
612
+
613
+ it "should coerce the argument using to_i on constructor" do
614
+ ct = CoerceTest.new(attribute_rw: "12")
615
+ ct.attribute_rw.should == 12
616
+ end
617
+
618
+ it "should coerce in the setter" do
619
+ ct = CoerceTest.new
620
+ ct.attribute_rw= "128"
621
+ ct.attribute_rw.should == 128
622
+ end
623
+
624
+ it "should coerce from builder" do
625
+ ct = CoerceTest.new
626
+ ct.attribute_lazy.should == 2048
627
+ end
628
+ end
629
+
630
+ class TriggerTest
631
+ include MooseX
632
+
633
+ has logger: {
634
+ is: :ro
635
+ }
636
+
637
+ has attr_with_trigger: {
638
+ is: :rw,
639
+ trigger: :my_method,
640
+ }
641
+
642
+ has attr_with_trigger_ro: {
643
+ is: :ro,
644
+ trigger: :my_method,
645
+ }
646
+
647
+ has attr_with_default: {
648
+ is: :rw,
649
+ trigger: lambda do |object, new_value|
650
+ object.logger.log "will update attr_with_trigger with new value #{new_value}"
651
+ end,
652
+ default: 1,
653
+ }
654
+
655
+ has attr_lazy_trigger: {
656
+ is: :lazy,
657
+ trigger: :my_method,
658
+ builder: lambda{ |x| 1},
659
+ }
660
+
661
+ def my_method(new_value)
662
+ logger.log "will update attr_with_trigger with new value #{new_value}"
663
+ end
664
+ end
665
+
666
+ describe "TriggerTest" do
667
+ it "should call trigger on constructor" do
668
+ log = double
669
+ log.should_receive(:log)
670
+ t = TriggerTest.new(attr_with_trigger: 1, logger: log)
671
+
672
+ end
673
+
674
+ it "should call trigger on constructor (ro)" do
675
+ log = double
676
+ log.should_receive(:log)
677
+ t = TriggerTest.new(attr_with_trigger_ro: 1, logger: log)
678
+
679
+ end
680
+
681
+ it "should NOT call trigger on constructor (with default)" do
682
+ log = double
683
+ log.should_not_receive(:log)
684
+ t = TriggerTest.new(logger: log)
685
+ end
686
+
687
+ it "should NOT call trigger on constructor (with default)" do
688
+ log = double
689
+ log.should_receive(:log)
690
+ t = TriggerTest.new(logger: log)
691
+
692
+ t.attr_with_default = 1
693
+ end
694
+
695
+ it "should call trigger on setter" do
696
+ log = double
697
+ log.should_receive(:log)
698
+ t = TriggerTest.new(logger: log)
699
+
700
+ t.attr_with_trigger = 1
701
+ end
702
+
703
+ it "should call trigger on setter" do
704
+ log = double
705
+ log.should_receive(:log)
706
+ t = TriggerTest.new(logger: log)
707
+
708
+ t.attr_lazy_trigger.should == 1
709
+ end
710
+ end
711
+
712
+ class BuildArgsExample
713
+ include MooseX
714
+
715
+ has [:x, :y], {
716
+ is: :rw,
717
+ required: true,
718
+ }
719
+
720
+ def BUILDARGS(args)
721
+ args[:x] = 1024
722
+ args[:y] = - args[:y]
723
+ args
724
+ end
725
+ end
726
+
727
+ describe "BuildArgsExample" do
728
+ it "should create the object" do
729
+ ex = BuildArgsExample.new(x: 10, y: -2)
730
+ ex.x.should == 1024
731
+ ex.y.should == 2
732
+ end
733
+ end
734
+
735
+ class BuildExample
736
+ include MooseX
737
+
738
+ has [:x, :y], {
739
+ is: :rw,
740
+ required: true,
741
+ }
742
+ def BUILD
743
+ if self.x == self.y
744
+ raise "invalid: you should use x != y"
745
+ end
746
+ end
747
+ end
748
+
749
+ describe "BuildExample" do
750
+ it "should raise exception on build" do
751
+ expect {
752
+ BuildExample.new(x: 0, y: 0)
753
+ }.to raise_error(/invalid: you should use x != y/)
754
+ end
578
755
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moosex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Peczenyj