sugar-high 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/Gemfile +0 -2
- data/VERSION +1 -1
- data/lib/sugar-high/alias.rb +17 -17
- data/lib/sugar-high/arguments.rb +6 -6
- data/lib/sugar-high/array.rb +23 -19
- data/lib/sugar-high/blank.rb +2 -2
- data/lib/sugar-high/delegate.rb +2 -2
- data/lib/sugar-high/enumerable.rb +9 -9
- data/lib/sugar-high/file_ext.rb +1 -1
- data/lib/sugar-high/hash.rb +5 -5
- data/lib/sugar-high/includes.rb +7 -7
- data/lib/sugar-high/math.rb +1 -1
- data/lib/sugar-high/methods.rb +1 -1
- data/lib/sugar-high/module.rb +4 -4
- data/lib/sugar-high/numeric.rb +4 -4
- data/lib/sugar-high/path.rb +10 -10
- data/lib/sugar-high/properties.rb +12 -12
- data/lib/sugar-high/regexp.rb +3 -3
- data/lib/sugar-high/rspec/matchers/have_aliases.rb +8 -8
- data/lib/sugar-high/string.rb +1 -1
- data/spec/sugar-high/alias_spec.rb +21 -21
- data/spec/sugar-high/arguments_spec.rb +16 -16
- data/spec/sugar-high/array_spec.rb +39 -33
- data/spec/sugar-high/blank_spec.rb +3 -3
- data/spec/sugar-high/delegate_spec.rb +2 -2
- data/spec/sugar-high/file_spec.rb +4 -4
- data/spec/sugar-high/hash_spec.rb +8 -3
- data/spec/sugar-high/kind_of_spec.rb +38 -39
- data/spec/sugar-high/methods_spec.rb +5 -5
- data/spec/sugar-high/numeric_spec.rb +4 -5
- data/spec/sugar-high/path_spec.rb +20 -20
- data/spec/sugar-high/properties_spec.rb +11 -11
- data/spec/sugar-high/rails/concerns_spec.rb +3 -3
- data/spec/sugar-high/regexp_spec.rb +2 -2
- data/sugar-high.gemspec +11 -14
- metadata +38 -24
- data/spec/sugar-high/delegate_defined_spec.rb +0 -14
data/lib/sugar-high/path.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
class String
|
1
|
+
class String
|
2
2
|
def path
|
3
3
|
self.extend PathString
|
4
|
-
end
|
4
|
+
end
|
5
5
|
end
|
6
6
|
|
7
7
|
module PathString
|
@@ -15,38 +15,38 @@ module PathString
|
|
15
15
|
def to_dir option=nil
|
16
16
|
raise ArgumentError, "Dir doesn't exist" if option == :raise && !File.directory?(self)
|
17
17
|
Dir.new(self) if File.directory? self
|
18
|
-
end
|
18
|
+
end
|
19
19
|
alias_method :new_dir, :to_dir
|
20
20
|
alias_method :dir, :to_dir
|
21
21
|
|
22
|
-
def to_symlink new_path #, option=nil
|
22
|
+
def to_symlink new_path #, option=nil
|
23
23
|
# raise ArgumentError, "New link location doesn't exist" if option == :raise && !File.exist?(new_path)
|
24
24
|
File.symlink(self, new_path)
|
25
25
|
end
|
26
26
|
alias_method :new_symlink, :to_symlink
|
27
27
|
alias_method :symlink, :to_symlink
|
28
|
-
|
29
|
-
def exists?
|
28
|
+
|
29
|
+
def exists?
|
30
30
|
File.exist? self
|
31
31
|
end
|
32
32
|
alias_method :there?, :exists?
|
33
33
|
|
34
|
-
def file?
|
34
|
+
def file?
|
35
35
|
File.file? self
|
36
36
|
end
|
37
37
|
alias_method :is_file?, :file?
|
38
38
|
|
39
|
-
def dir?
|
39
|
+
def dir?
|
40
40
|
File.directory? self
|
41
41
|
end
|
42
42
|
alias_method :is_dir?, :dir?
|
43
43
|
alias_method :directory?, :dir
|
44
44
|
|
45
|
-
def symlink?
|
45
|
+
def symlink?
|
46
46
|
File.symlink? self
|
47
47
|
end
|
48
48
|
alias_method :is_symlink?, :symlink?
|
49
|
-
|
49
|
+
|
50
50
|
def up lv
|
51
51
|
('../' * lv) + self
|
52
52
|
end
|
@@ -1,39 +1,39 @@
|
|
1
1
|
# From: http://www.infoq.com/articles/properties-metaprogramming
|
2
2
|
|
3
|
-
module Properties
|
4
|
-
def self.extended(base)
|
3
|
+
module Properties
|
4
|
+
def self.extended(base)
|
5
5
|
base.class_eval %{
|
6
|
-
def fire_event_for(sym, arg)
|
6
|
+
def fire_event_for(sym, arg)
|
7
7
|
return if !@listener[sym]
|
8
|
-
@listener[sym].each {|l| l.call(arg) }
|
9
|
-
end
|
8
|
+
@listener[sym].each {|l| l.call(arg) }
|
9
|
+
end
|
10
10
|
}
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
# def property(sym, &predicate)
|
14
14
|
def property(sym, predicate=nil)
|
15
15
|
define_method(sym) do
|
16
16
|
instance_variable_get("@#{sym}")
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
define_method("#{sym}=") do |arg|
|
20
20
|
return if !predicate.call(arg) if predicate
|
21
21
|
instance_variable_set("@#{sym}", arg)
|
22
22
|
fire_event_for(sym, arg)
|
23
23
|
end
|
24
|
-
|
25
|
-
define_method("add_#{sym}_listener") do |x|
|
24
|
+
|
25
|
+
define_method("add_#{sym}_listener") do |x|
|
26
26
|
@listener ||= {}
|
27
27
|
@listener[sym] ||= []
|
28
28
|
@listener[sym] << x
|
29
29
|
end
|
30
|
-
|
31
|
-
define_method("remove_#{sym}_listener") do |x|
|
30
|
+
|
31
|
+
define_method("remove_#{sym}_listener") do |x|
|
32
32
|
return if !@listener[sym]
|
33
33
|
@listener[sym].delete_at(x)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def is(test)
|
38
38
|
lambda {|val| test === val }
|
39
39
|
end
|
data/lib/sugar-high/regexp.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class String
|
1
|
+
class String
|
2
2
|
def to_regexp
|
3
3
|
/#{Regexp.escape(self)}/
|
4
4
|
end
|
@@ -7,7 +7,7 @@ end
|
|
7
7
|
class Regexp
|
8
8
|
def to_regexp
|
9
9
|
self
|
10
|
-
end
|
10
|
+
end
|
11
11
|
end
|
12
12
|
|
13
13
|
class MatchData
|
@@ -16,7 +16,7 @@ class MatchData
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def offset_before
|
19
|
-
offset(0)[0] -1
|
19
|
+
offset(0)[0] -1
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -4,7 +4,7 @@ module RSpec
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module RSpec::Sugar
|
7
|
-
module Matchers
|
7
|
+
module Matchers
|
8
8
|
class HaveAliases
|
9
9
|
|
10
10
|
attr_reader :method, :alias_methods, :cause
|
@@ -15,16 +15,16 @@ module RSpec::Sugar
|
|
15
15
|
@cause = []
|
16
16
|
end
|
17
17
|
|
18
|
-
def matches? obj, options={}
|
18
|
+
def matches? obj, options={}
|
19
19
|
if !obj.respond_to? method
|
20
20
|
cause << "Method ##{method} to alias does NOT exist"
|
21
21
|
return nil
|
22
22
|
end
|
23
|
-
|
24
|
-
alias_methods.each do |method|
|
25
|
-
cause << "Alias method ##{method} does NOT exist" if !is_alias? obj, alias_meth
|
26
|
-
end
|
27
|
-
cause.empty?
|
23
|
+
|
24
|
+
alias_methods.each do |method|
|
25
|
+
cause << "Alias method ##{method} does NOT exist" if !is_alias? obj, alias_meth
|
26
|
+
end
|
27
|
+
cause.empty?
|
28
28
|
end
|
29
29
|
|
30
30
|
def is_alias? obj, alias_meth
|
@@ -41,7 +41,7 @@ module RSpec::Sugar
|
|
41
41
|
|
42
42
|
def negative_failure_message
|
43
43
|
"Did not expect aliases to exist but, #{cause_msg}".gsub /NOT/, ''
|
44
|
-
end
|
44
|
+
end
|
45
45
|
end
|
46
46
|
|
47
47
|
def have_aliases method, *alias_methods
|
data/lib/sugar-high/string.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# multi_alias name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content
|
2
|
-
# :options => :after
|
3
|
-
#
|
2
|
+
# :options => :after
|
3
|
+
#
|
4
4
|
# create_xxx becomes new_xxx
|
5
5
|
# insert_into_xxx becomes inject_into_xxx and update_xxx
|
6
6
|
# read_xxx becomes xxx_content (overriding default :after action to insert at the X)
|
@@ -15,7 +15,7 @@ class Abc
|
|
15
15
|
multi_alias :_after_ => :kristian, :hello => :howdy
|
16
16
|
end
|
17
17
|
|
18
|
-
class Xyz
|
18
|
+
class Xyz
|
19
19
|
def hello_kristian
|
20
20
|
'hi'
|
21
21
|
end
|
@@ -23,46 +23,46 @@ class Xyz
|
|
23
23
|
multi_alias :kristian, :hello => :alloha
|
24
24
|
end
|
25
25
|
|
26
|
-
class Ged
|
26
|
+
class Ged
|
27
27
|
def kristian_hello
|
28
28
|
'hejsa'
|
29
29
|
end
|
30
|
-
|
31
|
-
multi_alias :_before_ => :kristian, :hejsa => :hello, :_direction_ => :reverse
|
32
|
-
end
|
30
|
+
|
31
|
+
multi_alias :_before_ => :kristian, :hejsa => :hello, :_direction_ => :reverse
|
32
|
+
end
|
33
33
|
|
34
34
|
class Plural
|
35
35
|
def monster
|
36
36
|
'monster'
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
alias_for :monster, :pluralize => true
|
40
|
-
end
|
40
|
+
end
|
41
41
|
|
42
42
|
class Plural2
|
43
43
|
def monster
|
44
44
|
'monster'
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
alias_for :monster, :beast, :pluralize => true
|
48
|
-
end
|
48
|
+
end
|
49
49
|
|
50
50
|
|
51
51
|
class Singular
|
52
52
|
def monsters
|
53
53
|
'monsters'
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
alias_for :monsters, :singularize => true
|
57
|
-
end
|
57
|
+
end
|
58
58
|
|
59
59
|
class Singular2
|
60
60
|
def monsters
|
61
61
|
'monsters'
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
alias_for :monsters, :beasts, :singularize => true
|
65
|
-
end
|
65
|
+
end
|
66
66
|
|
67
67
|
|
68
68
|
class AliasHash
|
@@ -74,9 +74,9 @@ class AliasHash
|
|
74
74
|
'monster'
|
75
75
|
end
|
76
76
|
|
77
|
-
|
77
|
+
|
78
78
|
alias_hash :monsters => :beasts, :singularize => true
|
79
|
-
end
|
79
|
+
end
|
80
80
|
|
81
81
|
|
82
82
|
class Wow
|
@@ -93,11 +93,11 @@ class Wow
|
|
93
93
|
end
|
94
94
|
}
|
95
95
|
end
|
96
|
-
multi_alias REGISTRATION_LINKS.merge(:_after_ => :link)
|
96
|
+
multi_alias REGISTRATION_LINKS.merge(:_after_ => :link)
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
describe "SugarHigh" do
|
100
|
-
describe "Arguments" do
|
100
|
+
describe "Arguments" do
|
101
101
|
context 'should alias :hello_kristian with :howdy_kristian ' do
|
102
102
|
it "should find alias" do
|
103
103
|
Abc.new.respond_to?(:howdy_kristian).should be_true
|
@@ -131,7 +131,7 @@ describe "SugarHigh" do
|
|
131
131
|
it "should find nice aliases from using alias_hash" do
|
132
132
|
ah = AliasHash.new
|
133
133
|
ah.beasts.should == 'monsters'
|
134
|
-
ah.beast.should == 'monster'
|
134
|
+
ah.beast.should == 'monster'
|
135
135
|
end
|
136
136
|
|
137
137
|
|
@@ -3,13 +3,13 @@ require 'sugar-high/arguments'
|
|
3
3
|
|
4
4
|
describe "SugarHigh" do
|
5
5
|
describe "Arguments" do
|
6
|
-
|
6
|
+
|
7
7
|
context 'Symbol' do
|
8
8
|
it "should return arg list with 'hello'" do
|
9
9
|
:hello.args.should == ['hello']
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
context 'String' do
|
14
14
|
it "should return arg list with 'hello'" do
|
15
15
|
:hello.args.should == ['hello']
|
@@ -28,23 +28,23 @@ describe "SugarHigh" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should return the :hello => 'abe' " do
|
31
|
-
[{:hi => :def}, 3, 4, :hi => 'abe'].last_arg.should == {:hi => 'abe'}
|
32
|
-
[{:hi => :def}, [3,4, :hi => 'abe']].last_arg.should == {:hi => 'abe'}
|
31
|
+
[{:hi => :def}, 3, 4, {:hi => 'abe'}].last_arg.should == {:hi => 'abe'}
|
32
|
+
[{:hi => :def}, [3,4, {:hi => 'abe'}]].last_arg.should == {:hi => 'abe'}
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
describe '#last_option' do
|
37
|
+
describe '#last_option' do
|
38
38
|
it "should return the last hash" do
|
39
|
-
[3,4, :x => 3, :y => 5].last_option.should == {:x => 3, :y => 5}
|
39
|
+
[3,4, {:x => 3, :y => 5}].last_option.should == {:x => 3, :y => 5}
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
describe "Last argument value" do
|
44
44
|
context 'Last arg with default hello' do
|
45
45
|
it "should return the arg value 'abe' " do
|
46
|
-
[3,4, :hi => 'abe'].last_arg_value(:hi => :def).should == 'abe'
|
47
|
-
[[3,4, :hi => 'abe']].last_arg_value(:hi => :def).should == 'abe'
|
46
|
+
[3,4, {:hi => 'abe'}].last_arg_value(:hi => :def).should == 'abe'
|
47
|
+
[[3,4, {:hi => 'abe'}]].last_arg_value(:hi => :def).should == 'abe'
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should return the arg value :def " do
|
@@ -55,7 +55,7 @@ describe "SugarHigh" do
|
|
55
55
|
[3,4].last_arg_value(:hi => :def).should == :def
|
56
56
|
end
|
57
57
|
end
|
58
|
-
end
|
58
|
+
end
|
59
59
|
end
|
60
60
|
|
61
61
|
describe "Last argument" do
|
@@ -66,8 +66,8 @@ describe "SugarHigh" do
|
|
66
66
|
|
67
67
|
it "should return the :hello => 'abe' " do
|
68
68
|
last_arg({:hi => :def}, 3,4, :hi => 'abe').should == {:hi => 'abe'}
|
69
|
-
last_arg({:hi => :def}, [3,4, :hi => 'abe']).should == {:hi => 'abe'}
|
70
|
-
end
|
69
|
+
last_arg({:hi => :def}, [3,4, {:hi => 'abe'}]).should == {:hi => 'abe'}
|
70
|
+
end
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -75,7 +75,7 @@ describe "SugarHigh" do
|
|
75
75
|
context 'Last arg with default hello' do
|
76
76
|
it "should return the arg value 'abe' " do
|
77
77
|
last_arg_value({:hi => :def}, 3,4, :hi => 'abe').should == 'abe'
|
78
|
-
last_arg_value({:hi => :def}, [3,4, :hi => 'abe']).should == 'abe'
|
78
|
+
last_arg_value({:hi => :def}, [3,4, {:hi => 'abe'}]).should == 'abe'
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should return the arg value :def " do
|
@@ -91,16 +91,16 @@ describe "SugarHigh" do
|
|
91
91
|
describe "Last option" do
|
92
92
|
context 'Last arg is Hash' do
|
93
93
|
it "should return the last hash" do
|
94
|
-
last_option(3,4, :x => 3, :y => 5).should == {:x => 3, :y => 5}
|
94
|
+
last_option(3,4, :x => 3, :y => 5).should == {:x => 3, :y => 5}
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
98
|
context 'Last arg is NOT Hash' do
|
99
99
|
it "should return an empty hash" do
|
100
|
-
last_option(3,4,3).should == {}
|
100
|
+
last_option(3,4,3).should == {}
|
101
101
|
end
|
102
102
|
end
|
103
|
-
end
|
103
|
+
end
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
@@ -4,7 +4,7 @@ require 'sugar-high/array'
|
|
4
4
|
describe "SugarHigh" do
|
5
5
|
describe "Array ext" do
|
6
6
|
|
7
|
-
describe '#to_symbols' do
|
7
|
+
describe '#to_symbols' do
|
8
8
|
it "should translate invalid array into empty array" do
|
9
9
|
[1, nil].to_symbols.should == []
|
10
10
|
end
|
@@ -14,90 +14,96 @@ describe "SugarHigh" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should translate nested array of numbers and strings into symbols only array, including numbers" do
|
17
|
-
[[1, 'blip', [3, "hello"]]].to_symbols_num.should == [:_1, :blip, :_3, :hello]
|
17
|
+
[[1, 'blip', [3, "hello"]]].to_symbols_num.should == [:_1, :blip, :_3, :hello]
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should translate nested array of numbers and strings into symbols only array, including numbers" do
|
21
|
-
[[1, 'blip', [1, "hello"]]].to_symbols_uniq.should include :blip, :hello
|
21
|
+
[[1, 'blip', [1, "hello"]]].to_symbols_uniq.should include :blip, :hello
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
24
24
|
|
25
|
-
describe '#to_symbols!' do
|
25
|
+
describe '#to_symbols!' do
|
26
26
|
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
27
27
|
x = [1, 'hello', 'blip', [3, "hello"]].to_symbols!
|
28
28
|
x.should include :hello, :blip
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "should translate invalid array into empty array" do
|
32
32
|
x = [1, nil].to_symbols!
|
33
33
|
x.should == []
|
34
|
-
end
|
34
|
+
end
|
35
35
|
end
|
36
|
-
|
37
|
-
describe '#to_strings' do
|
36
|
+
|
37
|
+
describe '#to_strings' do
|
38
38
|
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
39
39
|
[1, 'blip', [3, "hello"]].to_strings.should == ['blip', 'hello']
|
40
|
-
end
|
41
|
-
end
|
40
|
+
end
|
41
|
+
end
|
42
42
|
|
43
|
-
describe '#to_strings' do
|
43
|
+
describe '#to_strings' do
|
44
44
|
it "should translate invalid array into empty array" do
|
45
45
|
x = [1, nil].to_strings!
|
46
46
|
x.should == []
|
47
|
-
end
|
47
|
+
end
|
48
48
|
|
49
49
|
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
50
50
|
x = [1, 'blip', [3, "hello"]].to_strings!
|
51
51
|
x.should == ['blip', 'hello']
|
52
|
-
end
|
53
|
-
end
|
52
|
+
end
|
53
|
+
end
|
54
54
|
|
55
|
-
describe '#flat_uniq' do
|
55
|
+
describe '#flat_uniq' do
|
56
56
|
it "should flatten array, remove nils and make unique" do
|
57
57
|
[1, 'blip', ['blip', nil, 'c'], nil].flat_uniq.should == [1, 'blip', 'c']
|
58
|
-
end
|
58
|
+
end
|
59
59
|
|
60
60
|
it "should return empty list if called on nil" do
|
61
61
|
nil.flat_uniq.should == []
|
62
|
-
end
|
63
|
-
end
|
62
|
+
end
|
63
|
+
end
|
64
64
|
|
65
|
-
describe '#flat_uniq!' do
|
65
|
+
describe '#flat_uniq!' do
|
66
66
|
it "should translate invalid array into empty array" do
|
67
67
|
x = [1, nil].to_strings!
|
68
68
|
x.should == []
|
69
|
-
end
|
69
|
+
end
|
70
70
|
|
71
71
|
it "should flatten array, remove nils and make unique" do
|
72
72
|
x = [1, 'blip', ['blip', nil, 'c'], nil]
|
73
73
|
x.flat_uniq!
|
74
74
|
x.should == [1, 'blip', 'c']
|
75
75
|
end
|
76
|
-
end
|
76
|
+
end
|
77
77
|
|
78
|
-
describe '#sum' do
|
78
|
+
describe '#sum' do
|
79
79
|
it "should add elements in array" do
|
80
80
|
[1, 2, 3].extend(MathArray).sum.should == 6
|
81
|
-
end
|
81
|
+
end
|
82
82
|
end
|
83
83
|
|
84
|
-
describe '#mean' do
|
84
|
+
describe '#mean' do
|
85
85
|
it "should find mean of elements in array" do
|
86
86
|
[1, 2, 3].extend(MathArray).mean.should == 2
|
87
|
-
end
|
87
|
+
end
|
88
88
|
end
|
89
89
|
|
90
|
-
describe '#extract' do
|
90
|
+
describe '#extract' do
|
91
91
|
it "should call method on each element in array" do
|
92
92
|
["a", "ab", "abc"].extract(:size).extend(MathArray).mean.should == 2
|
93
|
-
end
|
94
|
-
end
|
93
|
+
end
|
94
|
+
end
|
95
95
|
|
96
|
-
describe '#none?' do
|
96
|
+
describe '#none?' do
|
97
97
|
it "should be none if no real values in array" do
|
98
98
|
[nil, nil].none?.should be_true
|
99
|
-
nil.none?.should be_true
|
100
|
-
end
|
101
|
-
end
|
99
|
+
nil.none?.should be_true
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#select!' do
|
104
|
+
arr = [1, 2, 3, 4]
|
105
|
+
arr.select!{|el| el % 2 == 0}
|
106
|
+
arr.should == [2, 4]
|
107
|
+
end
|
102
108
|
end
|
103
109
|
end
|