robsharp-extlib 0.9.15
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.
- data/.autotest +21 -0
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +47 -0
- data/README.rdoc +17 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/extlib.gemspec +147 -0
- data/lib/extlib.rb +50 -0
- data/lib/extlib/array.rb +38 -0
- data/lib/extlib/assertions.rb +8 -0
- data/lib/extlib/blank.rb +89 -0
- data/lib/extlib/boolean.rb +11 -0
- data/lib/extlib/byte_array.rb +6 -0
- data/lib/extlib/class.rb +179 -0
- data/lib/extlib/datetime.rb +29 -0
- data/lib/extlib/dictionary.rb +433 -0
- data/lib/extlib/hash.rb +450 -0
- data/lib/extlib/hook.rb +407 -0
- data/lib/extlib/inflection.rb +442 -0
- data/lib/extlib/lazy_array.rb +453 -0
- data/lib/extlib/lazy_module.rb +18 -0
- data/lib/extlib/logger.rb +198 -0
- data/lib/extlib/mash.rb +157 -0
- data/lib/extlib/module.rb +51 -0
- data/lib/extlib/nil.rb +5 -0
- data/lib/extlib/numeric.rb +5 -0
- data/lib/extlib/object.rb +178 -0
- data/lib/extlib/object_space.rb +13 -0
- data/lib/extlib/pathname.rb +20 -0
- data/lib/extlib/pooling.rb +235 -0
- data/lib/extlib/rubygems.rb +38 -0
- data/lib/extlib/simple_set.rb +66 -0
- data/lib/extlib/string.rb +176 -0
- data/lib/extlib/struct.rb +17 -0
- data/lib/extlib/symbol.rb +21 -0
- data/lib/extlib/time.rb +44 -0
- data/lib/extlib/try_dup.rb +44 -0
- data/lib/extlib/virtual_file.rb +10 -0
- data/spec/array_spec.rb +40 -0
- data/spec/blank_spec.rb +86 -0
- data/spec/byte_array_spec.rb +8 -0
- data/spec/class_spec.rb +158 -0
- data/spec/datetime_spec.rb +22 -0
- data/spec/hash_spec.rb +536 -0
- data/spec/hook_spec.rb +1235 -0
- data/spec/inflection/plural_spec.rb +565 -0
- data/spec/inflection/singular_spec.rb +498 -0
- data/spec/inflection_extras_spec.rb +111 -0
- data/spec/lazy_array_spec.rb +1961 -0
- data/spec/lazy_module_spec.rb +38 -0
- data/spec/mash_spec.rb +312 -0
- data/spec/module_spec.rb +71 -0
- data/spec/object_space_spec.rb +10 -0
- data/spec/object_spec.rb +114 -0
- data/spec/pooling_spec.rb +511 -0
- data/spec/rcov.opts +6 -0
- data/spec/simple_set_spec.rb +58 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/string_spec.rb +222 -0
- data/spec/struct_spec.rb +13 -0
- data/spec/symbol_spec.rb +9 -0
- data/spec/time_spec.rb +31 -0
- data/spec/try_call_spec.rb +74 -0
- data/spec/try_dup_spec.rb +46 -0
- data/spec/virtual_file_spec.rb +22 -0
- data/tasks/ci.rake +1 -0
- data/tasks/metrics.rake +36 -0
- data/tasks/spec.rake +25 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +198 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
class Struct
|
2
|
+
##
|
3
|
+
# Get a hash with names and values of all instance variables.
|
4
|
+
#
|
5
|
+
# class Foo < Struct.new(:name, :age, :gender); end
|
6
|
+
# f = Foo.new("Jill", 50, :female)
|
7
|
+
# f.attributes #=> {:name => "Jill", :age => 50, :gender => :female}
|
8
|
+
#
|
9
|
+
# @return [Hash] Hash of instance variables in receiver, keyed by ivar name
|
10
|
+
#
|
11
|
+
# @api public
|
12
|
+
def attributes
|
13
|
+
h = {}
|
14
|
+
each_pair { |k,v| h[k] = v }
|
15
|
+
h
|
16
|
+
end
|
17
|
+
end # class Struct
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Symbol
|
2
|
+
|
3
|
+
def try_dup
|
4
|
+
self
|
5
|
+
end
|
6
|
+
|
7
|
+
##
|
8
|
+
# Join with _o_ as a file path
|
9
|
+
#
|
10
|
+
# :merb/"core_ext" #=> "merb/core_ext"
|
11
|
+
# :merb / :core_ext / :string #=> "merb/core_ext/string"
|
12
|
+
#
|
13
|
+
# @param [#to_s] o The path component(s) to append.
|
14
|
+
#
|
15
|
+
# @return [String] The receiver (as path string), concatenated with _o_.
|
16
|
+
#
|
17
|
+
# @api public
|
18
|
+
def /(o)
|
19
|
+
File.join(self.to_s, o.to_s)
|
20
|
+
end
|
21
|
+
end
|
data/lib/extlib/time.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'extlib/datetime'
|
3
|
+
|
4
|
+
class Time
|
5
|
+
|
6
|
+
##
|
7
|
+
# Convert to ISO 8601 representation
|
8
|
+
#
|
9
|
+
# Time.now.to_json #=> "\"2008-03-28T17:54:20-05:00\""
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
# ISO 8601 compatible representation of the Time object.
|
13
|
+
#
|
14
|
+
# @api public
|
15
|
+
def to_json(*)
|
16
|
+
self.xmlschema.to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Return receiver (for DateTime/Time conversion protocol).
|
21
|
+
#
|
22
|
+
# Time.now.to_time #=> Wed Nov 19 20:08:28 -0800 2008
|
23
|
+
#
|
24
|
+
# @return [Time] Receiver
|
25
|
+
#
|
26
|
+
# @api public
|
27
|
+
remove_method :to_time if instance_methods(false).any? { |m| m.to_sym == :to_time }
|
28
|
+
def to_time
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# Convert to DateTime (for DateTime/Time conversion protocol).
|
34
|
+
#
|
35
|
+
# Time.now.to_datetime #=> #<DateTime: 106046956823/43200,-1/3,2299161>
|
36
|
+
#
|
37
|
+
# @return [DateTime] DateTime object representing the same moment as receiver
|
38
|
+
#
|
39
|
+
# @api public
|
40
|
+
remove_method :to_datetime if instance_methods(false).any? { |m| m.to_sym == :to_datetime }
|
41
|
+
def to_datetime
|
42
|
+
DateTime.new(year, month, day, hour, min, sec, Rational(gmt_offset, 24 * 3600))
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Object
|
2
|
+
# Override this in a child if it cannot be dup'ed
|
3
|
+
#
|
4
|
+
# @return [Object]
|
5
|
+
def try_dup
|
6
|
+
self.dup
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class TrueClass
|
11
|
+
def try_dup
|
12
|
+
self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class FalseClass
|
17
|
+
def try_dup
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Module
|
23
|
+
def try_dup
|
24
|
+
self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class NilClass
|
29
|
+
def try_dup
|
30
|
+
self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Numeric
|
35
|
+
def try_dup
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Symbol
|
41
|
+
def try_dup
|
42
|
+
self
|
43
|
+
end
|
44
|
+
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'extlib/array'
|
3
|
+
|
4
|
+
describe Array do
|
5
|
+
before :all do
|
6
|
+
@array = [ [ :a, [ 1 ] ], [ :b, [ 2 ] ], [ :c, [ 3 ] ] ].freeze
|
7
|
+
end
|
8
|
+
|
9
|
+
it { @array.should respond_to(:to_hash) }
|
10
|
+
|
11
|
+
describe '#to_hash' do
|
12
|
+
before :all do
|
13
|
+
@return = @array.to_hash
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a Hash' do
|
17
|
+
@return.should be_kind_of(Hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return expected value' do
|
21
|
+
@return.should == { :a => [ 1 ], :b => [ 2 ], :c => [ 3 ] }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it { @array.should respond_to(:to_mash) }
|
26
|
+
|
27
|
+
describe '#to_mash' do
|
28
|
+
before :all do
|
29
|
+
@return = @array.to_mash
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return a Mash' do
|
33
|
+
@return.should be_kind_of(Mash)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should return expected value' do
|
37
|
+
@return.should == { 'a' => [ 1 ], 'b' => [ 2 ], 'c' => [ 3 ] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/blank_spec.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'extlib/blank'
|
3
|
+
|
4
|
+
describe Object do
|
5
|
+
it 'should provide blank?' do
|
6
|
+
Object.new.should respond_to(:blank?)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should be blank if it is nil' do
|
10
|
+
object = Object.new
|
11
|
+
class << object
|
12
|
+
def nil?; true end
|
13
|
+
end
|
14
|
+
object.should be_blank
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should be blank if it is empty' do
|
18
|
+
{}.should be_blank
|
19
|
+
[].should be_blank
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should not be blank if not nil or empty' do
|
23
|
+
Object.new.should_not be_blank
|
24
|
+
[nil].should_not be_blank
|
25
|
+
{ nil => 0 }.should_not be_blank
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Numeric do
|
30
|
+
it 'should provide blank?' do
|
31
|
+
1.should respond_to(:blank?)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should never be blank' do
|
35
|
+
1.should_not be_blank
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe NilClass do
|
40
|
+
it 'should provide blank?' do
|
41
|
+
nil.should respond_to(:blank?)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should always be blank' do
|
45
|
+
nil.should be_blank
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe TrueClass do
|
50
|
+
it 'should provide blank?' do
|
51
|
+
true.should respond_to(:blank?)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should never be blank' do
|
55
|
+
true.should_not be_blank
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe FalseClass do
|
60
|
+
it 'should provide blank?' do
|
61
|
+
false.should respond_to(:blank?)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should always be blank' do
|
65
|
+
false.should be_blank
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe String do
|
70
|
+
it 'should provide blank?' do
|
71
|
+
'string'.should respond_to(:blank?)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should be blank if empty' do
|
75
|
+
''.should be_blank
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should be blank if it only contains whitespace' do
|
79
|
+
' '.should be_blank
|
80
|
+
" \r \n \t ".should be_blank
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should not be blank if it contains non-whitespace' do
|
84
|
+
' a '.should_not be_blank
|
85
|
+
end
|
86
|
+
end
|
data/spec/class_spec.rb
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'extlib/class'
|
3
|
+
|
4
|
+
class Grandparent
|
5
|
+
end
|
6
|
+
|
7
|
+
class Parent < Grandparent
|
8
|
+
end
|
9
|
+
|
10
|
+
class Child < Parent
|
11
|
+
end
|
12
|
+
|
13
|
+
class Parent
|
14
|
+
end
|
15
|
+
|
16
|
+
class Grandparent
|
17
|
+
class_inheritable_accessor :last_name, :_attribute
|
18
|
+
|
19
|
+
self._attribute = 1900
|
20
|
+
end
|
21
|
+
|
22
|
+
class ClassWithInheritableSymbolAccessor
|
23
|
+
class_inheritable_accessor :symbol
|
24
|
+
self.symbol = :foo
|
25
|
+
end
|
26
|
+
|
27
|
+
class ClassInheritingSymbolAccessor < ClassWithInheritableSymbolAccessor
|
28
|
+
end
|
29
|
+
|
30
|
+
describe Class, "#inheritable_accessor" do
|
31
|
+
|
32
|
+
after :each do
|
33
|
+
Grandparent.send(:remove_instance_variable, "@last_name") rescue nil
|
34
|
+
Parent.send(:remove_instance_variable, "@last_name") rescue nil
|
35
|
+
Child.send(:remove_instance_variable, "@last_name") rescue nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'inherits from parent unless overriden' do
|
39
|
+
Parent._attribute.should == 1900
|
40
|
+
Child._attribute.should == 1900
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'inherits from grandparent unless overriden' do
|
44
|
+
Child._attribute.should == 1900
|
45
|
+
end
|
46
|
+
|
47
|
+
it "inherits even if the accessor is made after the inheritance" do
|
48
|
+
Grandparent.last_name = "Merb"
|
49
|
+
Parent.last_name.should == "Merb"
|
50
|
+
Child.last_name.should == "Merb"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "supports ||= to change a child" do
|
54
|
+
Parent.last_name ||= "Merb"
|
55
|
+
Grandparent.last_name.should == nil
|
56
|
+
Parent.last_name.should == "Merb"
|
57
|
+
Child.last_name.should == "Merb"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "supports << to change a child when the parent is an Array" do
|
61
|
+
Grandparent.last_name = ["Merb"]
|
62
|
+
Parent.last_name << "Core"
|
63
|
+
Grandparent.last_name.should == ["Merb"]
|
64
|
+
Parent.last_name.should == ["Merb", "Core"]
|
65
|
+
end
|
66
|
+
|
67
|
+
it "supports ! methods on an Array" do
|
68
|
+
Grandparent.last_name = %w(Merb Core)
|
69
|
+
Parent.last_name.reverse!
|
70
|
+
Grandparent.last_name.should == %w(Merb Core)
|
71
|
+
Parent.last_name.should == %w(Core Merb)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "support modifying a parent Hash" do
|
75
|
+
Grandparent.last_name = {"Merb" => "name"}
|
76
|
+
Parent.last_name["Core"] = "name"
|
77
|
+
Parent.last_name.should == {"Merb" => "name", "Core" => "name"}
|
78
|
+
Grandparent.last_name.should == {"Merb" => "name"}
|
79
|
+
end
|
80
|
+
|
81
|
+
it "supports hard-merging a parent Hash" do
|
82
|
+
Grandparent.last_name = {"Merb" => "name"}
|
83
|
+
Parent.last_name.merge!("Core" => "name")
|
84
|
+
Parent.last_name.should == {"Merb" => "name", "Core" => "name"}
|
85
|
+
Grandparent.last_name.should == {"Merb" => "name"}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "supports changes to the parent even if the child has already been read" do
|
89
|
+
Child.last_name
|
90
|
+
Grandparent.last_name = "Merb"
|
91
|
+
Child.last_name.should == "Merb"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "handles nil being set midstream" do
|
95
|
+
Child.last_name
|
96
|
+
Parent.last_name = nil
|
97
|
+
Grandparent.last_name = "Merb"
|
98
|
+
Child.last_name.should == nil
|
99
|
+
end
|
100
|
+
|
101
|
+
it "handles false being used in Parent" do
|
102
|
+
Child.last_name
|
103
|
+
Parent.last_name = false
|
104
|
+
Grandparent.last_name = "Merb"
|
105
|
+
Child.last_name.should == false
|
106
|
+
end
|
107
|
+
|
108
|
+
it "handles the grandparent changing the value (as long as the child isn't read first)" do
|
109
|
+
Grandparent.last_name = "Merb"
|
110
|
+
Grandparent.last_name = "Core"
|
111
|
+
Child.last_name.should == "Core"
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
describe Class, "#inheritable_accessor (of type Symbol)" do
|
117
|
+
|
118
|
+
it "should not raise" do
|
119
|
+
lambda { ClassInheritingSymbolAccessor.symbol }.should_not raise_error(TypeError)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
#
|
125
|
+
# The bug that prompted this estoric spec was found in
|
126
|
+
# the wild when using dm-is-versioned with c_i_w.
|
127
|
+
#
|
128
|
+
|
129
|
+
module Plugin
|
130
|
+
def self.included(base)
|
131
|
+
base.class_eval do
|
132
|
+
class_inheritable_writer :plugin_options
|
133
|
+
class_inheritable_reader :plugin_options
|
134
|
+
self.plugin_options = :foo
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Model
|
140
|
+
def self.new
|
141
|
+
model = Class.new
|
142
|
+
model.send(:include, Plugin)
|
143
|
+
model
|
144
|
+
end
|
145
|
+
|
146
|
+
include Plugin
|
147
|
+
self.const_set("Version", Model.new)
|
148
|
+
end
|
149
|
+
|
150
|
+
describe Class, "#inheritable_accessor" do
|
151
|
+
it "uses object_id for comparison" do
|
152
|
+
Model.methods.map { |m| m.to_sym }.should be_include(:plugin_options)
|
153
|
+
Model.plugin_options.should == :foo
|
154
|
+
|
155
|
+
Model::Version.methods.map { |m| m.to_sym }.should be_include(:plugin_options)
|
156
|
+
Model::Version.plugin_options.should == :foo
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'extlib/datetime'
|
3
|
+
|
4
|
+
describe DateTime, "#to_time" do
|
5
|
+
before do
|
6
|
+
@expected = Time.now.to_s
|
7
|
+
@datetime = DateTime.parse(@expected)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return a copy of time" do
|
11
|
+
time = @datetime.to_time
|
12
|
+
time.class.should == Time
|
13
|
+
time.to_s.should == @expected
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe Time, "#to_datetime" do
|
18
|
+
it "should return a copy of its self" do
|
19
|
+
datetime = DateTime.now
|
20
|
+
datetime.to_datetime.should == datetime
|
21
|
+
end
|
22
|
+
end
|