ruby-ext 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/.git/COMMIT_EDITMSG +1 -0
  2. data/.git/HEAD +1 -0
  3. data/.git/config +12 -0
  4. data/.git/description +1 -0
  5. data/.git/hooks/applypatch-msg.sample +15 -0
  6. data/.git/hooks/commit-msg.sample +24 -0
  7. data/.git/hooks/post-commit.sample +8 -0
  8. data/.git/hooks/post-receive.sample +15 -0
  9. data/.git/hooks/post-update.sample +8 -0
  10. data/.git/hooks/pre-applypatch.sample +14 -0
  11. data/.git/hooks/pre-commit.sample +46 -0
  12. data/.git/hooks/pre-rebase.sample +169 -0
  13. data/.git/hooks/prepare-commit-msg.sample +36 -0
  14. data/.git/hooks/update.sample +128 -0
  15. data/.git/index +0 -0
  16. data/.git/info/exclude +6 -0
  17. data/.git/logs/HEAD +6 -0
  18. data/.git/logs/refs/heads/master +6 -0
  19. data/.git/logs/refs/remotes/origin/master +5 -0
  20. data/.git/objects/01/a6ccb4f4fb0c6cfdf9ec1bb23779721cf503e9 +5 -0
  21. data/.git/objects/05/18bc8be5c58a5bd9d7fc82c75c87478aa361e9 +5 -0
  22. data/.git/objects/05/b76a8d022e0e0fbca5b69b9f7b4d2a132fcc89 +0 -0
  23. data/.git/objects/06/864a068d188ab7ae17e39d58d6c25dc49a8ff3 +2 -0
  24. data/.git/objects/0e/13cf36bc744c90f576c0a31c3f76dcf1fd2485 +0 -0
  25. data/.git/objects/0f/add343d54917efbd138ee2d7c092a52b12ebd5 +0 -0
  26. data/.git/objects/10/53306d48d434b477991f5fb64a6d8d3aca84a0 +0 -0
  27. data/.git/objects/17/70296a7ba0a9aaadd3e857ae34d92d2e13e935 +0 -0
  28. data/.git/objects/20/c2ee1bb9f6749181e464a4ce444092fc1a1ecb +0 -0
  29. data/.git/objects/26/4d70a16bbad0c5b3b16fb7a92bcc6178f2cca2 +2 -0
  30. data/.git/objects/27/eb5694d78540b1a489a1aff5415d4713d5fca9 +0 -0
  31. data/.git/objects/29/88f150ee1623b97d234b2742b30caad382775b +0 -0
  32. data/.git/objects/29/8cb590f97362cc7a326815dac3289a88b69153 +0 -0
  33. data/.git/objects/2b/edc3eb69408a817194c28d14fb2dbfed5a21c0 +2 -0
  34. data/.git/objects/2e/3e3fc8e46949a9b944da3c36207c444aa2c9b7 +0 -0
  35. data/.git/objects/2f/74073c8d1580f5a8fc59389b4c1b6d89e4d85f +0 -0
  36. data/.git/objects/34/a418e8c4b43510124491ff3be262edd8008043 +3 -0
  37. data/.git/objects/38/91f271b22f2a1caee6910a34139076e27ade00 +0 -0
  38. data/.git/objects/38/bf6dbb2520b448e08ac9cb353523bbc72118db +0 -0
  39. data/.git/objects/44/73d59f32a2806eca098ba6bf10cdc2e6453542 +0 -0
  40. data/.git/objects/44/e92f51b4c9a44497c7f170598fae64564052eb +0 -0
  41. data/.git/objects/4b/2ac3de91e560277749f749ed47230ef080d36c +0 -0
  42. data/.git/objects/54/4fb213faf0c1b4cc459ada84120823d204ae3a +4 -0
  43. data/.git/objects/55/a433dd00ba298006a2fc028964eb0cde7c2e5b +0 -0
  44. data/.git/objects/55/f338afb8c82f8cad38b28d0e8cf887f9bda1ce +1 -0
  45. data/.git/objects/57/cf2479bb9c6e31118c42c5cceaffada1862c72 +0 -0
  46. data/.git/objects/59/60bdbdc3be504b89371c2b4b09d22754251260 +0 -0
  47. data/.git/objects/59/653cefe5b174e4a7002100f024448cdb337373 +0 -0
  48. data/.git/objects/5c/28c251bb201232470340d73ed4cc4926cd241d +0 -0
  49. data/.git/objects/5c/536c5d712f7cf34e21be5526aa3e826888810f +0 -0
  50. data/.git/objects/5d/37379f1b929c7b82f96382fc256895a50e97fd +0 -0
  51. data/.git/objects/60/a681e873ca0ae0b7da3f37dbc5701f6e42b970 +1 -0
  52. data/.git/objects/61/d4509b8fc63e53b8e5d417ace33919e8a7a8f2 +0 -0
  53. data/.git/objects/6b/dcac704e4f8abe1928e8f3f5d9b7e17e4992de +0 -0
  54. data/.git/objects/6c/18afba63861887fb7ac53fee1b24c181b2a8e4 +0 -0
  55. data/.git/objects/70/222d828221b1884b6b8ce124b7f22eea5d420d +0 -0
  56. data/.git/objects/74/bf8f37d70d9818474e1fb5081f04c496741a28 +0 -0
  57. data/.git/objects/77/141a7a91a61baff283e5f16e55a720d0bc65e0 +2 -0
  58. data/.git/objects/81/6bb8c35fe2169fa9ecbcd40941c440e5d032e1 +2 -0
  59. data/.git/objects/81/a5373f6837b02ace55c07cebf4a60fb6d4b084 +0 -0
  60. data/.git/objects/82/8cab0a228a826cb346b6cf4ffffac84c674e4b +3 -0
  61. data/.git/objects/84/2a9747ce87d8b90ee360f7252b557ff8e19352 +0 -0
  62. data/.git/objects/90/eea923ef08bb39013e7cd5be777b7539733638 +0 -0
  63. data/.git/objects/93/276f74ef0e157c5f81eeec7844f30d4eceda7a +0 -0
  64. data/.git/objects/9e/a37d9fc7e1ad1504b50da2a1ec639c523fb8b7 +2 -0
  65. data/.git/objects/ab/daaab37771475482389bdfcb2021b1651ba399 +0 -0
  66. data/.git/objects/ad/e1a70e7cf984a2f518d19ef625362d78a5fbd0 +3 -0
  67. data/.git/objects/b6/17a8cc2ae7f6ea496051db493aa56edc178073 +0 -0
  68. data/.git/objects/b6/863b5d4c0a4d87b81a9bbc29e786fcf7ef0df2 +0 -0
  69. data/.git/objects/bc/ee5c51061bdb9ab5a5aea4f00245a8ab3689b2 +0 -0
  70. data/.git/objects/c3/faad9ba42388a50a57acf9df88f6d2a64a4122 +1 -0
  71. data/.git/objects/d4/8b935ca7e38d2434ceb1711544293f4baf8203 +0 -0
  72. data/.git/objects/dd/e851a435431f0c03b12f7a9ca6e5dd5ce80617 +0 -0
  73. data/.git/objects/e0/c54997bfab8a0bf135d0de79ddee431b9112ad +0 -0
  74. data/.git/objects/e9/d385c1913656b7ce15a1ac1424361d2e065a6c +0 -0
  75. data/.git/objects/f0/c37cf78e53e6f859111f35d9dbf52cbfbc633d +0 -0
  76. data/.git/objects/f1/2346269ed5287267f0271f57546291c013b318 +0 -0
  77. data/.git/objects/f3/655e33b96e5b8b25990a2ae88fcdfed415074b +0 -0
  78. data/.git/objects/f7/c0fa5621b81aca548c738c08907055696e1956 +0 -0
  79. data/.git/objects/f8/c960b12cbddb90f18123673e6778efdc822058 +2 -0
  80. data/.git/objects/fb/85b5793dcebae826b04cd8c37406504540d8a4 +2 -0
  81. data/.git/objects/fd/d3ef7ec06e804f3688491e21caaa4d89796b16 +0 -0
  82. data/.git/objects/pack/pack-304aec40b1be62a8e3c105c6423354d9e62bd089.idx +0 -0
  83. data/.git/objects/pack/pack-304aec40b1be62a8e3c105c6423354d9e62bd089.pack +0 -0
  84. data/.git/packed-refs +2 -0
  85. data/.git/refs/heads/master +1 -0
  86. data/.git/refs/remotes/origin/HEAD +1 -0
  87. data/.git/refs/remotes/origin/master +1 -0
  88. data/.gitignore +5 -0
  89. data/README.rdoc +3 -0
  90. data/Rakefile +64 -0
  91. data/lib/ruby_ext/declarative_cache.rb +167 -0
  92. data/lib/ruby_ext/file.rb +23 -0
  93. data/lib/ruby_ext/kernel.rb +21 -0
  94. data/lib/ruby_ext/module.rb +84 -0
  95. data/lib/ruby_ext/not_defined.rb +2 -0
  96. data/lib/ruby_ext/object.rb +7 -0
  97. data/lib/ruby_ext/open_constructor.rb +51 -0
  98. data/lib/ruby_ext/prepare_arguments.rb +105 -0
  99. data/lib/ruby_ext/should.rb +150 -0
  100. data/lib/ruby_ext/string.rb +21 -0
  101. data/lib/ruby_ext/symbol.rb +21 -0
  102. data/lib/ruby_ext/synchronizer.rb +31 -0
  103. data/lib/ruby_ext.rb +17 -0
  104. data/spec/assert_spec.rb +27 -0
  105. data/spec/declarative_cache_spec.rb +97 -0
  106. data/spec/kernel_spec.rb +16 -0
  107. data/spec/module_spec.rb +127 -0
  108. data/spec/open_constructor_spec.rb +39 -0
  109. data/spec/prepare_arguments_spec.rb +50 -0
  110. data/spec/spec.opts +3 -0
  111. data/spec/spec_require.rb +10 -0
  112. data/spec/synchronizer_spec.rb +69 -0
  113. metadata +166 -0
@@ -0,0 +1,150 @@
1
+ class Object
2
+ def should_be! method
3
+ unless self.send("#{method}?") == true
4
+ raise RuntimeError, "
5
+ ASSERTION FAILED:
6
+ #{self.inspect} should be #{method}
7
+ ", caller
8
+ end
9
+
10
+ return self
11
+ end
12
+ alias_method :should_have!, :should_be!
13
+
14
+ def should_not_be! method
15
+ unless self.send("#{method}?") == false
16
+ raise RuntimeError, "
17
+ ASSERTION FAILED:
18
+ #{self.inspect} should not be #{method}
19
+ ", caller
20
+ end
21
+
22
+ return self
23
+ end
24
+ alias_method :should_not_have!, :should_not_be!
25
+
26
+ def should! cmd, arg = NotDefined
27
+ result = case cmd
28
+ when :be_never_called then
29
+ false
30
+
31
+ when :be_nil then
32
+ self.equal? nil
33
+
34
+ when :be_a
35
+ if arg.class == Array
36
+ arg.any?{|klass| self.is_a? klass}
37
+ else
38
+ self.is_a? arg
39
+ end
40
+
41
+ when :be
42
+ if arg.class == Array
43
+ arg.any?{|klass| self.respond_to :is?, klass}
44
+ else
45
+ self.respond_to :is?, arg
46
+ end
47
+
48
+ when :include then
49
+ self.include? arg
50
+
51
+ when :be_in then
52
+ arg.include? self
53
+
54
+ when :be_true then
55
+ self
56
+
57
+ when :be_false then
58
+ !self
59
+
60
+ when :be_empty then
61
+ self.empty?
62
+
63
+ else
64
+ if arg.equal? NotDefined
65
+ self.send cmd
66
+ else
67
+ self.send cmd, arg
68
+ end
69
+ end
70
+
71
+ unless result
72
+ unless arg.equal? NotDefined
73
+ raise RuntimeError, "
74
+ ASSERTION FAILED:
75
+ #{self.inspect} should #{cmd} #{arg.inspect}
76
+ ", caller
77
+ else
78
+ raise RuntimeError, "
79
+ ASSERTION FAILED:
80
+ #{self.inspect} should #{cmd}
81
+ ", caller
82
+ end
83
+ end
84
+
85
+ return self
86
+ end
87
+
88
+ def should_not! cmd, arg = NotDefined
89
+ result =
90
+ case cmd
91
+ when :be_never_called then
92
+ false
93
+
94
+ when :be_nil then
95
+ self.equal? nil
96
+
97
+ when :be_a
98
+ if arg.class == Array
99
+ arg.any?{|klass| self.is_a? klass}
100
+ else
101
+ self.is_a? arg
102
+ end
103
+
104
+ when :be
105
+ if arg.class == Array
106
+ arg.any?{|klass| self.respond_to :is?, klass}
107
+ else
108
+ self.respond_to :is?, arg
109
+ end
110
+
111
+ when :include then
112
+ self.include? arg
113
+
114
+ when :be_in then
115
+ arg.include? self
116
+
117
+ when :be_true then
118
+ self
119
+
120
+ when :be_false then
121
+ !self
122
+
123
+ when :be_empty then
124
+ self.empty?
125
+
126
+ else
127
+ if arg.equal? NotDefined
128
+ self.send cmd
129
+ else
130
+ self.send cmd, arg
131
+ end
132
+ end
133
+
134
+ if result
135
+ unless arg.equal? NotDefined
136
+ raise RuntimeError, "
137
+ ASSERTION FAILED:
138
+ #{self.inspect} should not #{cmd} #{arg.inspect}
139
+ ", caller
140
+ else
141
+ raise RuntimeError, "
142
+ ASSERTION FAILED:
143
+ #{self.inspect} should not #{cmd}
144
+ ", caller
145
+ end
146
+ end
147
+
148
+ return self
149
+ end
150
+ end
@@ -0,0 +1,21 @@
1
+ class String
2
+ def to_reader
3
+ self.to_sym
4
+ end
5
+
6
+ def to_writer
7
+ "#{self}=".to_sym
8
+ end
9
+
10
+ def to_iv
11
+ "@#{self}"
12
+ end
13
+
14
+ def substitute binding
15
+ binding.should! :be_a, Binding
16
+ return gsub(/\#\{.+?\}/) do |term|
17
+ identifier = term.slice(2 .. term.size-2)
18
+ binding.eval identifier
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ class Symbol
2
+ def <=> other
3
+ self.to_s <=> other.to_s
4
+ end
5
+
6
+ def to_reader
7
+ self
8
+ end
9
+
10
+ def to_writer
11
+ "#{self}=".to_sym
12
+ end
13
+
14
+ def to_iv
15
+ "@#{self}"
16
+ end
17
+
18
+ def + other
19
+ (self.to_s + other.to_s).to_sym
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'monitor'
2
+
3
+ module Synchronizer
4
+ include MonitorMixin
5
+
6
+ module ClassMethods
7
+ def synchronize *methods
8
+ methods.each do |name|
9
+ als = (name.to_s =~ /^[_a-zA-Z0-9]+$/) ? name : DeclarativeCache.alias_counter.to_sym
10
+
11
+ alias_method :"sync_#{als}", name
12
+ script = "\
13
+ def #{name} *p, &b
14
+ synchronize{sync_#{als} *p, &b}
15
+ end"
16
+ class_eval script, __FILE__, __LINE__
17
+ end
18
+ end
19
+
20
+ def synchronize_all
21
+ methods = self.instance_methods(false).collect{|m| m.to_sym}
22
+ synchronize *methods
23
+ end
24
+
25
+ protected
26
+ def alias_counter
27
+ @alias_counter += 1
28
+ return :"m#{@alias_counter}"
29
+ end
30
+ end
31
+ end
data/lib/ruby_ext.rb ADDED
@@ -0,0 +1,17 @@
1
+ %w{
2
+ kernel
3
+ module
4
+ not_defined
5
+ object
6
+ string
7
+ symbol
8
+
9
+ should
10
+ declarative_cache
11
+ file
12
+ open_constructor
13
+ prepare_arguments
14
+ synchronizer
15
+ }.each do |f|
16
+ require "#{File.dirname __FILE__}/ruby_ext/#{f}"
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/spec_require"
5
+ require "#{dir}/../lib/ruby_ext/should"
6
+
7
+ describe 'Assert' do
8
+ it 'should! & should_not!' do
9
+ lambda{should! :be_never_called}.should raise_error(/ever/)
10
+ lambda{nil.should_not! :be_nil}.should raise_error(/nil/)
11
+ 1.should_not! :be_nil
12
+ 1.should! :==, 1
13
+ lambda{1.should! :==, 2}.should raise_error(%r{==})
14
+ 1.should! :be_in, [1, 2]
15
+ "".should! :be_a, String
16
+ String.should! :be, String
17
+ 1.should! :<, 2
18
+ end
19
+
20
+ it 'should_be! & should_not_be!' do
21
+ [].should_be! :empty
22
+ [''].should_not_be! :empty
23
+
24
+ lambda{[''].should_be! :empty}.should raise_error(/should be/)
25
+ lambda{[].should_not_be! :empty}.should raise_error(/should not be/)
26
+ end
27
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/spec_require"
5
+ require "#{dir}/../lib/ruby_ext/should"
6
+ require "#{dir}/../lib/ruby_ext/declarative_cache"
7
+
8
+ describe 'DeclarativeCache' do
9
+ class CachedClass
10
+ attr_accessor :value
11
+ def value_get; @value end
12
+
13
+ attr_accessor :value2
14
+ def value2_get; @value2 end
15
+
16
+ attr_accessor :params
17
+ def params_get param; @params[param] end
18
+ end
19
+
20
+ DeclarativeCache.cached CachedClass, CachedClass, :value_get
21
+ DeclarativeCache.cached :cache_version_name, CachedClass, :value2_get
22
+ DeclarativeCache.cached_with_params CachedClass, CachedClass, :params_get
23
+
24
+ it "Simple Cache" do
25
+ o = CachedClass.new
26
+ o.value = 0
27
+ o.value2 = 0
28
+ o.value_get.should == 0
29
+ o.value2_get.should == 0
30
+ o.value = 1
31
+ o.value2 = 1
32
+ o.value_get.should == 0
33
+ o.value2_get.should == 0
34
+ DeclarativeCache.update CachedClass
35
+ o.value_get.should == 1
36
+ o.value2_get.should == 0
37
+ DeclarativeCache.update :cache_version_name
38
+ o.value2_get.should == 1
39
+ DeclarativeCache.update CachedClass, :cache_version_name
40
+ end
41
+
42
+ it "Cache With Params" do
43
+ o = CachedClass.new
44
+ o.params = {:a => :b}
45
+ o.params_get(:a).should == :b
46
+ o.params = {:a => :c}
47
+ o.params_get(:a).should == :b
48
+ DeclarativeCache.update CachedClass
49
+ o.params_get(:a).should == :c
50
+ end
51
+
52
+ class CachedClass2
53
+ class << self
54
+ attr_accessor :value
55
+ def value_get; @value end
56
+ end
57
+ end
58
+
59
+ DeclarativeCache.cached CachedClass2, CachedClass2.singleton_class, :value_get
60
+
61
+ it "Simple Cache" do
62
+ CachedClass2.value = 0
63
+ CachedClass2.value_get.should == 0
64
+ CachedClass2.value = 1
65
+ CachedClass2.value_get.should == 0
66
+ DeclarativeCache.update CachedClass2
67
+ CachedClass2.value_get.should == 1
68
+ end
69
+
70
+ class MultipleCacheVersions
71
+ attr_accessor :value
72
+ def value_get; @value end
73
+
74
+ attr_accessor :value2
75
+ def value2_get params; @value2 end
76
+ end
77
+
78
+ DeclarativeCache.cached [:class, :resource_update, :security_update], MultipleCacheVersions, :value_get
79
+ DeclarativeCache.cached_with_params [:class, :resource_update, :security_update], MultipleCacheVersions, :value2_get
80
+
81
+ it "Multiple cache versions" do
82
+ o = MultipleCacheVersions.new
83
+ o.value, o.value2 = 0, 0
84
+
85
+ o.value_get.should == 0
86
+ o.value = 1
87
+ o.value_get.should == 0
88
+ DeclarativeCache.update :class
89
+ o.value_get.should == 1
90
+ o.value = 2
91
+ o.value_get.should == 1
92
+ DeclarativeCache.update :resource_update, :security_update
93
+ o.value_get.should == 2
94
+
95
+ o.value2_get("params").should == 0
96
+ end
97
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/../lib/ruby_ext/kernel"
5
+
6
+ describe 'Kernel' do
7
+ class Respond
8
+ def test; 2 end
9
+ end
10
+
11
+ it "respond_to" do
12
+ r = Respond.new
13
+ r.respond_to(:not_exist).should be_nil
14
+ r.respond_to(:test).should == 2
15
+ end
16
+ end
@@ -0,0 +1,127 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/../lib/ruby_ext/module"
5
+
6
+
7
+ describe "Module" do
8
+ class A
9
+ class B
10
+ class C
11
+
12
+ end
13
+ end
14
+ end
15
+
16
+ it "Namespace" do
17
+ File.namespace.should == nil
18
+ A::B::C.namespace.should == A::B
19
+ end
20
+
21
+ class AnonymousClass
22
+ class << self
23
+ def anonymous_name
24
+ self.name
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ it "name" do
31
+ A::B::C.name.should == "A::B::C"
32
+ end
33
+
34
+ class X; end
35
+ class Y < X; end
36
+ class Z < Y; end
37
+
38
+
39
+ it "each_ancestor" do
40
+ list = []
41
+ Z.each_ancestor{|a| list << a}
42
+ list.should include Y
43
+ list.should include X
44
+ list.should_not include Z
45
+ list.should_not include Object
46
+ list.should_not include Kernel
47
+
48
+ list = []
49
+ Z.each_ancestor(true){|a| list << a}
50
+ list.should include Y
51
+ list.should include X
52
+ list.should_not include Z
53
+ list.should include Object
54
+ list.should include Kernel
55
+ end
56
+
57
+ it "each_namespace" do
58
+ list = []
59
+ A::B::C.each_namespace{|n| list << n}
60
+ list.should == [A::B, A]
61
+ end
62
+
63
+ it "is?" do
64
+ File.is?(IO).should be_true
65
+ end
66
+
67
+ class WrapMethod
68
+ def method_name value
69
+ 10*value
70
+ end
71
+ end
72
+
73
+ it "wrap_method" do
74
+ WrapMethod.wrap_method :method_name do |old, value|
75
+ send(old, value)*value
76
+ end
77
+ WrapMethod.new.method_name(10).should == 1000
78
+ end
79
+
80
+ module M
81
+ def m_m; end
82
+ module ClassMethods
83
+ def cm_m; end
84
+ end
85
+ end
86
+
87
+ class C
88
+ inherit M
89
+ end
90
+
91
+ it "inherit" do
92
+ C.respond_to?(:cm_m).should be_true
93
+ C.new.respond_to?(:m_m).should be_true
94
+ end
95
+
96
+ module M2
97
+ include M
98
+
99
+ module ClassMethods
100
+ def cm2_m; end
101
+ end
102
+ end
103
+
104
+ class D
105
+ inherit M2
106
+ end
107
+
108
+ it "should inherit all ancestors class methods" do
109
+ D.respond_to?(:cm_m).should be_true
110
+ D.respond_to?(:cm2_m).should be_true
111
+ D.new.respond_to?(:m_m).should be_true
112
+ end
113
+
114
+ class C2
115
+ inherit M
116
+ end
117
+
118
+ it "Shouldn't redefine ancestors class methods" do
119
+ C2.respond_to?(:cm2_m).should be_false
120
+ end
121
+
122
+ it "inherited_instance_methods" do
123
+ im = Set.instance_methods - Set.inherited_instance_methods
124
+ im.should include("union")
125
+ im.should_not include("object_id")
126
+ end
127
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/spec_require"
5
+ require "#{dir}/../lib/ruby_ext/open_constructor"
6
+
7
+ describe 'OpenConstructor' do
8
+ class Test
9
+ include OpenConstructor
10
+ attr_accessor :name, :value
11
+ end
12
+
13
+ it 'should initialize atributes from Hash' do
14
+ t = Test.new.set(:name => 'name', :value => 'value')
15
+ [t.name, t.value].should == ['name', 'value']
16
+ end
17
+
18
+ it 'should initialize atributes from any Object' do
19
+ t = Test.new.set(:name => 'name', :value => 'value')
20
+ t2 = Test.new.set t
21
+ [t2.name, t2.value].should == ['name', 'value']
22
+ end
23
+
24
+ it 'restrict copied values' do
25
+ t = Test.new.set(:name => 'name', :value => 'value')
26
+ t2 = Test.new.set t, [:name]
27
+ [t2.name, t2.value].should == ['name', nil]
28
+
29
+ t = {:name => 'name', :value => 'value'}
30
+ t2 = Test.new.set t, [:name]
31
+ [t2.name, t2.value].should == ['name', nil]
32
+ end
33
+
34
+ it 'to_hash' do
35
+ h = {:name => 'name', :value => 'value'}
36
+ t = Test.new.set h
37
+ t.to_hash.should == h
38
+ end
39
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/spec_require"
5
+ require "#{dir}/../lib/ruby_ext/should"
6
+ require "#{dir}/../lib/ruby_ext/prepare_arguments"
7
+
8
+ describe "Prepare Arguments" do
9
+
10
+ it "basic parsing" do
11
+ ArgumentsParser.register :m, [:object, :array, :hash]
12
+ ArgumentsParser.parse_arguments_for(:m, :o, 1).should == [:o, [1], {}]
13
+ end
14
+
15
+ it "basic method wrapping" do
16
+ class BMWTest
17
+ def add content, options
18
+ :ok
19
+ end
20
+ prepare_arguments_for :add, {:type => :object, :required => true}, :hash
21
+ end
22
+
23
+ BMWTest.new.add('text').should == :ok
24
+ end
25
+
26
+ it "except_last_hash for array" do
27
+ ArgumentsParser.register :m, [{:type => :array, :range => :except_last_hash}, :hash]
28
+ ArgumentsParser.parse_arguments_for(:m, 1, 2, 3, :a => :b).should == [[1, 2, 3], {:a => :b}]
29
+ ArgumentsParser.parse_arguments_for(:m, :a => :b).should == [[], {:a => :b}]
30
+ ArgumentsParser.parse_arguments_for(:m, 1, 2, 3).should == [[1, 2, 3], {}]
31
+ end
32
+
33
+ it "except_last_hash for object" do
34
+ ArgumentsParser.register :m, [{:type => :object, :range => :except_last_hash}, :hash]
35
+ ArgumentsParser.parse_arguments_for(:m, 1, :a => :b).should == [1, {:a => :b}]
36
+ ArgumentsParser.parse_arguments_for(:m, :a => :b).should == [nil, {:a => :b}]
37
+ ArgumentsParser.parse_arguments_for(:m, 1).should == [1, {}]
38
+ end
39
+
40
+ it "default value" do
41
+ ArgumentsParser.register :m, [{:type => :object, :default => ""}]
42
+ ArgumentsParser.parse_arguments_for(:m).should == [""]
43
+ end
44
+
45
+ it "string" do
46
+ ArgumentsParser.register :m, [:string]
47
+ ArgumentsParser.parse_arguments_for(:m).should == [""]
48
+ end
49
+
50
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --reverse
3
+ --backtrace
@@ -0,0 +1,10 @@
1
+ %w{
2
+ kernel
3
+ module
4
+ not_defined
5
+ object
6
+ string
7
+ symbol
8
+ }.each do |f|
9
+ require "#{File.dirname __FILE__}/../lib/ruby_ext/#{f}"
10
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec'
2
+
3
+ dir = File.dirname __FILE__
4
+ require "#{dir}/spec_require"
5
+ require "#{dir}/../lib/ruby_ext/synchronizer"
6
+
7
+ module RubyExt
8
+ describe "Synchronize" do
9
+ it "synchronize" do
10
+ class Account
11
+ inherit Synchronizer
12
+ attr_reader :from, :to
13
+
14
+ def initialize
15
+ super
16
+ @from, @to = 0, 0
17
+ end
18
+
19
+ def transfer
20
+ @from -= 1
21
+ @to += 1
22
+ end
23
+ synchronize :transfer
24
+ end
25
+
26
+ a, threads = Account.new, []
27
+ 100.times do
28
+ t = Thread.new do
29
+ 100.times{a.transfer}
30
+ end
31
+ threads << t
32
+ end
33
+ threads.each{|t| t.join}
34
+
35
+ a.from.should == -10_000
36
+ a.to.should == 10_000
37
+ end
38
+
39
+ it "synchronize_all" do
40
+ class Account
41
+ inherit Synchronizer
42
+ attr_reader :from, :to
43
+
44
+ def initialize
45
+ super
46
+ @from, @to = 0, 0
47
+ end
48
+
49
+ def transfer
50
+ @from -= 1
51
+ @to += 1
52
+ end
53
+ synchronize_all
54
+ end
55
+
56
+ a, threads = Account.new, []
57
+ 100.times do
58
+ t = Thread.new do
59
+ 100.times{a.transfer}
60
+ end
61
+ threads << t
62
+ end
63
+ threads.each{|t| t.join}
64
+
65
+ a.from.should == -10_000
66
+ a.to.should == 10_000
67
+ end
68
+ end
69
+ end