gorillib 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/.gitignore +49 -0
  2. data/.rspec +2 -0
  3. data/CHANGELOG.textile +49 -0
  4. data/Gemfile +15 -0
  5. data/Gemfile.lock +36 -0
  6. data/README.textile +6 -1
  7. data/Rakefile +42 -9
  8. data/VERSION +1 -1
  9. data/gorillib.gemspec +110 -73
  10. data/lib/gorillib/array/deep_compact.rb +3 -12
  11. data/lib/gorillib/datetime/#flat.rb# +30 -0
  12. data/lib/gorillib/datetime/flat.rb +2 -10
  13. data/lib/gorillib/datetime/parse.rb +3 -1
  14. data/lib/gorillib/enumerable/sum.rb +37 -35
  15. data/lib/gorillib/hash/deep_compact.rb +3 -13
  16. data/lib/gorillib/logger/log.rb +1 -1
  17. data/lib/gorillib/numeric/clamp.rb +9 -0
  18. data/lib/gorillib/object/try.rb +10 -0
  19. data/lib/gorillib/object/try_dup.rb +44 -0
  20. data/lib/gorillib/string/human.rb +1 -3
  21. data/lib/gorillib/string/truncate.rb +2 -2
  22. data/spec/array/compact_blank_spec.rb +37 -0
  23. data/spec/array/extract_options_spec.rb +47 -0
  24. data/spec/datetime/flat_spec.rb +38 -0
  25. data/spec/datetime/parse_spec.rb +48 -0
  26. data/spec/enumerable/sum_spec.rb +56 -0
  27. data/spec/hash/compact_spec.rb +70 -0
  28. data/spec/hash/deep_compact_spec.rb +37 -0
  29. data/spec/hash/deep_merge_spec.rb +117 -0
  30. data/spec/hash/keys_spec.rb +111 -0
  31. data/spec/hash/reverse_merge_spec.rb +25 -0
  32. data/spec/hash/slice_spec.rb +43 -0
  33. data/spec/hash/zip_spec.rb +31 -0
  34. data/spec/logger/log_spec.rb +31 -0
  35. data/spec/metaprogramming/aliasing_spec.rb +177 -0
  36. data/spec/metaprogramming/cattr_accessor_spec.rb +41 -0
  37. data/spec/metaprogramming/class_attribute_spec.rb +75 -0
  38. data/spec/metaprogramming/delegation_spec.rb +166 -0
  39. data/spec/metaprogramming/mattr_accessor_spec.rb +44 -0
  40. data/spec/metaprogramming/singleton_class_spec.rb +9 -0
  41. data/spec/{numeric_spec.rb → numeric/clamp_spec.rb} +2 -2
  42. data/spec/object/blank_spec.rb +93 -0
  43. data/spec/object/try_dup_spec.rb +47 -0
  44. data/spec/object/try_spec.rb +20 -0
  45. data/spec/spec_helper.rb +13 -8
  46. data/spec/string/constantize_spec.rb +56 -0
  47. data/spec/string/human_spec.rb +66 -0
  48. data/spec/string/inflections_spec.rb +74 -0
  49. data/{test → spec}/string/inflector_test_cases.rb +0 -0
  50. data/spec/string/truncate_spec.rb +42 -0
  51. data/spec/support/kcode_test_helper.rb +16 -0
  52. metadata +209 -96
  53. data/.document +0 -5
  54. data/fiddle/hubahuba.rb +0 -62
  55. data/spec/blank_spec.rb +0 -86
  56. data/spec/deep_compact_spec.rb +0 -36
  57. data/spec/gorillib_spec.rb +0 -7
  58. data/spec/rcov.opts +0 -6
  59. data/spec/spec.opts +0 -4
  60. data/spec/spec_tasks.rake +0 -15
  61. data/test/abstract_unit.rb +0 -25
  62. data/test/array/compact_blank_test.rb +0 -33
  63. data/test/array/extract_options_test.rb +0 -39
  64. data/test/datetime/flat_test.rb +0 -0
  65. data/test/datetime/parse_test.rb +0 -0
  66. data/test/enumerable/sum_test.rb +0 -50
  67. data/test/hash/compact_test.rb +0 -38
  68. data/test/hash/deep_merge_test.rb +0 -30
  69. data/test/hash/keys_test.rb +0 -110
  70. data/test/hash/reverse_merge_test.rb +0 -20
  71. data/test/hash/slice_test.rb +0 -47
  72. data/test/hash/zip_test.rb +0 -0
  73. data/test/logger/log_test.rb +0 -0
  74. data/test/metaprogramming/aliasing_test.rb +0 -188
  75. data/test/metaprogramming/cattr_accessor_test.rb +0 -38
  76. data/test/metaprogramming/class_attribute_test.rb +0 -73
  77. data/test/metaprogramming/delegation_test.rb +0 -166
  78. data/test/metaprogramming/mattr_accessor_test.rb +0 -40
  79. data/test/metaprogramming/singleton_class_test.rb +0 -9
  80. data/test/object/blank_test.rb +0 -22
  81. data/test/string/constantize_test.rb +0 -30
  82. data/test/string/human_test.rb +0 -65
  83. data/test/string/inflections_test.rb +0 -57
  84. data/test/string/truncate_test.rb +0 -37
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/metaprogramming/mattr_accessor'
3
+
4
+ describe Module do
5
+ describe 'mattr_accessor' do
6
+ before do
7
+ m = @module = Module.new do
8
+ mattr_accessor :foo
9
+ mattr_accessor :bar, :instance_writer => false
10
+ mattr_reader :shaq, :instance_reader => false
11
+ end
12
+ @class = Class.new
13
+ @class.instance_eval { include m }
14
+ @object = @class.new
15
+ end
16
+
17
+ it 'uses mattr default' do
18
+ @module.foo.should be_nil
19
+ @object.foo.should be_nil
20
+ end
21
+
22
+ it 'sets mattr value' do
23
+ @module.foo = :test
24
+ @object.foo.should == :test
25
+
26
+ @object.foo = :test2
27
+ @module.foo.should == :test2
28
+ end
29
+
30
+ it 'with :instance_writer => false, does not create instance writer' do
31
+ @module.should respond_to(:foo)
32
+ @module.should respond_to(:foo=)
33
+ @object.should respond_to(:bar)
34
+ @object.should_not respond_to(:bar=)
35
+ end
36
+
37
+ it 'with :instance_writer => false, does not create instance reader' do
38
+ @module.should respond_to(:shaq)
39
+ @object.should_not respond_to(:shaq)
40
+ @object.should_not respond_to(:shaq=)
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/metaprogramming/singleton_class'
3
+
4
+ describe 'Singleton Class' do
5
+ it 'returns the singleton class' do
6
+ o = Object.new
7
+ class << o; self end.should == o.singleton_class
8
+ end
9
+ end
@@ -1,8 +1,8 @@
1
- require 'spec_helper'
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
2
  require 'gorillib/numeric/clamp'
3
3
 
4
4
  describe Numeric do
5
- describe 'clamp' do
5
+ describe '#clamp' do
6
6
  it 'should return self if neither min nor max are given' do
7
7
  5.clamp().should == 5
8
8
  end
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/object/blank'
3
+
4
+ describe 'object/blank' do
5
+ describe Object do
6
+ it 'should provide blank?' do
7
+ Object.new.should respond_to(:blank?)
8
+ end
9
+
10
+ it 'should be blank if it is nil' do
11
+ object = Object.new
12
+ class << object
13
+ def nil?; true end
14
+ end
15
+ object.should be_blank
16
+ end
17
+
18
+ it 'should be blank if it is empty' do
19
+ {}.should be_blank
20
+ [].should be_blank
21
+ end
22
+
23
+ it 'should not be blank if not nil or empty' do
24
+ Object.new.should_not be_blank
25
+ [nil].should_not be_blank
26
+ { nil => 0 }.should_not be_blank
27
+ end
28
+
29
+ it 'should be present if not blank' do
30
+ nil.should_not be_present
31
+ "blarg".should be_present
32
+ end
33
+ end
34
+
35
+ describe Numeric do
36
+ it 'should provide blank?' do
37
+ 1.should respond_to(:blank?)
38
+ end
39
+
40
+ it 'should never be blank' do
41
+ 1.should_not be_blank
42
+ end
43
+ end
44
+
45
+ describe NilClass do
46
+ it 'should provide blank?' do
47
+ nil.should respond_to(:blank?)
48
+ end
49
+
50
+ it 'should always be blank' do
51
+ nil.should be_blank
52
+ end
53
+ end
54
+
55
+ describe TrueClass do
56
+ it 'should provide blank?' do
57
+ true.should respond_to(:blank?)
58
+ end
59
+
60
+ it 'should never be blank' do
61
+ true.should_not be_blank
62
+ end
63
+ end
64
+
65
+ describe FalseClass do
66
+ it 'should provide blank?' do
67
+ false.should respond_to(:blank?)
68
+ end
69
+
70
+ it 'should always be blank' do
71
+ false.should be_blank
72
+ end
73
+ end
74
+
75
+ describe String do
76
+ it 'should provide blank?' do
77
+ 'string'.should respond_to(:blank?)
78
+ end
79
+
80
+ it 'should be blank if empty' do
81
+ ''.should be_blank
82
+ end
83
+
84
+ it 'should be blank if it only contains whitespace' do
85
+ ' '.should be_blank
86
+ " \r \n \t ".should be_blank
87
+ end
88
+
89
+ it 'should not be blank if it contains non-whitespace' do
90
+ ' a '.should_not be_blank
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/object/try_dup'
3
+
4
+ describe "try_dup" do
5
+ it "returns a duplicate version on regular objects" do
6
+ obj = Object.new
7
+ oth = obj.try_dup
8
+ obj.should_not === oth
9
+ end
10
+
11
+ it "returns self on Numerics" do
12
+ obj = 12
13
+ oth = obj.try_dup
14
+ obj.should === oth
15
+ end
16
+
17
+ it "returns self on Symbols" do
18
+ obj = :test
19
+ oth = obj.try_dup
20
+ obj.should === oth
21
+ end
22
+
23
+ it "returns self on true" do
24
+ obj = true
25
+ oth = obj.try_dup
26
+ obj.should === oth
27
+ end
28
+
29
+ it "returns self on false" do
30
+ obj = false
31
+ oth = obj.try_dup
32
+ obj.should === oth
33
+ end
34
+
35
+ it "returns self on nil" do
36
+ obj = nil
37
+ oth = obj.try_dup
38
+ obj.should === oth
39
+ end
40
+
41
+ it "returns self on modules" do
42
+ obj = Module.new
43
+ oth = obj.try_dup
44
+ obj.object_id.should == oth.object_id
45
+ end
46
+ end
47
+
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/object/try'
3
+
4
+ class Foo
5
+ def i_am_a_method_hooray
6
+ "i was called!"
7
+ end
8
+ end
9
+
10
+ describe Object do
11
+ describe '#try' do
12
+ it 'returns nil if item does not #respond_to? method' do
13
+ Foo.new.try(:i_am_not_a_method).should be_nil
14
+ end
15
+ it 'calls the method if the item does #respond_to? it' do
16
+ Foo.new.try(:i_am_a_method_hooray).should == "i was called!"
17
+ end
18
+ end
19
+ end
20
+
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,17 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
1
+ require 'rubygems' unless defined?(Gem)
2
+ require 'spork'
3
3
  require 'rspec'
4
- require 'gorillib'
5
4
 
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5
+ Spork.prefork do
6
+ # You'll need to restart for changes to configuration or code from libraries loaded here
9
7
 
10
- RSpec.configure do |config|
11
-
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+ end
13
+ end
14
+
15
+ Spork.each_run do
16
+ # This code will be run each time you run your specs.
12
17
  end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/string/constantize'
3
+
4
+ module Ace
5
+ module Base
6
+ class Case
7
+ def lookup_from_instance(klass_name)
8
+ klass_name.constantize
9
+ end
10
+
11
+ def self.lookup_from_class(klass_name)
12
+ klass_name.constantize
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module InflectorTest
19
+
20
+ describe 'String' do
21
+ describe '#constantize' do
22
+ it "works from within instance" do
23
+ @obj = Ace::Base::Case.new
24
+ @obj.lookup_from_instance('Ace::Base::Case').should == Ace::Base::Case
25
+ @obj.lookup_from_instance('Ace::Base').should == Ace::Base
26
+ @obj.lookup_from_instance('Ace').should == Ace
27
+ end
28
+
29
+ it "works from within class" do
30
+ @klass = Ace::Base::Case
31
+ @klass.lookup_from_class('Ace::Base::Case').should == Ace::Base::Case
32
+ @klass.lookup_from_class('Ace::Base').should == Ace::Base
33
+ @klass.lookup_from_class('Ace').should == Ace
34
+ end
35
+
36
+ it "does lookup from top down" do
37
+ @obj = Ace::Base::Case.new
38
+ lambda{ @obj.lookup_from_instance('Case') }.should raise_error(NameError)
39
+ lambda{ @obj.lookup_from_instance('Base') }.should raise_error(NameError)
40
+ end
41
+
42
+ it "InflectorTest" do InflectorTest.should == "InflectorTest".constantize ; end
43
+ it "::InflectorTest" do InflectorTest.should == "::InflectorTest".constantize ; end
44
+ it "Ace::Base::Case" do Ace::Base::Case.should == "Ace::Base::Case".constantize ; end
45
+ it "::Ace::Base::Case" do Ace::Base::Case.should == "::Ace::Base::Case".constantize ; end
46
+ it "UnknownClass" do lambda{ "UnknownClass" .constantize }.should raise_error(NameError) ; end
47
+ it "An invalid string" do lambda{ "An invalid string".constantize }.should raise_error(NameError) ; end
48
+ it "InvalidClass\n" do lambda{ "InvalidClass\n" .constantize }.should raise_error(NameError) ; end
49
+
50
+ it 'does lexical lookup' do
51
+ lambda{ "Ace::Base::InflectorTest".constantize }.should raise_error(NameError)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require File.dirname(__FILE__)+'/inflector_test_cases'
3
+ require 'gorillib/string/human'
4
+
5
+ include InflectorTestCases
6
+
7
+ describe String do
8
+
9
+ describe '#titleize' do
10
+ MixtureToTitleCase.each do |raw, titleized|
11
+ it raw do
12
+ raw.titleize.should == titleized
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#humanize' do
18
+ UnderscoreToHuman.each do |raw, humanized|
19
+ it raw do
20
+ raw.humanize.should == humanized
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#to_sentence' do
26
+
27
+ it 'converts plain array to sentence' do
28
+ [].to_sentence.should == ""
29
+ ['one'].to_sentence.should == "one"
30
+ ['one', 'two'].to_sentence.should == "one and two"
31
+ ['one', 'two', 'three'].to_sentence.should == "one, two, and three"
32
+ end
33
+
34
+ it 'converts sentences with a word connector' do
35
+ ['one', 'two', 'three'].to_sentence(:words_connector => ' ') .should == "one two, and three"
36
+ ['one', 'two', 'three'].to_sentence(:words_connector => ' & ') .should == "one & two, and three"
37
+ ['one', 'two', 'three'].to_sentence(:words_connector => nil) .should == "onetwo, and three"
38
+ end
39
+
40
+ it 'converts sentences with a last word connector' do
41
+ ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ') .should == "one, two, and also three"
42
+ ['one', 'two', 'three'].to_sentence(:last_word_connector => nil) .should == "one, twothree"
43
+ ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ') .should == "one, two three"
44
+ ['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ') .should == "one, two and three"
45
+ end
46
+
47
+ it 'converts two elements' do
48
+ ['one', 'two'].to_sentence.should == "one and two"
49
+ ['one', 'two'].to_sentence(:two_words_connector => ' ').should == "one two"
50
+ end
51
+
52
+ it 'converts one element' do
53
+ ['one'].to_sentence.should == "one"
54
+ end
55
+
56
+ it 'converting one element makes new object' do
57
+ elements = ["one"]
58
+ elements.to_sentence.object_id.should_not == elements[0].object_id
59
+ elements.to_sentence.should_not equal(elements[0])
60
+ end
61
+
62
+ it 'converts a non-string element' do
63
+ [1].to_sentence.should == '1'
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require File.dirname(__FILE__)+'/inflector_test_cases'
3
+ require 'gorillib/string/inflections'
4
+
5
+ include InflectorTestCases
6
+
7
+ describe String do
8
+ describe 'camelize' do
9
+ CamelToUnderscore.each do |cameled, underscored|
10
+ it underscored do
11
+ underscored.camelize.should == cameled
12
+ end
13
+ end
14
+
15
+ it 'with lower, downcases the first letter' do
16
+ 'Capital'.camelize(:lower).should == 'capital'
17
+ end
18
+
19
+ UnderscoreToLowerCamel.each do |underscored, lower_cameled|
20
+ it lower_cameled do
21
+ underscored.camelize(:lower).should == lower_cameled
22
+ end
23
+ end
24
+
25
+ CamelWithModuleToUnderscoreWithSlash.each do |cameled, underscored|
26
+ it underscored do
27
+ underscored.camelize.should == cameled
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '#snakeize' do
33
+ UnderscoreToLowerCamel.each do |underscored, snaked|
34
+ it underscored do
35
+ underscored.snakeize.should == snaked
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#underscore' do
41
+ CamelToUnderscore.each do |cameled, underscored|
42
+ it cameled do
43
+ cameled.underscore.should == underscored
44
+ end
45
+ end
46
+
47
+ CamelToUnderscoreWithoutReverse.each do |cameled, underscored|
48
+ it underscored do
49
+ cameled.underscore.should == underscored
50
+ end
51
+ end
52
+
53
+ CamelWithModuleToUnderscoreWithSlash.each do |cameled, underscored|
54
+ it underscored do
55
+ cameled.underscore.should == underscored
56
+ end
57
+ end
58
+
59
+ it "HTMLTidy" do
60
+ "HTMLTidy".underscore.should == "html_tidy"
61
+ end
62
+
63
+ it "HTMLTidyGenerator" do
64
+ "HTMLTidyGenerator".underscore.should == "html_tidy_generator"
65
+ end
66
+ end
67
+
68
+ describe '#demodulize' do
69
+ it 'strips module part' do
70
+ "MyApplication::Billing::Account".demodulize.should == "Account"
71
+ end
72
+ end
73
+
74
+ end
File without changes
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__)+'/../spec_helper'
2
+ require 'gorillib/string/truncate'
3
+
4
+ describe String do
5
+ describe '#truncate' do
6
+ it 'leaves a short string alone' do
7
+ "Hello World!".truncate(12).should == "Hello World!"
8
+ end
9
+ it 'truncates a long string' do
10
+ "Hello World!!".truncate(12).should == "Hello Wor..."
11
+ end
12
+
13
+ it 'truncates with omission and separator' do
14
+ "Hello World!".truncate(10, :omission => "[...]") .should == "Hello[...]"
15
+ "Hello Big World!".truncate(13, :omission => "[...]", :separator => ' ') .should == "Hello[...]"
16
+ "Hello Big World!".truncate(14, :omission => "[...]", :separator => ' ') .should == "Hello Big[...]"
17
+ "Hello Big World!".truncate(15, :omission => "[...]", :separator => ' ') .should == "Hello Big[...]"
18
+ end
19
+
20
+ if RUBY_VERSION < '1.9.0'
21
+ it 'works with unicode when kcode=none' do
22
+ Gorillib::KcodeTestHelper.with_kcode('none') do
23
+ "\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224".truncate(10).
24
+ should == "\354\225\210\353\205\225\355..."
25
+ end
26
+ end
27
+
28
+ # # FIXME: breaks on ruby 1.8
29
+ # it 'works with unicode when kcode=u' do
30
+ # Gorillib::KcodeTestHelper.with_kcode('u') do
31
+ # "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".truncate(10).
32
+ # should == "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ..."
33
+ # end
34
+ # end
35
+ else # ruby 1.9
36
+ it 'works with unicode' do
37
+ "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8').truncate(10).
38
+ should == "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8')
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ module Gorillib
2
+ module KcodeTestHelper
3
+ def self.with_kcode(code)
4
+ if RUBY_VERSION < '1.9'
5
+ begin
6
+ old_kcode, $KCODE = $KCODE, code
7
+ yield
8
+ ensure
9
+ $KCODE = old_kcode
10
+ end
11
+ else
12
+ yield
13
+ end
14
+ end
15
+ end
16
+ end