gci-class-extensions 1.0.0
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/README +0 -0
- data/lib/gci-class-extensions.rb +30 -0
- data/spec/gci-class-extensions_spec.rb +81 -0
- metadata +75 -0
data/README
ADDED
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class Fixnum
|
|
2
|
+
# adding to_date on the FixNum class so we don't have to do iseries date.to_s.to_date.to_s(:xx) to format an iseries date...
|
|
3
|
+
def to_date
|
|
4
|
+
self.to_s.valid_date? ? self.to_s.to_date : nil
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class NilClass
|
|
9
|
+
# overriding the default nil.to_s to just ignore the parameter in case we think it's a date (like created_at.to_s(:mdy))
|
|
10
|
+
def to_s(param=nil)
|
|
11
|
+
""
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Object
|
|
16
|
+
def not_nil?
|
|
17
|
+
!nil?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def not_blank?
|
|
21
|
+
!blank?
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class Array
|
|
26
|
+
# Since Rails' default sum goes crazy when there are nils, we do a more awesome job and clear the nils out
|
|
27
|
+
def awesum
|
|
28
|
+
compact.sum
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/spec_helper")
|
|
2
|
+
|
|
3
|
+
describe NilClass do
|
|
4
|
+
it "should accept to_s without a parameter, matching default behavior" do
|
|
5
|
+
nil.to_s
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should accept to_s with a parameter, ignoring it, to match to_s in Date and DateTime" do
|
|
9
|
+
nil.to_s(:db).should == nil.to_s
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Apparently fiddling with the expectations of :nil? sends mock objects for a tizzy; trust me that this method works.
|
|
14
|
+
# describe Object, "knowing whether it is not nil" do
|
|
15
|
+
# before(:each) do
|
|
16
|
+
# @obj = mock("Object")
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# it "should be not_nil if nil? is false" do
|
|
20
|
+
# @obj.should_receive(:nil?).and_return(false)
|
|
21
|
+
# @obj.not_nil?.should be_false
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# it "should not be not_nil if nil? is true" do
|
|
25
|
+
# @obj.should_receive(:nil?).and_return(true)
|
|
26
|
+
# @obj.not_nil?.should be_true
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
describe Object, "knowing whether it is not blank" do
|
|
32
|
+
before(:each) do
|
|
33
|
+
@obj = mock("Object")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "should be not_blank if blank? is false" do
|
|
37
|
+
@obj.should_receive(:blank?).and_return(false)
|
|
38
|
+
@obj.not_blank?.should be_true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should not be not_blank if blank? is true" do
|
|
42
|
+
@obj.should_receive(:blank?).and_return(true)
|
|
43
|
+
@obj.not_blank?.should be_false
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe Array, "performing a more awesome sum" do
|
|
48
|
+
it "should perform identically to Array#sum for normal numeric arrays" do
|
|
49
|
+
array = [1, 2, 3.45]
|
|
50
|
+
array.awesum.should == array.sum
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should perform identically to Array#sum for normal string arrays" do
|
|
54
|
+
array = %w(I am awesome)
|
|
55
|
+
array.awesum.should == array.sum
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should correctly sum up numbers, ignoring any nils in the array" do
|
|
59
|
+
array = [1, 2, nil, 3.45]
|
|
60
|
+
array.awesum.should == 6.45
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "should correctly sum up the strings, ignoring any nils in the array" do
|
|
64
|
+
array = ["I", " am", nil, " awesum"]
|
|
65
|
+
array.awesum.should == "I am awesum"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe Fixnum, "converting itself to a date" do
|
|
70
|
+
# def to_date
|
|
71
|
+
# self.to_s.valid_date? ? self.to_s.to_date : nil
|
|
72
|
+
# end
|
|
73
|
+
it "should convert itself to a date" do
|
|
74
|
+
20091020.to_date.should == Date.new(2009, 10, 20)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "should return nil if doesn't contain a valid date" do
|
|
78
|
+
1111.to_date.should be_nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: gci-class-extensions
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Patrick Byrne
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2010-01-04 00:00:00 -06:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: activesupport
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: "0"
|
|
24
|
+
version:
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: valid-date
|
|
27
|
+
type: :runtime
|
|
28
|
+
version_requirement:
|
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: "0"
|
|
34
|
+
version:
|
|
35
|
+
description:
|
|
36
|
+
email: code@patrickbyrne.net
|
|
37
|
+
executables: []
|
|
38
|
+
|
|
39
|
+
extensions: []
|
|
40
|
+
|
|
41
|
+
extra_rdoc_files:
|
|
42
|
+
- README
|
|
43
|
+
files:
|
|
44
|
+
- lib/gci-class-extensions.rb
|
|
45
|
+
- README
|
|
46
|
+
has_rdoc: true
|
|
47
|
+
homepage: http://bitbucket.org/pbyrne/common-ruby-class-extensions/
|
|
48
|
+
licenses: []
|
|
49
|
+
|
|
50
|
+
post_install_message:
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
version:
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: "0"
|
|
66
|
+
version:
|
|
67
|
+
requirements: []
|
|
68
|
+
|
|
69
|
+
rubyforge_project:
|
|
70
|
+
rubygems_version: 1.3.5
|
|
71
|
+
signing_key:
|
|
72
|
+
specification_version: 3
|
|
73
|
+
summary: A gem used by our internal team for common class extensions, like not_blank? and not_nil? counterparts to blank? and nil?.
|
|
74
|
+
test_files:
|
|
75
|
+
- spec/gci-class-extensions_spec.rb
|