motion-support 0.1.0 → 0.2.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/.gitignore +3 -0
- data/Gemfile +1 -1
- data/README.md +231 -2
- data/Rakefile +3 -3
- data/app/app_delegate.rb +0 -2
- data/examples/Inflector/.gitignore +16 -0
- data/examples/Inflector/Gemfile +5 -0
- data/examples/Inflector/Rakefile +13 -0
- data/examples/Inflector/app/app_delegate.rb +8 -0
- data/examples/Inflector/app/inflections.rb +5 -0
- data/examples/Inflector/app/inflector_view_controller.rb +120 -0
- data/examples/Inflector/app/words_view_controller.rb +101 -0
- data/examples/Inflector/resources/Default-568h@2x.png +0 -0
- data/examples/Inflector/spec/main_spec.rb +9 -0
- data/lib/motion-support/core_ext/array.rb +13 -0
- data/lib/motion-support/core_ext/class.rb +8 -0
- data/lib/motion-support/core_ext/hash.rb +16 -0
- data/lib/motion-support/core_ext/integer.rb +9 -0
- data/lib/motion-support/core_ext/module.rb +14 -0
- data/lib/motion-support/core_ext/numeric.rb +8 -0
- data/lib/motion-support/core_ext/object.rb +14 -0
- data/lib/motion-support/core_ext/range.rb +8 -0
- data/lib/motion-support/core_ext/string.rb +14 -0
- data/lib/motion-support/core_ext/time.rb +19 -0
- data/lib/motion-support/core_ext.rb +3 -0
- data/lib/motion-support/inflector.rb +12 -156
- data/lib/motion-support.rb +5 -5
- data/motion/_stdlib/cgi.rb +22 -0
- data/motion/_stdlib/date.rb +77 -0
- data/motion/_stdlib/time.rb +19 -0
- data/motion/core_ext/array/access.rb +28 -0
- data/motion/core_ext/array/conversions.rb +86 -0
- data/motion/core_ext/array/extract_options.rb +11 -0
- data/motion/core_ext/array/grouping.rb +99 -0
- data/motion/core_ext/array/prepend_and_append.rb +7 -0
- data/motion/core_ext/array/wrap.rb +45 -0
- data/{lib/motion-support → motion/core_ext}/array.rb +0 -4
- data/motion/core_ext/class/attribute.rb +119 -0
- data/motion/core_ext/class/attribute_accessors.rb +168 -0
- data/motion/core_ext/date/acts_like.rb +8 -0
- data/motion/core_ext/date/calculations.rb +117 -0
- data/motion/core_ext/date/conversions.rb +56 -0
- data/motion/core_ext/date_and_time/calculations.rb +232 -0
- data/motion/core_ext/enumerable.rb +90 -0
- data/motion/core_ext/hash/deep_merge.rb +27 -0
- data/motion/core_ext/hash/except.rb +15 -0
- data/motion/core_ext/hash/indifferent_access.rb +19 -0
- data/motion/core_ext/hash/keys.rb +138 -0
- data/motion/core_ext/hash/reverse_merge.rb +22 -0
- data/motion/core_ext/hash/slice.rb +40 -0
- data/motion/core_ext/integer/inflections.rb +27 -0
- data/motion/core_ext/integer/multiple.rb +10 -0
- data/motion/core_ext/integer/time.rb +41 -0
- data/{lib/motion-support → motion/core_ext}/metaclass.rb +0 -0
- data/motion/core_ext/module/aliasing.rb +69 -0
- data/motion/core_ext/module/anonymous.rb +19 -0
- data/motion/core_ext/module/attr_internal.rb +38 -0
- data/motion/core_ext/module/attribute_accessors.rb +64 -0
- data/motion/core_ext/module/delegation.rb +175 -0
- data/motion/core_ext/module/introspection.rb +60 -0
- data/motion/core_ext/module/reachable.rb +5 -0
- data/motion/core_ext/module/remove_method.rb +12 -0
- data/motion/core_ext/ns_dictionary.rb +11 -0
- data/motion/core_ext/numeric/bytes.rb +44 -0
- data/motion/core_ext/numeric/conversions.rb +7 -0
- data/motion/core_ext/numeric/time.rb +75 -0
- data/motion/core_ext/object/acts_like.rb +10 -0
- data/motion/core_ext/object/blank.rb +105 -0
- data/motion/core_ext/object/deep_dup.rb +44 -0
- data/motion/core_ext/object/duplicable.rb +83 -0
- data/motion/core_ext/object/instance_variables.rb +28 -0
- data/motion/core_ext/object/to_param.rb +58 -0
- data/motion/core_ext/object/to_query.rb +26 -0
- data/motion/core_ext/object/try.rb +78 -0
- data/motion/core_ext/range/include_range.rb +23 -0
- data/motion/core_ext/range/overlaps.rb +8 -0
- data/motion/core_ext/regexp.rb +5 -0
- data/motion/core_ext/string/access.rb +104 -0
- data/motion/core_ext/string/behavior.rb +6 -0
- data/motion/core_ext/string/exclude.rb +11 -0
- data/motion/core_ext/string/filters.rb +55 -0
- data/motion/core_ext/string/indent.rb +43 -0
- data/motion/core_ext/string/inflections.rb +195 -0
- data/motion/core_ext/string/starts_ends_with.rb +4 -0
- data/motion/core_ext/string/strip.rb +24 -0
- data/motion/core_ext/time/acts_like.rb +8 -0
- data/motion/core_ext/time/calculations.rb +215 -0
- data/motion/core_ext/time/conversions.rb +52 -0
- data/motion/duration.rb +104 -0
- data/motion/hash_with_indifferent_access.rb +251 -0
- data/motion/inflections.rb +67 -0
- data/motion/inflector/inflections.rb +203 -0
- data/motion/inflector/methods.rb +321 -0
- data/{lib/motion-support → motion}/logger.rb +0 -0
- data/{lib/motion-support → motion}/version.rb +1 -1
- data/motion-support.gemspec +2 -2
- data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
- data/spec/motion-support/_helpers/inflector_test_cases.rb +313 -0
- data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
- data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
- data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
- data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
- data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
- data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
- data/spec/motion-support/{array_spec.rb → core_ext/array_spec.rb} +0 -5
- data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
- data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
- data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
- data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
- data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
- data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
- data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
- data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
- data/spec/motion-support/core_ext/hash/key_spec.rb +230 -0
- data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
- data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
- data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
- data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
- data/spec/motion-support/{metaclass_spec.rb → core_ext/metaclass_spec.rb} +0 -0
- data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
- data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
- data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
- data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
- data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
- data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
- data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
- data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
- data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
- data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
- data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
- data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
- data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
- data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
- data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
- data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
- data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
- data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
- data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
- data/spec/motion-support/core_ext/string/filter_spec.rb +48 -0
- data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
- data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
- data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
- data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
- data/spec/motion-support/core_ext/string_spec.rb +88 -0
- data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
- data/spec/motion-support/core_ext/time/conversion_spec.rb +54 -0
- data/spec/motion-support/duration_spec.rb +107 -0
- data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
- data/spec/motion-support/inflector_spec.rb +474 -35
- data/spec/motion-support/ns_dictionary_spec.rb +29 -0
- metadata +212 -35
- data/lib/motion-support/cattr_accessor.rb +0 -19
- data/lib/motion-support/class_inheritable_accessor.rb +0 -23
- data/lib/motion-support/class_inheritable_array.rb +0 -29
- data/lib/motion-support/hash.rb +0 -31
- data/lib/motion-support/nilclass.rb +0 -5
- data/lib/motion-support/object.rb +0 -17
- data/lib/motion-support/string.rb +0 -71
- data/spec/motion-support/cattr_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_array_spec.rb +0 -61
- data/spec/motion-support/hash_spec.rb +0 -31
- data/spec/motion-support/nilclass_spec.rb +0 -5
- data/spec/motion-support/object_spec.rb +0 -43
- data/spec/motion-support/string_spec.rb +0 -145
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
describe "enumerable" do
|
|
2
|
+
describe "collect_with_index" do
|
|
3
|
+
describe "on Array" do
|
|
4
|
+
it "should return an empty array on an empty array" do
|
|
5
|
+
[].collect_with_index { |x, i| x }.should == []
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should yield an index for each element" do
|
|
9
|
+
[1, 2, 3].collect_with_index { |x, i| [i, x] }.should == [[0, 1], [1, 2], [2, 3]]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "should apply the block to each element" do
|
|
13
|
+
[1, 2, 3].collect_with_index { |x, i| x * i }.should == [0, 2, 6]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "on Hash" do
|
|
18
|
+
it "should return an empty array on an empty hash" do
|
|
19
|
+
{}.collect_with_index { |x, i| x }.should == []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "should apply the block to each element" do
|
|
23
|
+
result = {:foo => 1, :bar => 2, :baz => 3}.collect_with_index { |p, i| [p.first, p.last, i] }
|
|
24
|
+
result.should == [[:foo, 1, 0], [:bar, 2, 1], [:baz, 3, 2]]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe "sum" do
|
|
30
|
+
it "should return the sum of an integer array" do
|
|
31
|
+
[5, 15, 10].sum.should == 30
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should concatenate strings in a string array" do
|
|
35
|
+
['foo', 'bar'].sum.should == "foobar"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "should concatenate arrays" do
|
|
39
|
+
[[1, 2], [3, 1, 5]].sum.should == [1, 2, 3, 1, 5]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "should yield a block to each element before summing if given" do
|
|
43
|
+
[1, 2, 3].sum { |p| p * 2 }.should == 12
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should allow a custom result for an empty list" do
|
|
47
|
+
[].sum("empty") { |i| i.amount }.should == "empty"
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe "index_by" do
|
|
52
|
+
class Person < Struct.new(:first_name, :last_name)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
before do
|
|
56
|
+
@peter = Person.new('Peter', 'Griffin')
|
|
57
|
+
@homer = Person.new('Homer', 'Simpson')
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "should create hash with keys given by block result" do
|
|
61
|
+
[@peter, @homer].index_by { |x| x.first_name }.should == { 'Peter' => @peter, 'Homer' => @homer }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "should create enumerator if no block is given" do
|
|
65
|
+
[@peter, @homer].index_by.should.is_a Enumerator
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe "many?" do
|
|
70
|
+
it "should return false for an empty enumerable" do
|
|
71
|
+
[].many?.should == false
|
|
72
|
+
{}.many?.should == false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "should return false for an enumerable with only one element" do
|
|
76
|
+
[1].many?.should == false
|
|
77
|
+
{:foo => :bar}.many?.should == false
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "should return true for an enumerable with more than one element" do
|
|
81
|
+
[1, 2].many?.should == true
|
|
82
|
+
[1, 2, 3].many?.should == true
|
|
83
|
+
{:foo => :bar, :baz => :bingo}.many?.should == true
|
|
84
|
+
{:a => :b, :c => :d, :e => :f}.many?.should == true
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "should return false if there are less than two elements satisfying the block if given" do
|
|
88
|
+
[1].many? { |x| x * 10 > 100 }.should == false
|
|
89
|
+
[1, 2, 3, 4, 5].many? { |x| x >= 5 }.should == false
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it "should return true if there is more than one element satisfying the block if given" do
|
|
93
|
+
[1, 2, 3, 4, 5].many? { |x| x < 4 }.should == true
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
describe "exclude?" do
|
|
98
|
+
it "should return true if the element is not in the array" do
|
|
99
|
+
[1, 2, 3].exclude?(4).should == true
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "should return false if the element is in the array" do
|
|
103
|
+
[1, 2, 3].exclude?(2).should == false
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it "should always return true for an empty array" do
|
|
107
|
+
[].exclude?(rand).should == true
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
describe "Range" do
|
|
113
|
+
describe "sum" do
|
|
114
|
+
it "should return the sum of integers in an inclusive range" do
|
|
115
|
+
(3..5).sum.should == 12
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it "should return the sum of integers in an exclusive range" do
|
|
119
|
+
(3...5).sum.should == 7
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "should return zero for an empty range" do
|
|
123
|
+
(1...1).sum.should == 0
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it "should return custom identity for an empty range if given" do
|
|
127
|
+
(1...1).sum("empty").should == "empty"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
describe "hash" do
|
|
2
|
+
before do
|
|
3
|
+
@hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
|
|
4
|
+
@hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
describe "deep_merge" do
|
|
8
|
+
it "should deep merge" do
|
|
9
|
+
expected = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
|
10
|
+
@hash_1.deep_merge(@hash_2).should == expected
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should deep merge with block" do
|
|
14
|
+
expected = { :a => [:a, "a", 1], :b => "b", :c => { :c1 => [:c1, "c1", 2], :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
|
15
|
+
(@hash_1.deep_merge(@hash_2) { |k,o,n| [k, o, n] }).should == expected
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe "deep_merge!" do
|
|
20
|
+
it "should deep merge" do
|
|
21
|
+
expected = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
|
22
|
+
@hash_1.deep_merge!(@hash_2)
|
|
23
|
+
@hash_1.should == expected
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should deep merge with block" do
|
|
27
|
+
expected = { :a => [:a, "a", 1], :b => "b", :c => { :c1 => [:c1, "c1", 2], :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
|
28
|
+
@hash_1.deep_merge!(@hash_2) { |k,o,n| [k, o, n] }
|
|
29
|
+
@hash_1.should == expected
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
describe "hash" do
|
|
2
|
+
describe "except" do
|
|
3
|
+
it "should remove key" do
|
|
4
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
5
|
+
expected = { :a => 'x', :b => 'y' }
|
|
6
|
+
|
|
7
|
+
original.except(:c).should == expected
|
|
8
|
+
original.should.not == expected
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should remove more than one key" do
|
|
12
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
13
|
+
expected = { :a => 'x' }
|
|
14
|
+
original.except(:b, :c).should == expected
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "should work on frozen hash" do
|
|
18
|
+
original = { :a => 'x', :b => 'y' }
|
|
19
|
+
original.freeze
|
|
20
|
+
lambda { original.except(:a) }.should.not.raise
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "except!" do
|
|
25
|
+
it "should remove key in place" do
|
|
26
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
27
|
+
expected = { :a => 'x', :b => 'y' }
|
|
28
|
+
|
|
29
|
+
original.should.not == expected
|
|
30
|
+
original.except!(:c).should == expected
|
|
31
|
+
original.should == expected
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should remove more than one key in place" do
|
|
35
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
36
|
+
expected = { :a => 'x' }
|
|
37
|
+
|
|
38
|
+
original.should.not == expected
|
|
39
|
+
original.except!(:b, :c).should == expected
|
|
40
|
+
original.should == expected
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
describe "Hash" do
|
|
2
|
+
describe "keys" do
|
|
3
|
+
before do
|
|
4
|
+
@strings = { 'a' => 1, 'b' => 2 }
|
|
5
|
+
@nested_strings = { 'a' => { 'b' => { 'c' => 3 } } }
|
|
6
|
+
@symbols = { :a => 1, :b => 2 }
|
|
7
|
+
@nested_symbols = { :a => { :b => { :c => 3 } } }
|
|
8
|
+
@mixed = { :a => 1, 'b' => 2 }
|
|
9
|
+
@nested_mixed = { 'a' => { :b => { 'c' => 3 } } }
|
|
10
|
+
@fixnums = { 0 => 1, 1 => 2 }
|
|
11
|
+
@nested_fixnums = { 0 => { 1 => { 2 => 3} } }
|
|
12
|
+
@illegal_symbols = { [] => 3 }
|
|
13
|
+
@nested_illegal_symbols = { [] => { [] => 3} }
|
|
14
|
+
@upcase_strings = { 'A' => 1, 'B' => 2 }
|
|
15
|
+
@nested_upcase_strings = { 'A' => { 'B' => { 'C' => 3 } } }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should have all key methods defined on literal hash" do
|
|
19
|
+
h = {}
|
|
20
|
+
h.should.respond_to :transform_keys
|
|
21
|
+
h.should.respond_to :transform_keys!
|
|
22
|
+
h.should.respond_to :deep_transform_keys
|
|
23
|
+
h.should.respond_to :deep_transform_keys!
|
|
24
|
+
h.should.respond_to :symbolize_keys
|
|
25
|
+
h.should.respond_to :symbolize_keys!
|
|
26
|
+
h.should.respond_to :deep_symbolize_keys
|
|
27
|
+
h.should.respond_to :deep_symbolize_keys!
|
|
28
|
+
h.should.respond_to :stringify_keys
|
|
29
|
+
h.should.respond_to :stringify_keys!
|
|
30
|
+
h.should.respond_to :deep_stringify_keys
|
|
31
|
+
h.should.respond_to :deep_stringify_keys!
|
|
32
|
+
h.should.respond_to :to_options
|
|
33
|
+
h.should.respond_to :to_options!
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe "transform_keys" do
|
|
37
|
+
it "should transform keys" do
|
|
38
|
+
@strings.transform_keys{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
39
|
+
@symbols.transform_keys{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
40
|
+
@mixed.transform_keys{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "should not mutate" do
|
|
44
|
+
transformed_hash = @mixed.dup
|
|
45
|
+
transformed_hash.transform_keys{ |key| key.to_s.upcase }
|
|
46
|
+
transformed_hash.should == @mixed
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "deep_transform_keys" do
|
|
51
|
+
it "should deep transform keys" do
|
|
52
|
+
@nested_symbols.deep_transform_keys{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
53
|
+
@nested_strings.deep_transform_keys{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
54
|
+
@nested_mixed.deep_transform_keys{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not mutate" do
|
|
58
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
59
|
+
transformed_hash.deep_transform_keys{ |key| key.to_s.upcase }
|
|
60
|
+
transformed_hash.should == @nested_mixed
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "transform_keys!" do
|
|
65
|
+
it "should transform keys" do
|
|
66
|
+
@symbols.dup.transform_keys!{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
67
|
+
@strings.dup.transform_keys!{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
68
|
+
@mixed.dup.transform_keys!{ |key| key.to_s.upcase }.should == @upcase_strings
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "should mutate" do
|
|
72
|
+
transformed_hash = @mixed.dup
|
|
73
|
+
transformed_hash.transform_keys!{ |key| key.to_s.upcase }
|
|
74
|
+
transformed_hash.should == @upcase_strings
|
|
75
|
+
{ :a => 1, "b" => 2 }.should == @mixed
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
describe "deep_transform_keys!" do
|
|
80
|
+
it "should deep transform keys" do
|
|
81
|
+
@nested_symbols.deep_dup.deep_transform_keys!{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
82
|
+
@nested_strings.deep_dup.deep_transform_keys!{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
83
|
+
@nested_mixed.deep_dup.deep_transform_keys!{ |key| key.to_s.upcase }.should == @nested_upcase_strings
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it "should mutate" do
|
|
87
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
88
|
+
transformed_hash.deep_transform_keys!{ |key| key.to_s.upcase }
|
|
89
|
+
transformed_hash.should == @nested_upcase_strings
|
|
90
|
+
{ 'a' => { :b => { 'c' => 3 } } }.should == @nested_mixed
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
describe "symbolize_keys" do
|
|
95
|
+
it "should symbolize keys" do
|
|
96
|
+
@symbols.symbolize_keys.should == @symbols
|
|
97
|
+
@strings.symbolize_keys.should == @symbols
|
|
98
|
+
@mixed.symbolize_keys.should == @symbols
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it "should not mutate" do
|
|
102
|
+
transformed_hash = @mixed.dup
|
|
103
|
+
transformed_hash.symbolize_keys
|
|
104
|
+
transformed_hash.should == @mixed
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should preserve keys that can't be symbolized" do
|
|
108
|
+
@illegal_symbols.symbolize_keys.should == @illegal_symbols
|
|
109
|
+
@illegal_symbols.dup.symbolize_keys!.should == @illegal_symbols
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "should preserve fixnum keys" do
|
|
113
|
+
@fixnums.symbolize_keys.should == @fixnums
|
|
114
|
+
@fixnums.dup.symbolize_keys!.should == @fixnums
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
describe "deep_symbolize_keys" do
|
|
119
|
+
it "should deep symbolize keys" do
|
|
120
|
+
@nested_symbols.deep_symbolize_keys.should == @nested_symbols
|
|
121
|
+
@nested_strings.deep_symbolize_keys.should == @nested_symbols
|
|
122
|
+
@nested_mixed.deep_symbolize_keys.should == @nested_symbols
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "should not mutate" do
|
|
126
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
127
|
+
transformed_hash.deep_symbolize_keys
|
|
128
|
+
transformed_hash.should == @nested_mixed
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it "should preserve keys that can't be symbolized" do
|
|
132
|
+
@nested_illegal_symbols.deep_symbolize_keys.should == @nested_illegal_symbols
|
|
133
|
+
@nested_illegal_symbols.deep_dup.deep_symbolize_keys!.should == @nested_illegal_symbols
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it "should preserve fixnum keys" do
|
|
137
|
+
@nested_fixnums.deep_symbolize_keys.should == @nested_fixnums
|
|
138
|
+
@nested_fixnums.deep_dup.deep_symbolize_keys!.should == @nested_fixnums
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
describe "symbolize_keys!" do
|
|
143
|
+
it "should symbolize keys" do
|
|
144
|
+
@symbols.dup.symbolize_keys!.should == @symbols
|
|
145
|
+
@strings.dup.symbolize_keys!.should == @symbols
|
|
146
|
+
@mixed.dup.symbolize_keys!.should == @symbols
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "should mutate" do
|
|
150
|
+
transformed_hash = @mixed.dup
|
|
151
|
+
transformed_hash.deep_symbolize_keys!
|
|
152
|
+
transformed_hash.should == @symbols
|
|
153
|
+
{ :a => 1, "b" => 2 }.should == @mixed
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
describe "deep_symbolize_keys!" do
|
|
158
|
+
it "should deep symbolize keys" do
|
|
159
|
+
@nested_symbols.deep_dup.deep_symbolize_keys!.should == @nested_symbols
|
|
160
|
+
@nested_strings.deep_dup.deep_symbolize_keys!.should == @nested_symbols
|
|
161
|
+
@nested_mixed.deep_dup.deep_symbolize_keys!.should == @nested_symbols
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it "should mutate" do
|
|
165
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
166
|
+
transformed_hash.deep_symbolize_keys!
|
|
167
|
+
transformed_hash.should == @nested_symbols
|
|
168
|
+
{ 'a' => { :b => { 'c' => 3 } } }.should == @nested_mixed
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
describe "stringify_keys" do
|
|
173
|
+
it "should stringify keys" do
|
|
174
|
+
@symbols.stringify_keys.should == @strings
|
|
175
|
+
@strings.stringify_keys.should == @strings
|
|
176
|
+
@mixed.stringify_keys.should == @strings
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "should not mutate" do
|
|
180
|
+
transformed_hash = @mixed.dup
|
|
181
|
+
transformed_hash.stringify_keys
|
|
182
|
+
transformed_hash.should == @mixed
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
describe "deep stringify_keys" do
|
|
187
|
+
it "should deep stringify keys" do
|
|
188
|
+
@nested_symbols.deep_stringify_keys.should == @nested_strings
|
|
189
|
+
@nested_strings.deep_stringify_keys.should == @nested_strings
|
|
190
|
+
@nested_mixed.deep_stringify_keys.should == @nested_strings
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it "should not mutate" do
|
|
194
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
195
|
+
transformed_hash.deep_stringify_keys
|
|
196
|
+
transformed_hash.should == @nested_mixed
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
describe "stringify_keys!" do
|
|
201
|
+
it "should stringify keys" do
|
|
202
|
+
@symbols.dup.stringify_keys!.should == @strings
|
|
203
|
+
@strings.dup.stringify_keys!.should == @strings
|
|
204
|
+
@mixed.dup.stringify_keys!.should == @strings
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
it "should mutate" do
|
|
208
|
+
transformed_hash = @mixed.dup
|
|
209
|
+
transformed_hash.stringify_keys!
|
|
210
|
+
transformed_hash.should == @strings
|
|
211
|
+
{ :a => 1, "b" => 2 }.should == @mixed
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
describe "deep_stringify_keys!" do
|
|
216
|
+
it "should deep stringify keys" do
|
|
217
|
+
@nested_symbols.deep_dup.deep_stringify_keys!.should == @nested_strings
|
|
218
|
+
@nested_strings.deep_dup.deep_stringify_keys!.should == @nested_strings
|
|
219
|
+
@nested_mixed.deep_dup.deep_stringify_keys!.should == @nested_strings
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
it "should mutate" do
|
|
223
|
+
transformed_hash = @nested_mixed.deep_dup
|
|
224
|
+
transformed_hash.deep_stringify_keys!
|
|
225
|
+
transformed_hash.should == @nested_strings
|
|
226
|
+
{ 'a' => { :b => { 'c' => 3 } } }.should == @nested_mixed
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
describe "hash" do
|
|
2
|
+
describe "reverse_merge" do
|
|
3
|
+
before do
|
|
4
|
+
@defaults = { :a => "x", :b => "y", :c => 10 }.freeze
|
|
5
|
+
@options = { :a => 1, :b => 2 }
|
|
6
|
+
@expected = { :a => 1, :b => 2, :c => 10 }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should merge defaults into options, creating a new hash" do
|
|
10
|
+
@options.reverse_merge(@defaults).should == @expected
|
|
11
|
+
@options.should.not == @expected
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should merge! defaults into options, replacing options" do
|
|
15
|
+
merged = @options.dup
|
|
16
|
+
merged.reverse_merge!(@defaults).should == @expected
|
|
17
|
+
merged.should == @expected
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should be an alias for reverse_merge!" do
|
|
21
|
+
merged = @options.dup
|
|
22
|
+
merged.reverse_update(@defaults).should == @expected
|
|
23
|
+
merged.should == @expected
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
describe "hash" do
|
|
2
|
+
describe "slice" do
|
|
3
|
+
it "should return a new hash with only the given keys" do
|
|
4
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
5
|
+
expected = { :a => 'x', :b => 'y' }
|
|
6
|
+
|
|
7
|
+
original.slice(:a, :b).should == expected
|
|
8
|
+
original.should.not == expected
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should replace the hash with only the given keys" do
|
|
12
|
+
original = { :a => 'x', :b => 'y', :c => 10 }
|
|
13
|
+
expected = { :c => 10 }
|
|
14
|
+
|
|
15
|
+
original.slice!(:a, :b).should == expected
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should return a new hash with only the given keys when given an array key" do
|
|
19
|
+
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
|
20
|
+
expected = { [:a, :b] => "an array key", :c => 10 }
|
|
21
|
+
|
|
22
|
+
original.slice([:a, :b], :c).should == expected
|
|
23
|
+
original.should.not == expected
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should replace the hash with only the given keys when given an array key" do
|
|
27
|
+
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
|
28
|
+
expected = { :a => 'x', :b => 'y' }
|
|
29
|
+
|
|
30
|
+
original.slice!([:a, :b], :c).should == expected
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should grab each of the splatted keys" do
|
|
34
|
+
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
|
35
|
+
expected = { :a => 'x', :b => "y" }
|
|
36
|
+
|
|
37
|
+
original.slice(*[:a, :b]).should == expected
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe "extract!" do
|
|
42
|
+
it "should extract subhash" do
|
|
43
|
+
original = {:a => 1, :b => 2, :c => 3, :d => 4}
|
|
44
|
+
expected = {:a => 1, :b => 2}
|
|
45
|
+
remaining = {:c => 3, :d => 4}
|
|
46
|
+
|
|
47
|
+
original.extract!(:a, :b, :x).should == expected
|
|
48
|
+
original.should == remaining
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "should extract nils" do
|
|
52
|
+
original = {:a => nil, :b => nil}
|
|
53
|
+
expected = {:a => nil}
|
|
54
|
+
extracted = original.extract!(:a, :x)
|
|
55
|
+
|
|
56
|
+
extracted.should == expected
|
|
57
|
+
extracted[:a].should == nil
|
|
58
|
+
extracted[:x].should == nil
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# These tests are mostly just to ensure that the ordinalize method exists.
|
|
2
|
+
# Its results are tested comprehensively in the inflector test cases.
|
|
3
|
+
describe "Integer" do
|
|
4
|
+
describe "ordinalize" do
|
|
5
|
+
it "should ordinalize 1" do
|
|
6
|
+
1.ordinalize.should == '1st'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should ordinalize 8" do
|
|
10
|
+
8.ordinalize.should == '8th'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "ordinal" do
|
|
15
|
+
it "should get ordinal of 1" do
|
|
16
|
+
1.ordinal.should == 'st'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should get ordinal of 8" do
|
|
20
|
+
8.ordinal.should == 'th'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
PRIME = 22953686867719691230002707821868552601124472329079
|
|
2
|
+
|
|
3
|
+
describe "Integer" do
|
|
4
|
+
describe "multiple_of" do
|
|
5
|
+
it "should determine if an integer is a multiple of another" do
|
|
6
|
+
[ -7, 0, 7, 14 ].each { |i| i.should.be.multiple_of 7 }
|
|
7
|
+
[ -7, 7, 14 ].each { |i| i.should.not.be.multiple_of 6 }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should work with edge cases" do
|
|
11
|
+
0.should.be.multiple_of 0
|
|
12
|
+
5.should.not.be.multiple_of 0
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should work with a prime" do
|
|
16
|
+
[2, 3, 5, 7].each { |i| PRIME.should.not.be.multiple_of i }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
File without changes
|