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/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/lib/sugar-high/alias.rb
CHANGED
@@ -5,53 +5,53 @@ require 'sugar-high/array'
|
|
5
5
|
require 'active_support/inflector'
|
6
6
|
|
7
7
|
class Module
|
8
|
-
|
8
|
+
|
9
9
|
# multi_alias name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content
|
10
|
-
# :options => :after
|
11
|
-
#
|
10
|
+
# :options => :after
|
11
|
+
#
|
12
12
|
# create_xxx becomes new_xxx
|
13
13
|
# insert_into_xxx becomes inject_into_xxx and update_xxx
|
14
14
|
# read_xxx becomes xxx_content (overriding default :after action to insert at the X)
|
15
|
-
|
15
|
+
|
16
16
|
def multi_alias *args
|
17
17
|
name = case args.first
|
18
|
-
when Symbol, String
|
18
|
+
when Symbol, String
|
19
19
|
args.first.to_s
|
20
|
-
when Hash
|
20
|
+
when Hash
|
21
21
|
# default is :after
|
22
22
|
args.first[:_before_] ? :before : :after
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
if name.kind_of? Symbol
|
26
26
|
config_options = name
|
27
27
|
options = args.first
|
28
28
|
name = options[:"_#{name}_"]
|
29
29
|
else
|
30
30
|
options = args[1]
|
31
|
-
end
|
32
|
-
|
31
|
+
end
|
32
|
+
|
33
33
|
raise ArgumentError, "Name of method pattern to alias not specified. Please pass name as either first argument or as :_before_ or :_after_ option" if !name
|
34
34
|
|
35
35
|
options.delete(:_after_)
|
36
36
|
options.delete(:_before_)
|
37
37
|
direction = options.delete(:_direction_)
|
38
|
-
|
38
|
+
|
39
39
|
options = options.hash_revert if direction == :reverse
|
40
|
-
|
40
|
+
|
41
41
|
options.each_pair do |original, aliases|
|
42
42
|
alias_methods name.to_sym, original, [aliases].flatten, config_options
|
43
|
-
end
|
43
|
+
end
|
44
44
|
end
|
45
45
|
|
46
46
|
def alias_methods name, original, aliases, config_options
|
47
|
-
aliases.each do |alias_name|
|
47
|
+
aliases.each do |alias_name|
|
48
48
|
new_alias = make_name(name, alias_name.to_s, config_options)
|
49
49
|
original_name = make_name(name, original.to_s, config_options)
|
50
|
-
begin
|
50
|
+
begin
|
51
51
|
alias_method new_alias, original_name
|
52
52
|
rescue
|
53
53
|
raise ArgumentError, "Error creating alias for ##{original_name} with ##{new_alias}"
|
54
|
-
end
|
54
|
+
end
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -59,7 +59,7 @@ class Module
|
|
59
59
|
pluralize = hash.delete(:pluralize)
|
60
60
|
singularize = hash.delete(:singularize)
|
61
61
|
# option = :pluralize => pluralize, :singularize => singularize
|
62
|
-
|
62
|
+
|
63
63
|
hash.each_pair do |original, alias_meth|
|
64
64
|
alias_for original, alias_meth
|
65
65
|
alias_for original.to_s.singularize, alias_meth.to_s.singularize, :singularize => true if singularize
|
@@ -85,7 +85,7 @@ class Module
|
|
85
85
|
protected
|
86
86
|
|
87
87
|
def make_name name, alias_name, config_options
|
88
|
-
return alias_name.gsub(/X/, name.to_s) if alias_name =~ /X/
|
88
|
+
return alias_name.gsub(/X/, name.to_s) if alias_name =~ /X/
|
89
89
|
case config_options
|
90
90
|
when :before
|
91
91
|
"#{name}_#{alias_name}"
|
data/lib/sugar-high/arguments.rb
CHANGED
@@ -2,13 +2,13 @@ class Array
|
|
2
2
|
def args
|
3
3
|
flatten.map{|a| a.args}.flatten
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
def last_option
|
7
7
|
default = self.last_arg
|
8
8
|
last = self.flatten.last
|
9
|
-
last.kind_of?(Hash) ? last : default
|
10
|
-
end
|
11
|
-
|
9
|
+
last.kind_of?(Hash) ? last : default
|
10
|
+
end
|
11
|
+
|
12
12
|
def last_arg default = {}
|
13
13
|
last = self.flatten.last
|
14
14
|
last.kind_of?(Hash) ? last : default
|
@@ -20,7 +20,7 @@ class Array
|
|
20
20
|
key = default.keys.first
|
21
21
|
return default[key] if !last.kind_of? Hash
|
22
22
|
last[key] ? last[key] : default[key]
|
23
|
-
end
|
23
|
+
end
|
24
24
|
end
|
25
25
|
|
26
26
|
class Symbol
|
@@ -38,7 +38,7 @@ end
|
|
38
38
|
def last_option *args
|
39
39
|
default = last_arg({}, *args)
|
40
40
|
last = args.flatten.last
|
41
|
-
last.kind_of?(Hash) ? last : default
|
41
|
+
last.kind_of?(Hash) ? last : default
|
42
42
|
end
|
43
43
|
|
44
44
|
def last_arg default, *args
|
data/lib/sugar-high/array.rb
CHANGED
@@ -51,19 +51,19 @@ class Array
|
|
51
51
|
def to_strings!
|
52
52
|
self.flatten!
|
53
53
|
self.select_labels!.map!(&:to_s)
|
54
|
-
end
|
54
|
+
end
|
55
55
|
|
56
56
|
def to_strings
|
57
57
|
self.flatten.select_labels.map(&:to_s)
|
58
|
-
end
|
58
|
+
end
|
59
59
|
|
60
60
|
def to_filenames
|
61
61
|
self.to_strings.map(&:underscore)
|
62
|
-
end
|
62
|
+
end
|
63
63
|
|
64
64
|
def to_filenames!
|
65
65
|
self.to_strings!.map!(&:underscore)
|
66
|
-
end
|
66
|
+
end
|
67
67
|
|
68
68
|
def to_paths
|
69
69
|
self.map(&:path)
|
@@ -76,32 +76,32 @@ class Array
|
|
76
76
|
|
77
77
|
def file_join
|
78
78
|
File.join(*self.flatten)
|
79
|
-
end
|
79
|
+
end
|
80
80
|
|
81
81
|
def to_files
|
82
82
|
self.map{|fp| fp.path.to_file }
|
83
|
-
self.extend FilesArray
|
83
|
+
self.extend FilesArray
|
84
84
|
end
|
85
85
|
|
86
86
|
def to_files!
|
87
87
|
self.map!{|fp| fp.path.to_file }
|
88
|
-
self.extend FilesArray
|
88
|
+
self.extend FilesArray
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
def none?
|
92
92
|
self.flatten.compact.empty?
|
93
|
-
end unless [].respond_to? :none?
|
94
|
-
|
93
|
+
end unless [].respond_to? :none?
|
94
|
+
|
95
95
|
def flat_uniq
|
96
96
|
self.flatten.compact.uniq
|
97
|
-
end
|
97
|
+
end
|
98
98
|
|
99
99
|
def flat_uniq!
|
100
100
|
self.flatten!
|
101
101
|
self.compact!
|
102
102
|
self.uniq!
|
103
103
|
self
|
104
|
-
end
|
104
|
+
end
|
105
105
|
|
106
106
|
def extract(sym)
|
107
107
|
map { |e| e.send(sym) }
|
@@ -110,7 +110,11 @@ class Array
|
|
110
110
|
# Repeat overall-used method here, to use it without Rails
|
111
111
|
def extract_options!
|
112
112
|
last.is_a?(::Hash) ? pop : {}
|
113
|
-
end unless [].respond_to? :extract_options!
|
113
|
+
end unless [].respond_to? :extract_options!
|
114
|
+
|
115
|
+
def select! &block
|
116
|
+
replace select &block
|
117
|
+
end unless [].respond_to? :select!
|
114
118
|
end
|
115
119
|
|
116
120
|
module MathArray
|
@@ -120,22 +124,22 @@ module MathArray
|
|
120
124
|
|
121
125
|
def mean
|
122
126
|
(size > 0) ? sum.to_f / size : 0
|
123
|
-
end
|
124
|
-
end
|
127
|
+
end
|
128
|
+
end
|
125
129
|
|
126
130
|
module FilesArray
|
127
131
|
def delete_all!
|
128
|
-
self.each do |f|
|
132
|
+
self.each do |f|
|
129
133
|
f.delete! if f.kind_of?(File)
|
130
134
|
end
|
131
135
|
end
|
132
136
|
end
|
133
137
|
|
134
|
-
class NilClass
|
135
|
-
def flat_uniq
|
138
|
+
class NilClass
|
139
|
+
def flat_uniq
|
136
140
|
[]
|
137
141
|
end
|
138
|
-
|
142
|
+
|
139
143
|
def none?
|
140
144
|
true
|
141
145
|
end
|
data/lib/sugar-high/blank.rb
CHANGED
data/lib/sugar-high/delegate.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
module Enumerable
|
2
|
-
def only_kinds_of? *kinds
|
3
|
-
all?{|a| a.any_kind_of? *kinds }
|
2
|
+
def only_kinds_of? *kinds
|
3
|
+
all?{|a| a.any_kind_of? *kinds }
|
4
4
|
end
|
5
5
|
|
6
6
|
def only_labels?
|
7
|
-
all?{|a| a.kind_of_label? }
|
7
|
+
all?{|a| a.kind_of_label? }
|
8
8
|
end
|
9
9
|
|
10
10
|
def only_numbers?
|
11
|
-
all?{|a| a.kind_of_number? }
|
11
|
+
all?{|a| a.kind_of_number? }
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def select_kinds_of *kinds
|
15
15
|
select{|a| a.any_kind_of? *kinds }
|
16
16
|
end
|
@@ -61,19 +61,19 @@ module Enumerable
|
|
61
61
|
self
|
62
62
|
end
|
63
63
|
|
64
|
-
def select_only type
|
64
|
+
def select_only type
|
65
65
|
const = type.kind_of_label? ? "#{type.to_s.camelize}".constantize : type
|
66
66
|
select{|a| a.kind_of? const}
|
67
67
|
end
|
68
68
|
|
69
|
-
def select_only! type
|
69
|
+
def select_only! type
|
70
70
|
const = type.kind_of_label? ? "#{type.to_s.camelize}".constantize : type
|
71
71
|
select!{|a| a.kind_of? const}
|
72
72
|
self
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def all_kinds
|
76
|
-
map do |a|
|
76
|
+
map do |a|
|
77
77
|
case a
|
78
78
|
when Kinds
|
79
79
|
a.kinds
|
data/lib/sugar-high/file_ext.rb
CHANGED
data/lib/sugar-high/hash.rb
CHANGED
@@ -9,18 +9,18 @@ class Hash
|
|
9
9
|
rewritten_hash
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def hash_revert
|
14
14
|
r = Hash.new {|h,k| h[k] = []}
|
15
15
|
each {|k,v| r[v] << k}
|
16
16
|
r
|
17
|
-
end
|
18
|
-
|
17
|
+
end
|
18
|
+
|
19
19
|
def try_keys *keys
|
20
20
|
option = last_option keys
|
21
|
-
keys.flatten.each do |key|
|
21
|
+
keys.flatten.each do |key|
|
22
22
|
return self[key] if self[key]
|
23
|
-
end
|
23
|
+
end
|
24
24
|
return option[:default] if option[:default]
|
25
25
|
nil
|
26
26
|
end
|
data/lib/sugar-high/includes.rb
CHANGED
@@ -9,29 +9,29 @@ class Module
|
|
9
9
|
module_names.to_symbols.each do |name|
|
10
10
|
class_eval %{
|
11
11
|
#{name} \#{name.to_s.camelize}
|
12
|
-
}
|
12
|
+
}
|
13
13
|
end
|
14
|
-
end
|
14
|
+
end
|
15
15
|
|
16
16
|
def #{name}_from module_name, *sub_modules
|
17
17
|
sub_modules.to_symbols.each do |name|
|
18
18
|
class_eval %{
|
19
19
|
#{name} \#{module_name.to_s.camelize}::\#{name.to_s.camelize}
|
20
|
-
}
|
20
|
+
}
|
21
21
|
end
|
22
|
-
end
|
22
|
+
end
|
23
23
|
}
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def includes_and_extends *module_names
|
27
27
|
includes module_names
|
28
28
|
extends module_names
|
29
|
-
end
|
29
|
+
end
|
30
30
|
|
31
31
|
def includes_and_extends_from module_name, *sub_modules
|
32
32
|
includes module_name, *sub_modules
|
33
33
|
extends module_name, *sub_modules
|
34
|
-
end
|
34
|
+
end
|
35
35
|
|
36
36
|
alias_method :extends_and_includes, :includes_and_extends
|
37
37
|
alias_method :extends_and_includes_from, :includes_and_extends_from
|
data/lib/sugar-high/math.rb
CHANGED
data/lib/sugar-high/methods.rb
CHANGED
data/lib/sugar-high/module.rb
CHANGED
@@ -13,16 +13,16 @@ def modules *module_names, &block
|
|
13
13
|
module #{name.to_s.camelize}
|
14
14
|
#{yield block if block}
|
15
15
|
end
|
16
|
-
}
|
16
|
+
}
|
17
17
|
end
|
18
|
-
end
|
18
|
+
end
|
19
19
|
|
20
20
|
def nested_modules *module_names, &block
|
21
|
-
module_names.flatten.inject([]) do |res, name|
|
21
|
+
module_names.flatten.inject([]) do |res, name|
|
22
22
|
res << %{
|
23
23
|
module #{name.to_s.camelize}
|
24
24
|
#{yield block if block}
|
25
|
-
end}
|
25
|
+
end}
|
26
26
|
end.flatten.join("\n")
|
27
27
|
end
|
28
28
|
|
data/lib/sugar-high/numeric.rb
CHANGED
@@ -6,17 +6,17 @@ module NumericCheckExt
|
|
6
6
|
|
7
7
|
def check_numeric! arg
|
8
8
|
raise ArgumentError, "Argument must be Numeric" if !is_numeric? arg
|
9
|
-
end
|
9
|
+
end
|
10
10
|
end
|
11
11
|
|
12
|
-
module NumberDslExt
|
12
|
+
module NumberDslExt
|
13
13
|
def thousand
|
14
14
|
self * 1000
|
15
|
-
end
|
15
|
+
end
|
16
16
|
|
17
17
|
def hundred
|
18
18
|
self * 100
|
19
|
-
end
|
19
|
+
end
|
20
20
|
end
|
21
21
|
|
22
22
|
[Float, Numeric].each do |mod|
|