ruby_ext 0.4.23 → 0.4.24
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_ext/core/file.rb +7 -7
- data/lib/ruby_ext/core/string.rb +9 -9
- data/lib/ruby_ext/core/symbol.rb +11 -11
- data/lib/ruby_ext/more/open_constructor.rb +5 -5
- data/lib/ruby_ext/more.rb +0 -1
- metadata +2 -4
- data/lib/ruby_ext/more/safe_hash.rb +0 -177
- data/spec/more/safe_hash_spec.rb +0 -101
data/lib/ruby_ext/core/file.rb
CHANGED
@@ -12,12 +12,12 @@ File.class_eval do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def create_directory dir
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def delete_directory dir
|
20
|
-
|
21
|
-
end
|
15
|
+
# def create_directory dir
|
16
|
+
# FileUtils.mkdir_p dir unless File.exist? dir
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# def delete_directory dir
|
20
|
+
# FileUtils.rm_r dir, force: true if File.exist? dir
|
21
|
+
# end
|
22
22
|
end
|
23
23
|
end
|
data/lib/ruby_ext/core/string.rb
CHANGED
@@ -7,17 +7,17 @@ String.class_eval do
|
|
7
7
|
File.expand_path(self)
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_reader
|
11
|
-
|
12
|
-
end
|
10
|
+
# def to_reader
|
11
|
+
# self.to_sym
|
12
|
+
# end
|
13
13
|
|
14
|
-
def to_writer
|
15
|
-
|
16
|
-
end
|
14
|
+
# def to_writer
|
15
|
+
# "#{self}=".to_sym
|
16
|
+
# end
|
17
17
|
|
18
|
-
def to_iv
|
19
|
-
|
20
|
-
end
|
18
|
+
# def to_iv
|
19
|
+
# "@#{self}"
|
20
|
+
# end
|
21
21
|
|
22
22
|
def to_a
|
23
23
|
[self]
|
data/lib/ruby_ext/core/symbol.rb
CHANGED
@@ -3,17 +3,17 @@ Symbol.class_eval do
|
|
3
3
|
self.to_s <=> other.to_s
|
4
4
|
end
|
5
5
|
|
6
|
-
def to_reader
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
def to_writer
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
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
17
|
|
18
18
|
def + other
|
19
19
|
(self.to_s + other.to_s).to_sym
|
@@ -3,11 +3,11 @@ module OpenConstructor
|
|
3
3
|
unless list
|
4
4
|
if values.is_a? Hash
|
5
5
|
values.each do |k, v|
|
6
|
-
self.respond_to k
|
6
|
+
self.respond_to "#{k}=", v
|
7
7
|
end
|
8
8
|
else
|
9
9
|
values.instance_variables.each do |name|
|
10
|
-
accessor = name[1..name.size]
|
10
|
+
accessor = "#{name[1..name.size]}="
|
11
11
|
if self.respond_to? accessor
|
12
12
|
self.send accessor, values.instance_variable_get(name)
|
13
13
|
end
|
@@ -16,13 +16,13 @@ module OpenConstructor
|
|
16
16
|
else
|
17
17
|
if values.is_a? Hash
|
18
18
|
values.each do |k, v|
|
19
|
-
self.respond_to k
|
19
|
+
self.respond_to "#{k}=", v if list.include? k
|
20
20
|
end
|
21
21
|
else
|
22
22
|
values.instance_variables.each do |name|
|
23
23
|
accessor = name[1..name.size]
|
24
24
|
if list.include?(accessor.to_sym)
|
25
|
-
accessor = accessor
|
25
|
+
accessor = "#{accessor}="
|
26
26
|
if self.respond_to?(accessor)
|
27
27
|
self.send accessor, values.instance_variable_get(name)
|
28
28
|
end
|
@@ -35,7 +35,7 @@ module OpenConstructor
|
|
35
35
|
|
36
36
|
def set! values
|
37
37
|
values.each do |k, v|
|
38
|
-
self.send k
|
38
|
+
self.send "#{k}=", v
|
39
39
|
end
|
40
40
|
return self
|
41
41
|
end
|
data/lib/ruby_ext/more.rb
CHANGED
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.
|
4
|
+
version: 0.4.24
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-08-
|
13
|
+
date: 2011-08-08 00:00:00 +04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -56,7 +56,6 @@ files:
|
|
56
56
|
- lib/ruby_ext/more/miscellaneous.rb
|
57
57
|
- lib/ruby_ext/more/observable2.rb
|
58
58
|
- lib/ruby_ext/more/open_constructor.rb
|
59
|
-
- lib/ruby_ext/more/safe_hash.rb
|
60
59
|
- lib/ruby_ext/more/synchronize.rb
|
61
60
|
- lib/ruby_ext/more/tuple.rb
|
62
61
|
- lib/ruby_ext/more.rb
|
@@ -78,7 +77,6 @@ files:
|
|
78
77
|
- spec/more/miscellaneous_spec.rb
|
79
78
|
- spec/more/observable2_spec.rb
|
80
79
|
- spec/more/open_constructor_spec.rb
|
81
|
-
- spec/more/safe_hash_spec.rb
|
82
80
|
- spec/more/spec_helper.rb
|
83
81
|
- spec/more/synchronize_spec.rb
|
84
82
|
has_rdoc: true
|
@@ -1,177 +0,0 @@
|
|
1
|
-
# require 'singleton'
|
2
|
-
|
3
|
-
class SafeHash < BasicObject
|
4
|
-
attr_reader :hash
|
5
|
-
|
6
|
-
alias_method :send, :__send__
|
7
|
-
|
8
|
-
def initialize hash = {}
|
9
|
-
reinitialize hash
|
10
|
-
end
|
11
|
-
|
12
|
-
def []= key, value
|
13
|
-
value = ::SafeHash.new value if value.is_a? ::Hash
|
14
|
-
@hash[key.to_sym] = value
|
15
|
-
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
|
22
|
-
|
23
|
-
def include? key
|
24
|
-
@hash.include? key.to_sym
|
25
|
-
end
|
26
|
-
|
27
|
-
def tap &b
|
28
|
-
b.call self
|
29
|
-
self
|
30
|
-
end
|
31
|
-
|
32
|
-
def [] key, if_not_exist = ::NotDefined
|
33
|
-
last = key[-1]
|
34
|
-
if last == '!'
|
35
|
-
key = key[0..-2].to_sym
|
36
|
-
if @hash.include? key
|
37
|
-
@hash[key]
|
38
|
-
else
|
39
|
-
raise "no key :#{key}"
|
40
|
-
end
|
41
|
-
elsif last == '?'
|
42
|
-
key = key[0..-2].to_sym
|
43
|
-
@hash.include? key
|
44
|
-
else
|
45
|
-
key = key.to_sym
|
46
|
-
if @hash.include? key
|
47
|
-
@hash[key]
|
48
|
-
elsif if_not_exist == ::NotDefined
|
49
|
-
SafeNil.new key
|
50
|
-
else
|
51
|
-
return if_not_exist
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def reinitialize hash
|
57
|
-
@hash = {}
|
58
|
-
merge! hash
|
59
|
-
# hash.each do |k, v|
|
60
|
-
# v = ::SafeHash.new v if v.is_a? ::Hash
|
61
|
-
# @hash[k.to_sym] = v
|
62
|
-
# end
|
63
|
-
# @hash.freeze
|
64
|
-
end
|
65
|
-
|
66
|
-
def method_missing m, obj = ::NotDefined, &b
|
67
|
-
raise "invalid usage, can't pass block to (:#{m})!" if b
|
68
|
-
last = m[-1]
|
69
|
-
if last == '='
|
70
|
-
self[m[0..-2]] = obj
|
71
|
-
else
|
72
|
-
self[m, obj]
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def to_yaml *args
|
77
|
-
@hash.to_yaml *args
|
78
|
-
end
|
79
|
-
|
80
|
-
def inspect
|
81
|
-
@hash.inspect
|
82
|
-
end
|
83
|
-
|
84
|
-
def delete key
|
85
|
-
@hash.delete key.to_sym
|
86
|
-
end
|
87
|
-
|
88
|
-
# deep conversion, check and converts nested SafeHashes to Hashes
|
89
|
-
def to_hash options = {}
|
90
|
-
r = {}
|
91
|
-
@hash.each do |k, v|
|
92
|
-
k = k.to_s if options[:to_s]
|
93
|
-
r[k] = if v.respond_to :is_a_safe_hash?
|
94
|
-
v.to_hash options
|
95
|
-
else
|
96
|
-
v
|
97
|
-
end
|
98
|
-
end
|
99
|
-
r
|
100
|
-
end
|
101
|
-
alias_method :to_h, :to_hash
|
102
|
-
|
103
|
-
def is_a_safe_hash?
|
104
|
-
true
|
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
|
116
|
-
|
117
|
-
|
118
|
-
class SafeNil < BasicObject
|
119
|
-
# include ::Singleton
|
120
|
-
|
121
|
-
def initialize key
|
122
|
-
@key = key
|
123
|
-
end
|
124
|
-
|
125
|
-
def [] key, if_not_exist = ::NotDefined
|
126
|
-
last = key[-1]
|
127
|
-
if last == '!'
|
128
|
-
raise "no key :#{key}"
|
129
|
-
elsif last == '?'
|
130
|
-
false
|
131
|
-
elsif if_not_exist == ::NotDefined
|
132
|
-
SafeNil.new key
|
133
|
-
else
|
134
|
-
return if_not_exist
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def method_missing m, if_not_exist = ::NotDefined, &b
|
139
|
-
raise "invalid usage, can't pass block to (:#{m})!" if b
|
140
|
-
last = m[-1]
|
141
|
-
if last == '='
|
142
|
-
raise "no key '#{@key}'!"
|
143
|
-
else
|
144
|
-
self[m, if_not_exist]
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def include? key
|
149
|
-
false
|
150
|
-
end
|
151
|
-
|
152
|
-
def to_b
|
153
|
-
false
|
154
|
-
end
|
155
|
-
|
156
|
-
def to_yaml *args
|
157
|
-
nil.to_yaml *args
|
158
|
-
end
|
159
|
-
|
160
|
-
def to_h
|
161
|
-
{}
|
162
|
-
end
|
163
|
-
|
164
|
-
def to_s
|
165
|
-
raise "can't convert SafeNil for key '#{@key}' to String!"
|
166
|
-
end
|
167
|
-
|
168
|
-
def inspect
|
169
|
-
nil.inspect
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
protected
|
174
|
-
def p *a
|
175
|
-
::Object.send :p, *a
|
176
|
-
end
|
177
|
-
end
|
data/spec/more/safe_hash_spec.rb
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'more/spec_helper'
|
2
|
-
|
3
|
-
describe "SafeHash and SafeNil" do
|
4
|
-
it "should allow check for value presence" do
|
5
|
-
h = SafeHash.new a: :b
|
6
|
-
h.a?.should be_true
|
7
|
-
h.b?.should be_false
|
8
|
-
|
9
|
-
h.should include(:a)
|
10
|
-
h.should_not include(:b)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "? should return boolean (from error)" do
|
14
|
-
h = SafeHash.new
|
15
|
-
lambda{raise "" unless h.a?.class.equal?(NilClass)}.should raise_error
|
16
|
-
lambda{raise "" unless h.a.a?.class.equal?(NilClass)}.should raise_error
|
17
|
-
end
|
18
|
-
|
19
|
-
it "passing block should be threated as invalid usage" do
|
20
|
-
h = SafeHash.new
|
21
|
-
lambda{h.development{}}.should raise_error(/invalid usage/)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should treat assigned nil as value (from error)" do
|
25
|
-
h = SafeHash.new v: nil
|
26
|
-
h.v?.should be_true
|
27
|
-
h.v!.should == nil
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should modify hash" do
|
31
|
-
h = SafeHash.new
|
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/)
|
37
|
-
end
|
38
|
-
|
39
|
-
# it "should allow owerride values" do
|
40
|
-
# h = SafeHash.new a: :b
|
41
|
-
# h.b = :c
|
42
|
-
# h.b!.should == :c
|
43
|
-
# end
|
44
|
-
|
45
|
-
it "general behaviour" do
|
46
|
-
h = SafeHash.new key: :value
|
47
|
-
|
48
|
-
h.key.should == :value
|
49
|
-
h.key(:missing).should == :value
|
50
|
-
|
51
|
-
h[:key].should == :value
|
52
|
-
h[:key, :missing].should == :value
|
53
|
-
|
54
|
-
h['key'].should == :value
|
55
|
-
h['key', :missing].should == :value
|
56
|
-
|
57
|
-
h.a.b.c[:d].e('missing').should == 'missing'
|
58
|
-
h.a.b.c[:d][:e, 'missing'].should == 'missing'
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should build hierarchies of SafeHash" do
|
62
|
-
h = SafeHash.new a: {a: :b}
|
63
|
-
|
64
|
-
h.a.a.should == :b
|
65
|
-
h.a.missing.b.c('missing').should == 'missing'
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should require setting if ! used" do
|
69
|
-
h = SafeHash.new a: :v, b: {c: :v}
|
70
|
-
|
71
|
-
h.a!.should == :v
|
72
|
-
h.b.c!.should == :v
|
73
|
-
h.b!.c!.should == :v
|
74
|
-
|
75
|
-
lambda{h.j!}.should raise_error(/no key :j/)
|
76
|
-
lambda{h.j.b.c!}.should raise_error(/no key :c/)
|
77
|
-
end
|
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
|
85
|
-
|
86
|
-
it "should implement include?" do
|
87
|
-
h = SafeHash.new a: :b
|
88
|
-
h.include?(:a).should be_true
|
89
|
-
h.include?('a').should be_true
|
90
|
-
h.include?(:b).should be_false
|
91
|
-
|
92
|
-
h.b.include?(:a).should be_false
|
93
|
-
end
|
94
|
-
|
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
|
101
|
-
end
|