moosex 0.0.16 → 0.0.17

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.
@@ -67,4 +67,4 @@ module MooseX
67
67
  end
68
68
  end
69
69
  end
70
- end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module MooseX
2
- VERSION = "0.0.16"
2
+ VERSION = "0.0.17"
3
3
  end
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.5"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
- spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "simplecov", ">= 0.8.2"
27
+ spec.add_development_dependency 'coveralls', '>= 0.7.0'
27
28
  end
@@ -0,0 +1,84 @@
1
+ #
2
+ # This example was ported from
3
+ # https://metacpan.org/pod/Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassing
4
+
5
+ require 'moosex'
6
+
7
+ class BankAccount
8
+ include MooseX
9
+
10
+ has balance: {
11
+ is: :rw,
12
+ isa: Integer,
13
+ default: 0,
14
+ }
15
+
16
+ def deposit(amount)
17
+ self.balance += amount
18
+ end
19
+
20
+ def withdraw(amount)
21
+ current_balance = self.balance
22
+
23
+ raise "Acount overdrawn" if amount > current_balance
24
+
25
+ self.balance= current_balance - amount
26
+
27
+ end
28
+ end
29
+
30
+ ba = BankAccount.new(balance: 100)
31
+
32
+ ba.deposit(50)
33
+
34
+ puts ba.balance # should be 150
35
+
36
+ ba.withdraw(70)
37
+
38
+ puts ba.balance # should be 80
39
+
40
+ begin
41
+ ba.withdraw(999)
42
+ rescue =>e
43
+ puts "can't withdraw 999: #{e}"
44
+ end
45
+
46
+ class CheckingAccount < BankAccount
47
+
48
+ has overdraft_account: {
49
+ is: :rw,
50
+ isa: BankAccount,
51
+ predicate: true,
52
+ handles: {
53
+ withdraw_from_overdraft_account: :withdraw
54
+ },
55
+ }
56
+
57
+ before(:withdraw) do |account, amount|
58
+ overdraft_amount = amount - account.balance
59
+
60
+ if account.has_overdraft_account? && overdraft_amount > 0
61
+
62
+ account.withdraw_from_overdraft_account( overdraft_amount )
63
+
64
+ account.deposit(overdraft_amount )
65
+
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ ba = BankAccount.new(balance: 1000)
72
+ ca1 = CheckingAccount.new(balance: 1000, overdraft_account: ba)
73
+ ca2 = CheckingAccount.new(balance: 1000)
74
+
75
+ ca1.withdraw(1500)
76
+
77
+ puts ca1.balance # should print 0
78
+ puts ba.balance # should print 500
79
+
80
+ begin
81
+ ca2.withdraw(1500)
82
+ rescue => e
83
+ puts "can't withdraw 1500: #{e}"
84
+ end
@@ -0,0 +1,82 @@
1
+ #
2
+ # This example was ported from
3
+ # https://metacpan.org/pod/Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild
4
+ #
5
+ # What is a Binary Tree?
6
+ # http://en.wikipedia.org/wiki/Binary_treehttp://en.wikipedia.org/wiki/Binary_tree
7
+
8
+ require 'moosex'
9
+
10
+ class BinaryTree
11
+ include MooseX
12
+
13
+ has node: {
14
+ is: :rw,
15
+ predicate: :full?, # full? indicate there is a value
16
+ }
17
+
18
+ has parent: {
19
+ is: :rw,
20
+ isa: BinaryTree,
21
+ predicate: true, # will inject has_parent?
22
+ }
23
+
24
+ has [ :left, :right ], {
25
+ is: :rw,
26
+ lazy: true,
27
+ isa: BinaryTree,
28
+ predicate: true, # will inject has_left? has_right?
29
+ builder: :build_child_tree,
30
+ }
31
+
32
+ def build_child_tree
33
+ BinaryTree.new( parent: self )
34
+ end
35
+
36
+ before(:left=, :right=) do |this, tree|
37
+ tree.parent= this
38
+ end
39
+
40
+ def <<(value)
41
+ if ! full?
42
+ self.node= value # <= here is ambiguous, without self., if it is
43
+ elsif (value > node ) # method or an assignment to a local variable!
44
+ left << value
45
+ else
46
+ right << value
47
+ end
48
+ end
49
+
50
+ def to_s
51
+ return "?" unless full?
52
+ "{#{left} <= #{node} => #{right}}"
53
+ end
54
+ end
55
+
56
+ bt = BinaryTree.new()
57
+
58
+ bt << 4 # 1st node
59
+ bt << 8 # 2nd node, to the left of 4
60
+ bt << 1 # 3rd node, to the right if 4
61
+ bt.left.right = BinaryTree.new(node: 7)
62
+
63
+ puts "bt.node=#{bt.node}, bt.left.parent.node=#{bt.left.parent.node}"
64
+ puts "bt.left.right.parent.node=#{bt.left.right.parent.node}"
65
+ puts ""
66
+ puts ""
67
+ puts "#{bt}"
68
+
69
+ puts <<EOF
70
+
71
+ # this example will print
72
+ # {{? <= 8 => {? <= 7 => ?}} <= 4 => {? <= 1 => ?}}
73
+ #
74
+ # this is the equivalent to this tree
75
+ #
76
+ # 4
77
+ # 8 1
78
+ # ? 7 ? ?
79
+ #
80
+ # have fun :)
81
+ EOF
82
+
@@ -1,3 +1,7 @@
1
+ #
2
+ # This example was ported from
3
+ # http://search.cpan.org/~winter/MooseX-Event-v0.2.0/lib/MooseX/Event.pm
4
+
1
5
  require 'moosex'
2
6
  require 'moosex/event'
3
7
 
@@ -37,4 +41,4 @@ ep.on_pong do |obj, message|
37
41
  end
38
42
 
39
43
  ep.ping # will print "receive ping!"
40
- ep.pong 1 # will print "receive pong!"
44
+ ep.pong 1 # will print "receive pong!"
@@ -0,0 +1,48 @@
1
+ #
2
+ # This example was ported from
3
+ # https://metacpan.org/pod/Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercion
4
+
5
+ require 'moosex'
6
+ require 'moosex/types'
7
+
8
+ class Human
9
+ include MooseX
10
+ include MooseX::Types
11
+
12
+ has sex: {
13
+ is: :rw,
14
+ isa: isEnum(:male, :female),
15
+ required: true,
16
+ }
17
+
18
+ has [:mother, :father], {
19
+ is: :rw,
20
+ isa: Human,
21
+ }
22
+
23
+ def +(partner)
24
+ raise "ops" if self.sex.eql?(partner.sex)
25
+
26
+ mother, father = (self.sex === :female)? [self, partner] : [partner, self]
27
+
28
+ sex = [:male, :female].shuffle.shift
29
+
30
+ Human.new(sex: sex, mother: mother, father: father)
31
+ end
32
+ end
33
+
34
+ adam = Human.new( sex: :male )
35
+ eve = Human.new( sex: :female )
36
+ lilith = Human.new( sex: :female )
37
+
38
+ childs = []
39
+ childs << adam + eve
40
+ childs << eve + adam
41
+
42
+ puts childs.inspect
43
+
44
+ begin
45
+ eve + lilith
46
+ rescue => e
47
+ p e
48
+ end
@@ -1,3 +1,7 @@
1
+ #
2
+ # This example was ported from
3
+ # https://metacpan.org/pod/Moose::Cookbook::Basics::Point_AttributesAndSubclassing
4
+
1
5
  require 'moosex'
2
6
 
3
7
  class Point
@@ -56,4 +60,4 @@ p1.clear!
56
60
  p3.clear!
57
61
 
58
62
  puts ">> clear"
59
- puts p1, p3
63
+ puts p1, p3
@@ -1,8 +1,11 @@
1
- require 'moosex'
1
+ #
2
+ # This example was ported from
3
+ # https://metacpan.org/pod/Moose::Cookbook::Roles::Comparable_CodeReuse
2
4
 
5
+ require 'moosex'
3
6
 
4
7
  module Eq
5
- include MooseX.disable_warnings()
8
+ include MooseX.init( warnings: false)
6
9
 
7
10
  requires :equal
8
11
 
@@ -20,7 +23,7 @@ end
20
23
  class Currency
21
24
  include Valuable
22
25
  include Eq # will warn unless disable_warnings was called.
23
- # to avoid warnings, you should include after
26
+ # to avoid warnings, you should include after
24
27
  # define all required modules,
25
28
 
26
29
  def equal(other)
@@ -54,10 +57,14 @@ c1 = Currency.new( value: 12 )
54
57
  c2 = Currency.new( value: 12 )
55
58
  c3 = Currency.new( value: 24 )
56
59
 
57
- c1.equal(c2) # true
58
- c1.equal(c3) # false
60
+ p c1.equal(c2) # true
61
+ p c1.equal(c3) # false
59
62
 
60
- Comparator.new(compare_to: c1).no_equal(c2) # false
61
- Comparator.new(compare_to: c1).no_equal(c3) # true
63
+ p Comparator.new(compare_to: c1).no_equal(c2) # false
64
+ p Comparator.new(compare_to: c1).no_equal(c3) # true
62
65
 
63
- WrongClass.new(one: 1, two: 2) # will raise exception
66
+ begin
67
+ WrongClass.new(one: 1, two: 2) # will raise exception
68
+ rescue => e
69
+ puts "will raise exception #{e}"
70
+ end
@@ -171,4 +171,4 @@ describe RoleTest::AlienX do
171
171
  a.race.should == :borg
172
172
  a.x.should == -1
173
173
  end
174
- end
174
+ end
@@ -83,4 +83,4 @@ describe "Baz" do
83
83
  baz.has_boom?.should be_false
84
84
  baz.boom.should be_nil
85
85
  end
86
- end
86
+ end
@@ -20,4 +20,4 @@ describe "BuildExample" do
20
20
  BuildExample.new(x: 0, y: 0)
21
21
  }.to raise_error(/invalid: you should use x != y/)
22
22
  end
23
- end
23
+ end
@@ -45,3 +45,37 @@ describe "CoerceTest" do
45
45
  ct.attribute_lazy.should == 2048
46
46
  end
47
47
  end
48
+ =begin
49
+ require 'moosex/types'
50
+
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
@@ -30,4 +30,4 @@ describe "SuperTypes" do
30
30
  }.to raise_error(MooseX::Types::TypeCheckError,
31
31
  "isa check for field x: AnyOf Check violation: caused by [Constant violation: value '[1]' (Array) is not '1' (Fixnum), Maybe violation: caused by AnyOf Check violation: caused by [Type violation: value '[1]' (Array) is not an instance of [Type Integer], Constant violation: value '[1]' (Array) is not '' (NilClass)], Array violation: caused by hasMethods violation: object 1 (Fixnum) should implement method baz, Enum Check violation: value '[1]' (Array) is not [:foo, :bar]]")
32
32
  end
33
- end
33
+ end
@@ -123,4 +123,4 @@ describe "LazyFox" do
123
123
  end
124
124
  l.last_lazy_attr.should be_zero
125
125
  end
126
- end
126
+ end
@@ -39,4 +39,4 @@ describe "Lol" do
39
39
  lol.desintegrate_c
40
40
  lol.can_haz_c?.should be_false
41
41
  end
42
- end
42
+ end
@@ -3,6 +3,15 @@ require 'moosex'
3
3
 
4
4
  class A
5
5
  include MooseX
6
+
7
+ has :foo
8
+
9
+ has :bar, { doc: "bar..."}
10
+ end
11
+
12
+ class B < A
13
+
14
+ has :bar, { doc: "new Bar... ", override: true }
6
15
  end
7
16
 
8
17
  describe "MooseX" do
@@ -14,4 +23,18 @@ describe "MooseX" do
14
23
  a = A.new
15
24
  a.is_a?(A).should be_true
16
25
  end
17
- end
26
+
27
+ it "A should has an attribute foo" do
28
+ a = A.new(foo: 1)
29
+ a.foo.should == 1
30
+ a.foo = 6
31
+ a.foo.should == 6
32
+ end
33
+
34
+ it "B should has an attribute foo" do
35
+ a = B.new(foo: 1)
36
+ a.foo.should == 1
37
+ a.foo = 6
38
+ a.foo.should == 6
39
+ end
40
+ end
@@ -114,6 +114,7 @@ class Point3D < Point
114
114
  is: :rw, # Redefine attribute
115
115
  isa: String, # should be String
116
116
  default: "5", # default value is "5" (constant)
117
+ override: true, # mandatory, or it will raise exception
117
118
  }
118
119
 
119
120
  has z: {
@@ -203,4 +203,4 @@ describe "ProxyToTargetUsingClass" do
203
203
  p.target.method_y(1,2,3).should == 6
204
204
  p.method_y(1,2,3).should == 6
205
205
  end
206
- end
206
+ end
@@ -211,4 +211,4 @@ describe "AfterBefore::Undertest2" do
211
211
  u = AfterBefore::Undertest2.new(logger: logger)
212
212
  u.say(1).should == 6 # 1 -> 1+1 -> 2*2 -> 4+2
213
213
  end
214
- end
214
+ end