ruby_ext 0.4.12 → 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake_ext'
5
5
 
6
6
  project(
7
7
  name: "ruby_ext",
8
- version: "0.4.12",
8
+ version: "0.4.13",
9
9
  summary: "Ruby Extensions",
10
10
 
11
11
  author: "Alexey Petrushin",
data/lib/rake_ext.rb CHANGED
@@ -1,6 +1,20 @@
1
1
  require 'rake'
2
2
  require 'fileutils'
3
3
 
4
+ #
5
+ # Code Coverage
6
+ #
7
+ begin
8
+ require 'class_loader/tasks'
9
+ rescue LoadError
10
+ end
11
+ desc "Clean temporary files"
12
+ task clean: 'class_loader:clean' do
13
+ require 'fileutils'
14
+ FileUtils.rm_r '../*/coverage' if File.exist? '../*/coverage'
15
+ end
16
+
17
+
4
18
  #
5
19
  # Spec
6
20
  #
data/lib/rspec_ext.rb CHANGED
@@ -1,3 +1,21 @@
1
+ #
2
+ # Code Coverage
3
+ #
4
+ # require 'simplecov'
5
+ # SimpleCov.start do
6
+ # add_filter "/spec/"
7
+ # end
8
+ # SimpleCov.at_exit do
9
+ # SimpleCov.result.format!
10
+ # Kernel.exec 'open ./coverage/index.html'
11
+ # end
12
+ #
13
+ # CLASS_LOADER_GENERATE_TMP_FILES = true
14
+
15
+
16
+ #
17
+ # Rspec
18
+ #
1
19
  require 'rspec'
2
20
  require 'fileutils'
3
21
 
@@ -22,15 +40,18 @@ rspec do
22
40
  end
23
41
 
24
42
  rspec do
25
- def self.with_load_path *args
26
- list = if args.first.is_a? Array
27
- args.first
28
- else
29
- args
43
+ def self.with_load_path *paths
44
+ before(:all){paths.each{|path| $LOAD_PATH << path}}
45
+ after(:all){paths.each{|path| $LOAD_PATH.delete path}}
46
+ end
47
+
48
+ def with_load_path *paths, &b
49
+ begin
50
+ paths.each{|path| $LOAD_PATH << path}
51
+ b.call
52
+ ensure
53
+ paths.each{|path| $LOAD_PATH.delete path}
30
54
  end
31
-
32
- before(:all){list.each{|path| $LOAD_PATH << path}}
33
- after(:all){list.each{|path| $LOAD_PATH.delete path}}
34
55
  end
35
56
 
36
57
  def self.with_tmp_spec_dir *args
@@ -11,5 +11,7 @@ class Array
11
11
  Array(value)
12
12
  end
13
13
 
14
+ alias_method :blank?, :empty?
15
+
14
16
  alias_method :filter, :select
15
17
  end
@@ -0,0 +1,3 @@
1
+ class FalseClass
2
+ def blank?; true end
3
+ end
@@ -19,5 +19,6 @@ class Hash
19
19
  raise "unknown options :#{unknown_options.join(': ')}!" unless unknown_options.empty?
20
20
  end
21
21
 
22
+ alias_method :blank?, :empty?
22
23
  alias_method :to_h, :to_hash
23
24
  end
@@ -126,5 +126,11 @@ Module.class_eval do
126
126
  m.to_sym
127
127
  end
128
128
 
129
+ def require_attr *attrs
130
+ attrs.each do |attr|
131
+ define_method(attr){instance_variable_get(:"@#{attr}") || raise("key :#{attr} not defined!")}
132
+ end
133
+ end
134
+
129
135
  public :include, :define_method
130
136
  end
@@ -0,0 +1,3 @@
1
+ class NilClass
2
+ def blank?; true end
3
+ end
@@ -108,6 +108,22 @@ class OpenObject < Hash
108
108
  def respond_to? m
109
109
  true
110
110
  end
111
+
112
+ def self.initialize_from hash, deep = false
113
+ unless deep
114
+ ::OpenObject.new.update hash
115
+ else
116
+ r = ::OpenObject.new
117
+ hash.each do |k, v|
118
+ r[k] = if v.is_a? Hash
119
+ v.to_openobject
120
+ else
121
+ v
122
+ end
123
+ end
124
+ r
125
+ end
126
+ end
111
127
 
112
128
  protected
113
129
  def method_missing m, arg = nil, &block
@@ -137,19 +153,7 @@ end
137
153
 
138
154
  class Hash
139
155
  def to_openobject deep = false
140
- unless deep
141
- OpenObject.new.update self
142
- else
143
- r = OpenObject.new
144
- each do |k, v|
145
- r[k] = if v.is_a? Hash
146
- v.to_openobject
147
- else
148
- v
149
- end
150
- end
151
- r
152
- end
156
+ OpenObject.initialize_from self, deep
153
157
  end
154
158
  alias_method :to_oo, :to_openobject
155
159
 
@@ -68,4 +68,6 @@ String.class_eval do
68
68
  end
69
69
  constant
70
70
  end
71
+
72
+ alias_method :blank?, :empty?
71
73
  end
@@ -18,4 +18,6 @@ Symbol.class_eval do
18
18
  def + other
19
19
  (self.to_s + other.to_s).to_sym
20
20
  end
21
+
22
+ def blank?; to_s.blank? end
21
23
  end
@@ -0,0 +1,3 @@
1
+ class TrueClass
2
+ def blank?; false end
3
+ end
data/lib/ruby_ext/core.rb CHANGED
@@ -5,6 +5,7 @@ require 'ruby_ext/fixes'
5
5
 
6
6
  %w(
7
7
  basic_object
8
+ nil_class
8
9
  file
9
10
  enumerable
10
11
  array
@@ -10,16 +10,15 @@ class SafeHash < BasicObject
10
10
  end
11
11
 
12
12
  def []= key, value
13
- ::SafeHash.forbidden! key
14
- # value = ::SafeHash.new value if value.is_a? ::Hash
15
- # @hash[key.to_sym] = value
16
- end
17
-
18
- def set! key, value
19
13
  value = ::SafeHash.new value if value.is_a? ::Hash
20
14
  @hash[key.to_sym] = value
21
15
  end
22
- def set *args; raise "you probably mistyped :set! method!" end
16
+
17
+ # def set! key, value
18
+ # value = ::SafeHash.new value if value.is_a? ::Hash
19
+ # @hash[key.to_sym] = value
20
+ # end
21
+ # def set *args; raise "you probably mistyped :set! method!" end
23
22
 
24
23
  def include? key
25
24
  @hash.include? key.to_sym
@@ -68,8 +67,7 @@ class SafeHash < BasicObject
68
67
  raise "invalid usage, can't pass block to (:#{m})!" if b
69
68
  last = m[-1]
70
69
  if last == '='
71
- ::SafeHash.forbidden! m[0..-2]
72
- # self[m[0..-2]] = obj
70
+ self[m[0..-2]] = obj
73
71
  else
74
72
  self[m, obj]
75
73
  end
@@ -87,42 +85,6 @@ class SafeHash < BasicObject
87
85
  @hash.delete key.to_sym
88
86
  end
89
87
 
90
- def merge! hash, options = {}
91
- # parsing arguments
92
- deep = options[:deep] || true
93
- override = options[:override] || false
94
- blank = options[:blank] || false
95
- options.validate_options! :deep, :override, :blank
96
- raise "invalid options, can't do both :blank and :override simultaneously!" if blank and override
97
-
98
- # merging
99
- hash.each do |k, v|
100
- k = k.to_sym
101
- if @hash.include? k
102
- if deep and (old_v = @hash[k]).respond_to(:is_a_safe_hash?) and v.is_a?(::Hash)
103
- old_v.merge! v, options
104
- else
105
- if blank
106
- # do nothing
107
- elsif override
108
- self.set! k, v
109
- else
110
- raise "can't override :#{k} config value!" unless override
111
- end
112
- end
113
- else
114
- self.set! k, v
115
- end
116
- end
117
- end
118
- def merge *args; raise "you probably mistyped :merge! method!" end
119
-
120
- def merge_if_blank! hash, options = {}
121
- options[:blank] = true
122
- merge! hash, options
123
- end
124
- def merge_if_blank *args; raise "you probably mistyped :merge_if_blank method!" end
125
-
126
88
  # deep conversion, check and converts nested SafeHashes to Hashes
127
89
  def to_hash options = {}
128
90
  r = {}
@@ -141,6 +103,16 @@ class SafeHash < BasicObject
141
103
  def is_a_safe_hash?
142
104
  true
143
105
  end
106
+
107
+ protected
108
+ def reinitialize hash
109
+ @hash = {}
110
+ hash.each do |k, v|
111
+ v = ::SafeHash.new v if v.is_a? ::Hash
112
+ @hash[k.to_sym] = v
113
+ end
114
+ @hash
115
+ end
144
116
 
145
117
 
146
118
  class SafeNil < BasicObject
@@ -167,8 +139,7 @@ class SafeHash < BasicObject
167
139
  raise "invalid usage, can't pass block to (:#{m})!" if b
168
140
  last = m[-1]
169
141
  if last == '='
170
- ::SafeHash.forbidden! m[0..-2]
171
- # raise "No key '#{@key}'!"
142
+ raise "no key '#{@key}'!"
172
143
  else
173
144
  self[m, if_not_exist]
174
145
  end
@@ -197,15 +168,7 @@ class SafeHash < BasicObject
197
168
  def inspect
198
169
  nil.inspect
199
170
  end
200
- end
201
-
202
-
203
- class << self
204
- def forbidden! key
205
- raise "direct modifications of SafeHash is forbidden, use :merge instead (you are trying to assign :#{key} value)!"
206
- end
207
- end
208
-
171
+ end
209
172
 
210
173
  protected
211
174
  def p *a
@@ -27,15 +27,15 @@ describe "SafeHash and SafeNil" do
27
27
  h.v!.should == nil
28
28
  end
29
29
 
30
- it "should not allow direct modification of hash" do
30
+ it "should modify hash" do
31
31
  h = SafeHash.new
32
- lambda{h.a = 1}.should raise_error(/forbidden/)
33
- lambda{h[:a] = 1}.should raise_error(/forbidden/)
34
- lambda{h.a.b = 1}.should raise_error(/forbidden/)
32
+ h.a = 1
33
+ h.a!.should == 1
34
+ h[:b] = 1
35
+ h.b!.should == 1
36
+ lambda{h.c.d = 1}.should raise_error(/no key/)
35
37
  end
36
38
 
37
- it "should not allo"
38
-
39
39
  # it "should allow owerride values" do
40
40
  # h = SafeHash.new a: :b
41
41
  # h.b = :c
@@ -76,12 +76,12 @@ describe "SafeHash and SafeNil" do
76
76
  lambda{h.j.b.c!}.should raise_error(/no key :c/)
77
77
  end
78
78
 
79
- it "should be able to update itself" do
80
- h = SafeHash.new
81
- h.b?.should be_false
82
- h.merge! a: :a
83
- h.a!.should == :a
84
- end
79
+ # it "should be able to update itself" do
80
+ # h = SafeHash.new
81
+ # h.b?.should be_false
82
+ # h.merge! a: :a
83
+ # h.a!.should == :a
84
+ # end
85
85
 
86
86
  it "should implement include?" do
87
87
  h = SafeHash.new a: :b
@@ -90,44 +90,12 @@ describe "SafeHash and SafeNil" do
90
90
  h.include?(:b).should be_false
91
91
 
92
92
  h.b.include?(:a).should be_false
93
- end
93
+ end
94
94
 
95
- describe "merge" do
96
- it "should not override by default" do
97
- h = SafeHash.new a: :b
98
- lambda{h.merge! a: :c}.should raise_error(/can't override/)
99
- end
100
-
101
- it "should skip existing if specified" do
102
- h = SafeHash.new a: :b
103
- h.merge!({a: :c, d: :e}, blank: true)
104
- h.a!.should == :b
105
- h.d!.should == :e
106
- end
107
-
108
- it "merging hashes should not be countig as overriding" do
109
- h = SafeHash.new b: {c: :d}
110
- h.merge! b: {e: :f}, override: false
111
- end
112
-
113
- it "should override if specified" do
114
- h = SafeHash.new a: {b: :c}
115
- h.merge!({a: {b: :c2}}, override: true)
116
- h.a!.b!.should == :c2
117
- end
118
-
119
- it "should perform deep merge by default" do
120
- h = SafeHash.new a: {b: {c: :d}}
121
- h.merge!({a: {b: {c2: :d2}}})
122
- h.a!.b!.c?.should be_true
123
- h.a!.b!.c2?.should be_true
124
- end
125
- end
126
-
127
- it "merge_if_blank" do
128
- h = SafeHash.new a: :b
129
- h.merge_if_blank! a: :c, d: :e
130
- h.a!.should == :b
131
- h.d!.should == :e
132
- end
95
+ # it "merge_if_blank" do
96
+ # h = SafeHash.new a: :b
97
+ # h.merge_if_blank! a: :c, d: :e
98
+ # h.a!.should == :b
99
+ # h.d!.should == :e
100
+ # end
133
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.4.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-12 00:00:00.000000000 +04:00
12
+ date: 2011-07-02 00:00:00.000000000 +04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
  description:
@@ -28,6 +28,7 @@ files:
28
28
  - lib/ruby_ext/core/class.rb
29
29
  - lib/ruby_ext/core/deep_clone.rb
30
30
  - lib/ruby_ext/core/enumerable.rb
31
+ - lib/ruby_ext/core/false_class.rb
31
32
  - lib/ruby_ext/core/file.rb
32
33
  - lib/ruby_ext/core/hash.rb
33
34
  - lib/ruby_ext/core/kernel.rb
@@ -35,11 +36,13 @@ files:
35
36
  - lib/ruby_ext/core/module.rb
36
37
  - lib/ruby_ext/core/multiple_inheritance.rb
37
38
  - lib/ruby_ext/core/must.rb
39
+ - lib/ruby_ext/core/nil_class.rb
38
40
  - lib/ruby_ext/core/not_defined.rb
39
41
  - lib/ruby_ext/core/object.rb
40
42
  - lib/ruby_ext/core/open_object.rb
41
43
  - lib/ruby_ext/core/string.rb
42
44
  - lib/ruby_ext/core/symbol.rb
45
+ - lib/ruby_ext/core/true_class.rb
43
46
  - lib/ruby_ext/core.rb
44
47
  - lib/ruby_ext/fixes.rb
45
48
  - lib/ruby_ext/gems.rb