motion_blender-support 0.2.7
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.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/HACKS.md +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +359 -0
- data/Rakefile +14 -0
- data/app/app_delegate.rb +5 -0
- 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 +109 -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/callbacks.rb +8 -0
- data/lib/motion-support/concern.rb +4 -0
- data/lib/motion-support/core_ext/array.rb +10 -0
- data/lib/motion-support/core_ext/class.rb +5 -0
- data/lib/motion-support/core_ext/hash.rb +13 -0
- data/lib/motion-support/core_ext/integer.rb +6 -0
- data/lib/motion-support/core_ext/module.rb +11 -0
- data/lib/motion-support/core_ext/numeric.rb +6 -0
- data/lib/motion-support/core_ext/object.rb +12 -0
- data/lib/motion-support/core_ext/range.rb +5 -0
- data/lib/motion-support/core_ext/string.rb +13 -0
- data/lib/motion-support/core_ext/time.rb +16 -0
- data/lib/motion-support/core_ext.rb +13 -0
- data/lib/motion-support/inflector.rb +8 -0
- data/lib/motion-support.rb +81 -0
- data/motion/_stdlib/array.rb +13 -0
- data/motion/_stdlib/cgi.rb +22 -0
- data/motion/_stdlib/date.rb +81 -0
- data/motion/_stdlib/enumerable.rb +9 -0
- data/motion/_stdlib/time.rb +19 -0
- data/motion/callbacks.rb +511 -0
- data/motion/concern.rb +122 -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/motion/core_ext/array.rb +19 -0
- 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_delete_if.rb +23 -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 +150 -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/motion/core_ext/kernel/singleton_class.rb +6 -0
- data/motion/core_ext/metaclass.rb +8 -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 +14 -0
- data/motion/core_ext/ns_string.rb +14 -0
- data/motion/core_ext/numeric/bytes.rb +44 -0
- data/motion/core_ext/numeric/conversions.rb +49 -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 +87 -0
- data/motion/core_ext/object/inclusion.rb +15 -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 +178 -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/descendants_tracker.rb +50 -0
- data/motion/duration.rb +104 -0
- data/motion/hash_with_indifferent_access.rb +253 -0
- data/motion/inflections.rb +67 -0
- data/motion/inflector/inflections.rb +203 -0
- data/motion/inflector/methods.rb +321 -0
- data/motion/logger.rb +47 -0
- data/motion/number_helper.rb +54 -0
- data/motion/version.rb +3 -0
- data/motion_blender-support.gemspec +21 -0
- data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
- data/spec/motion-support/_helpers/inflector_test_cases.rb +270 -0
- data/spec/motion-support/callback_spec.rb +702 -0
- data/spec/motion-support/concern_spec.rb +93 -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/core_ext/array_spec.rb +16 -0
- 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_delete_if_spec.rb +19 -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 +236 -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/core_ext/kernel/singleton_class_spec.rb +9 -0
- data/spec/motion-support/core_ext/metaclass_spec.rb +9 -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/numeric/conversions_spec.rb +40 -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/inclusion_spec.rb +34 -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 +49 -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 +53 -0
- data/spec/motion-support/descendants_tracker_spec.rb +58 -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 +504 -0
- data/spec/motion-support/ns_dictionary_spec.rb +89 -0
- data/spec/motion-support/ns_string_spec.rb +182 -0
- data/spec/motion-support/number_helper_spec.rb +55 -0
- metadata +352 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
class ConcernTest
|
|
2
|
+
module Baz
|
|
3
|
+
extend MotionSupport::Concern
|
|
4
|
+
|
|
5
|
+
module ClassMethods
|
|
6
|
+
def baz
|
|
7
|
+
"baz"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def included_ran=(value)
|
|
11
|
+
@@included_ran = value
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def included_ran
|
|
15
|
+
@@included_ran
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
included do
|
|
20
|
+
self.included_ran = true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def baz
|
|
24
|
+
"baz"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
module Bar
|
|
29
|
+
extend MotionSupport::Concern
|
|
30
|
+
|
|
31
|
+
include Baz
|
|
32
|
+
|
|
33
|
+
def bar
|
|
34
|
+
"bar"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def baz
|
|
38
|
+
"bar+" + super
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module Foo
|
|
43
|
+
extend MotionSupport::Concern
|
|
44
|
+
|
|
45
|
+
include Bar, Baz
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "Concern" do
|
|
50
|
+
before do
|
|
51
|
+
@klass = Class.new
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "should be included normally" do
|
|
55
|
+
@klass.send(:include, ConcernTest::Baz)
|
|
56
|
+
@klass.new.baz.should == "baz"
|
|
57
|
+
@klass.included_modules.include?(ConcernTest::Baz).should.be.true
|
|
58
|
+
|
|
59
|
+
@klass.send(:include, ConcernTest::Baz)
|
|
60
|
+
@klass.new.baz.should == "baz"
|
|
61
|
+
@klass.included_modules.include?(ConcernTest::Baz).should.be.true
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "should extend class methods" do
|
|
65
|
+
@klass.send(:include, ConcernTest::Baz)
|
|
66
|
+
@klass.baz.should == "baz"
|
|
67
|
+
(class << @klass; self.included_modules; end)[0].should == ConcernTest::Baz::ClassMethods
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "should include instance methods" do
|
|
71
|
+
@klass.send(:include, ConcernTest::Baz)
|
|
72
|
+
@klass.new.baz.should == "baz"
|
|
73
|
+
@klass.included_modules.include?(ConcernTest::Baz).should.be.true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "should run included block" do
|
|
77
|
+
@klass.send(:include, ConcernTest::Baz)
|
|
78
|
+
@klass.included_ran.should.be.true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should meet dependencies" do
|
|
82
|
+
@klass.send(:include, ConcernTest::Bar)
|
|
83
|
+
@klass.new.bar.should == "bar"
|
|
84
|
+
@klass.new.baz.should == "bar+baz"
|
|
85
|
+
@klass.baz.should == "baz"
|
|
86
|
+
@klass.included_modules.include?(ConcernTest::Bar).should.be.true
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "should meet dependencies with multiple modules" do
|
|
90
|
+
@klass.send(:include, ConcernTest::Foo)
|
|
91
|
+
@klass.included_modules[0..2].should == [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
describe 'array access' do
|
|
2
|
+
describe "from" do
|
|
3
|
+
it "should return the tail of an array from position" do
|
|
4
|
+
['a', 'b', 'c', 'd'].from(0).should == ["a", "b", "c", "d"]
|
|
5
|
+
['a', 'b', 'c', 'd'].from(2).should == ["c", "d"]
|
|
6
|
+
['a', 'b', 'c', 'd'].from(10).should == []
|
|
7
|
+
[].from(0).should == []
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe "to" do
|
|
12
|
+
it "should return the head of an array up to position" do
|
|
13
|
+
['a', 'b', 'c', 'd'].to(0).should == ["a"]
|
|
14
|
+
['a', 'b', 'c', 'd'].to(2).should == ["a", "b", "c"]
|
|
15
|
+
['a', 'b', 'c', 'd'].to(10).should == ["a", "b", "c", "d"]
|
|
16
|
+
[].to(0).should == []
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "second" do
|
|
21
|
+
it "should return the second element in an array" do
|
|
22
|
+
['a', 'b', 'c', 'd'].second.should == 'b'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should return nil if there is no second element" do
|
|
26
|
+
[].second.should == nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
describe 'array' do
|
|
2
|
+
describe "to_sentence" do
|
|
3
|
+
it "should convert plain array" do
|
|
4
|
+
[].to_sentence.should == ""
|
|
5
|
+
['one'].to_sentence.should == "one"
|
|
6
|
+
['one', 'two'].to_sentence.should == "one and two"
|
|
7
|
+
['one', 'two', 'three'].to_sentence.should == "one, two, and three"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should convert sentence with words connector" do
|
|
11
|
+
['one', 'two', 'three'].to_sentence(:words_connector => ' ').should == "one two, and three"
|
|
12
|
+
['one', 'two', 'three'].to_sentence(:words_connector => ' & ').should == "one & two, and three"
|
|
13
|
+
['one', 'two', 'three'].to_sentence(:words_connector => nil).should == "onetwo, and three"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should convert sentence with last word connector" do
|
|
17
|
+
['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ').should == "one, two, and also three"
|
|
18
|
+
['one', 'two', 'three'].to_sentence(:last_word_connector => nil).should == "one, twothree"
|
|
19
|
+
['one', 'two', 'three'].to_sentence(:last_word_connector => ' ').should == "one, two three"
|
|
20
|
+
['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ').should == "one, two and three"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should convert two-element array" do
|
|
24
|
+
['one', 'two'].to_sentence.should == "one and two"
|
|
25
|
+
['one', 'two'].to_sentence(:two_words_connector => ' ').should == "one two"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should convert one-element array" do
|
|
29
|
+
['one'].to_sentence.should == "one"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should create new object" do
|
|
33
|
+
elements = ["one"]
|
|
34
|
+
elements.to_sentence.object_id.should.not == elements[0].object_id
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "should convert non-string element" do
|
|
38
|
+
[1].to_sentence.should == '1'
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should not modify given hash" do
|
|
42
|
+
options = { words_connector: ' ' }
|
|
43
|
+
['one', 'two', 'three'].to_sentence(options).should == "one two, and three"
|
|
44
|
+
options.should == { words_connector: ' ' }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe "to_s" do
|
|
49
|
+
it "should convert to database format" do
|
|
50
|
+
collection = [
|
|
51
|
+
Class.new { def id() 1 end }.new,
|
|
52
|
+
Class.new { def id() 2 end }.new,
|
|
53
|
+
Class.new { def id() 3 end }.new
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[].to_s(:db).should == "null"
|
|
57
|
+
collection.to_s(:db).should == "1,2,3"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
describe 'array options' do
|
|
2
|
+
describe "extract_options!" do
|
|
3
|
+
it "should extract an options hash from an array" do
|
|
4
|
+
[1, 2, :a => :b].extract_options!.should == { :a => :b }
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should return an empty hash if the last element is not a hash" do
|
|
8
|
+
[1, 2].extract_options!.should == {}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should return an empty hash on an empty array" do
|
|
12
|
+
[].extract_options!.should == {}
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
describe 'array grouping' do
|
|
2
|
+
describe "in_groups_of" do
|
|
3
|
+
it "should group array and fill rest with nil" do
|
|
4
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3).should == [
|
|
5
|
+
["1", "2", "3"],
|
|
6
|
+
["4", "5", "6"],
|
|
7
|
+
["7", "8", "9"],
|
|
8
|
+
["10", nil, nil]
|
|
9
|
+
]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "should group array and fill with specified filler" do
|
|
13
|
+
%w(1 2 3 4 5).in_groups_of(2, 'empty').should == [
|
|
14
|
+
["1", "2"],
|
|
15
|
+
["3", "4"],
|
|
16
|
+
["5", "empty"]
|
|
17
|
+
]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should not fill the last group if turned off" do
|
|
21
|
+
%w(1 2 3 4 5).in_groups_of(2, false).should == [
|
|
22
|
+
["1", "2"],
|
|
23
|
+
["3", "4"],
|
|
24
|
+
["5"]
|
|
25
|
+
]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should yield each slice to a block if given" do
|
|
29
|
+
result = []
|
|
30
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) { |group| result << ['foo'] + group + ['bar'] }
|
|
31
|
+
result.should == [
|
|
32
|
+
["foo", "1", "2", "3", "bar"],
|
|
33
|
+
["foo", "4", "5", "6", "bar"],
|
|
34
|
+
["foo", "7", "8", "9", "bar"],
|
|
35
|
+
["foo", "10", nil, nil, "bar"]
|
|
36
|
+
]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe "in_groups" do
|
|
41
|
+
it "should group array and fill the rest with nil" do
|
|
42
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3).should == [
|
|
43
|
+
["1", "2", "3", "4"],
|
|
44
|
+
["5", "6", "7", nil],
|
|
45
|
+
["8", "9", "10", nil]
|
|
46
|
+
]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should group array and fill the result with specified filler" do
|
|
50
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, 'empty').should == [
|
|
51
|
+
["1", "2", "3", "4"],
|
|
52
|
+
["5", "6", "7", "empty"],
|
|
53
|
+
["8", "9", "10", "empty"]
|
|
54
|
+
]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not fill the last group if turned off" do
|
|
58
|
+
%w(1 2 3 4 5 6 7).in_groups(3, false).should == [
|
|
59
|
+
["1", "2", "3"],
|
|
60
|
+
["4", "5"],
|
|
61
|
+
["6", "7"]
|
|
62
|
+
]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "should yield each slice to a block if given" do
|
|
66
|
+
result = []
|
|
67
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3) { |group| result << ['foo'] + group + ['bar'] }
|
|
68
|
+
result.should == [
|
|
69
|
+
["foo", "1", "2", "3", "4", "bar"],
|
|
70
|
+
["foo", "5", "6", "7", nil, "bar"],
|
|
71
|
+
["foo", "8", "9", "10", nil, "bar"]
|
|
72
|
+
]
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
describe "split" do
|
|
77
|
+
it "should split array based on delimiting value" do
|
|
78
|
+
[1, 2, 3, 4, 5].split(3).should == [[1, 2], [4, 5]]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should split array based on block result" do
|
|
82
|
+
(1..10).to_a.split { |i| i % 3 == 0 }.should == [[1, 2], [4, 5], [7, 8], [10]]
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
describe 'array' do
|
|
2
|
+
describe "prepend" do
|
|
3
|
+
it "should add an element to the front of the array" do
|
|
4
|
+
[1, 2, 3].prepend(0).should == [0, 1, 2, 3]
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should change the array" do
|
|
8
|
+
array = [1, 2, 3]
|
|
9
|
+
array.prepend(0)
|
|
10
|
+
array.should == [0, 1, 2, 3]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "append" do
|
|
15
|
+
it "should add an element to the back of the array" do
|
|
16
|
+
[1, 2, 3].append(4).should == [1, 2, 3, 4]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should change the array" do
|
|
20
|
+
array = [1, 2, 3]
|
|
21
|
+
array.append(4)
|
|
22
|
+
array.should == [1, 2, 3, 4]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
describe 'array' do
|
|
2
|
+
describe "wrap" do
|
|
3
|
+
it "should return empty array for nil" do
|
|
4
|
+
Array.wrap(nil).should == []
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should return unchanged array for array" do
|
|
8
|
+
Array.wrap([1, 2, 3]).should == [1, 2, 3]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should not flatten multidimensional array" do
|
|
12
|
+
Array.wrap([[1], [2], [3]]).should == [[1], [2], [3]]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should turn simple object into array" do
|
|
16
|
+
Array.wrap(0).should == [0]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
describe 'array' do
|
|
2
|
+
it 'finds hash values' do
|
|
3
|
+
array_of_hashes = [
|
|
4
|
+
{
|
|
5
|
+
line1: 3,
|
|
6
|
+
line2: 5
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
line3: 7,
|
|
10
|
+
line4: 9
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
array_of_hashes.has_hash_value?(5).should.be.true
|
|
14
|
+
array_of_hashes.has_hash_value?(4).should.not.be.true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
class CAttrAccessorBase
|
|
2
|
+
cattr_accessor :empty_accessor
|
|
3
|
+
cattr_accessor :base_accessor, :derived_accessor
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class CAttrAccessorDerived < CAttrAccessorBase
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe "class" do
|
|
10
|
+
describe "attribute accessors" do
|
|
11
|
+
before do
|
|
12
|
+
@class = Class.new
|
|
13
|
+
@class.instance_eval do
|
|
14
|
+
cattr_accessor :foo
|
|
15
|
+
cattr_accessor :bar, :instance_writer => false
|
|
16
|
+
cattr_reader :shaq, :instance_reader => false
|
|
17
|
+
cattr_accessor :camp, :instance_accessor => false
|
|
18
|
+
end
|
|
19
|
+
@object = @class.new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "reader" do
|
|
23
|
+
it "should return nil by default" do
|
|
24
|
+
@class.foo.should.be.nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe "writer" do
|
|
29
|
+
it "should set value" do
|
|
30
|
+
@class.foo = :test
|
|
31
|
+
@class.foo.should == :test
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should set value through instance writer" do
|
|
35
|
+
@object.foo = :bar
|
|
36
|
+
@object.foo.should == :bar
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "should set instance reader's value through module's writer" do
|
|
40
|
+
@class.foo = :test
|
|
41
|
+
@object.foo.should == :test
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "should set module reader's value through instances's writer" do
|
|
45
|
+
@object.foo = :bar
|
|
46
|
+
@class.foo.should == :bar
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "instance_writer => false" do
|
|
51
|
+
it "should not create instance writer" do
|
|
52
|
+
@class.should.respond_to :foo
|
|
53
|
+
@class.should.respond_to :foo=
|
|
54
|
+
@object.should.respond_to :bar
|
|
55
|
+
@object.should.not.respond_to :bar=
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
describe "instance_reader => false" do
|
|
60
|
+
it "should not create instance reader" do
|
|
61
|
+
@class.should.respond_to :shaq
|
|
62
|
+
@object.should.not.respond_to :shaq
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe "instance_accessor => false" do
|
|
67
|
+
it "should not create reader or writer" do
|
|
68
|
+
@class.should.respond_to :camp
|
|
69
|
+
@object.should.not.respond_to :camp
|
|
70
|
+
@object.should.not.respond_to :camp=
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
describe "invalid attribute accessors" do
|
|
76
|
+
it "should raise NameError when creating an invalid reader" do
|
|
77
|
+
lambda do
|
|
78
|
+
Class.new do
|
|
79
|
+
cattr_reader "invalid attribute name"
|
|
80
|
+
end
|
|
81
|
+
end.should.raise NameError
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "should raise NameError when creating an invalid writer" do
|
|
85
|
+
lambda do
|
|
86
|
+
Class.new do
|
|
87
|
+
cattr_writer "invalid attribute name"
|
|
88
|
+
end
|
|
89
|
+
end.should.raise NameError
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
describe "inheritance" do
|
|
94
|
+
it "should be accessible in the base class and the derived class" do
|
|
95
|
+
CAttrAccessorBase.respond_to?(:empty_accessor).should == true
|
|
96
|
+
CAttrAccessorDerived.respond_to?(:empty_accessor).should == true
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "should return nil for an unset accessor in the base class" do
|
|
100
|
+
CAttrAccessorBase.empty_accessor.should == nil
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "should return nil for an unset accessor in the derived class" do
|
|
104
|
+
CAttrAccessorDerived.empty_accessor.should == nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "should return a value for an accessor set in the base class in the base class" do
|
|
108
|
+
CAttrAccessorBase.base_accessor = 10
|
|
109
|
+
CAttrAccessorBase.base_accessor.should == 10
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "should return a value for an accessor set in the base class in the derived class" do
|
|
113
|
+
CAttrAccessorBase.base_accessor = 10
|
|
114
|
+
CAttrAccessorDerived.base_accessor.should == 10
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it "should return a value for the base class if set for the derived class" do
|
|
118
|
+
CAttrAccessorDerived.derived_accessor = 20
|
|
119
|
+
CAttrAccessorBase.derived_accessor.should == 20
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "should return a value for an accessor set in the derived class in the derived class" do
|
|
123
|
+
CAttrAccessorDerived.derived_accessor = 20
|
|
124
|
+
CAttrAccessorDerived.derived_accessor.should == 20
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
describe "class" do
|
|
2
|
+
describe "class_attribute" do
|
|
3
|
+
before do
|
|
4
|
+
@klass = Class.new
|
|
5
|
+
@klass.class_eval { class_attribute :setting }
|
|
6
|
+
@sub = Class.new(@klass)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should default to nil" do
|
|
10
|
+
@klass.setting.should.be.nil
|
|
11
|
+
@sub.setting.should.be.nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should be inheritable" do
|
|
15
|
+
@klass.setting = 1
|
|
16
|
+
@sub.setting.should == 1
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should be overridable" do
|
|
20
|
+
@sub.setting = 1
|
|
21
|
+
@klass.setting.should.be.nil
|
|
22
|
+
|
|
23
|
+
@klass.setting = 2
|
|
24
|
+
@sub.setting.should == 1
|
|
25
|
+
|
|
26
|
+
Class.new(@sub).setting.should == 1
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should define a query method" do
|
|
30
|
+
@klass.setting?.should.be.false
|
|
31
|
+
@klass.setting = 1
|
|
32
|
+
@klass.setting?.should.be.true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "should define an instance reader that delegates to class" do
|
|
36
|
+
@klass.new.setting.should.be.nil
|
|
37
|
+
|
|
38
|
+
@klass.setting = 1
|
|
39
|
+
@klass.new.setting.should == 1
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "should allow to override per instance" do
|
|
43
|
+
object = @klass.new
|
|
44
|
+
object.setting = 1
|
|
45
|
+
@klass.setting.should == nil
|
|
46
|
+
@klass.setting = 2
|
|
47
|
+
object.setting.should == 1
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "should define query method on instance" do
|
|
51
|
+
object = @klass.new
|
|
52
|
+
object.setting?.should.be.false
|
|
53
|
+
object.setting = 1
|
|
54
|
+
object.setting?.should.be.true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "instance_writer => false" do
|
|
58
|
+
it "should not create instance writer" do
|
|
59
|
+
object = Class.new { class_attribute :setting, :instance_writer => false }.new
|
|
60
|
+
lambda { object.setting = 'boom' }.should.raise NoMethodError
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "instance_reader => false" do
|
|
65
|
+
it "should not create instance reader" do
|
|
66
|
+
object = Class.new { class_attribute :setting, :instance_reader => false }.new
|
|
67
|
+
lambda { object.setting }.should.raise NoMethodError
|
|
68
|
+
lambda { object.setting? }.should.raise NoMethodError
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
describe "instance_accessor => false" do
|
|
73
|
+
it "should not create reader or writer" do
|
|
74
|
+
object = Class.new { class_attribute :setting, :instance_accessor => false }.new
|
|
75
|
+
lambda { object.setting }.should.raise NoMethodError
|
|
76
|
+
lambda { object.setting? }.should.raise NoMethodError
|
|
77
|
+
lambda { object.setting = 'boom' }.should.raise NoMethodError
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should work well with singleton classes" do
|
|
82
|
+
object = @klass.new
|
|
83
|
+
object.singleton_class.setting = 'foo'
|
|
84
|
+
object.setting.should == "foo"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "should return set value through setter" do
|
|
88
|
+
val = @klass.send(:setting=, 1)
|
|
89
|
+
val.should == 1
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|