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.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -1
- data/.gitignore +1 -1
- data/.travis.yml +2 -0
- data/Changelog +7 -1
- data/Gemfile.lock +16 -2
- data/README.md +105 -30
- data/lib/moosex.rb +639 -613
- data/lib/moosex/event.rb +1 -1
- data/lib/moosex/version.rb +1 -1
- data/moosex.gemspec +2 -1
- data/samples/bank_account.rb +84 -0
- data/samples/binary_tree.rb +82 -0
- data/samples/events.rb +5 -1
- data/samples/human.rb +48 -0
- data/samples/point.rb +5 -1
- data/samples/roles.rb +15 -8
- data/spec/baserole_spec.rb +1 -1
- data/spec/baz_spec.rb +1 -1
- data/spec/build_spec.rb +1 -1
- data/spec/coerce_spec.rb +34 -0
- data/spec/complex_type_spec.rb +1 -1
- data/spec/lazy_spec.rb +1 -1
- data/spec/lol_spec.rb +1 -1
- data/spec/moosex_spec.rb +24 -1
- data/spec/point_spec.rb +1 -0
- data/spec/proxy_spec.rb +1 -1
- data/spec/role_spec.rb +1 -1
- data/spec/spec_helper.rb +17 -4
- data/spec/trigger_spec.rb +1 -1
- data/spec/types_spec.rb +1 -1
- data/spec/weak_spec.rb +43 -0
- metadata +24 -4
data/lib/moosex/event.rb
CHANGED
data/lib/moosex/version.rb
CHANGED
data/moosex.gemspec
CHANGED
@@ -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
|
+
|
data/samples/events.rb
CHANGED
@@ -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!"
|
data/samples/human.rb
ADDED
@@ -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
|
data/samples/point.rb
CHANGED
data/samples/roles.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
|
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.
|
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
|
-
|
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
|
-
|
66
|
+
begin
|
67
|
+
WrongClass.new(one: 1, two: 2) # will raise exception
|
68
|
+
rescue => e
|
69
|
+
puts "will raise exception #{e}"
|
70
|
+
end
|
data/spec/baserole_spec.rb
CHANGED
data/spec/baz_spec.rb
CHANGED
data/spec/build_spec.rb
CHANGED
data/spec/coerce_spec.rb
CHANGED
@@ -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
|
data/spec/complex_type_spec.rb
CHANGED
@@ -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
|
data/spec/lazy_spec.rb
CHANGED
data/spec/lol_spec.rb
CHANGED
data/spec/moosex_spec.rb
CHANGED
@@ -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
|
-
|
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
|
data/spec/point_spec.rb
CHANGED
data/spec/proxy_spec.rb
CHANGED
data/spec/role_spec.rb
CHANGED