Module: Rearmed
- Defined in:
- lib/rearmed.rb,
lib/rearmed/methods.rb,
lib/rearmed/version.rb,
lib/rearmed/exceptions.rb,
lib/generators/rearmed/setup_generator.rb
Defined Under Namespace
Modules: Exceptions
Classes: SetupGenerator
Constant Summary
collapse
- VERSION =
"1.2.1"
Class Method Summary
collapse
Class Method Details
.dig(collection, *values) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/rearmed/methods.rb', line 3
def self.dig(collection, *values)
current_val = nil
current_collection = collection
values.each_with_index do |val,i|
if i+1 == values.length
if (current_collection.is_a?(Array) && val.is_a?(Integer)) || (current_collection.is_a?(Hash) && ['String','Symbol'].include?(val.class.name))
current_val = current_collection[val]
else
current_val = nil
end
elsif current_collection.is_a?(Array)
if val.is_a?(Integer)
current_collection = current_collection[val]
next
else
current_val = nil
break
end
elsif current_collection.is_a?(Hash)
if ['Symbol','String'].include?(val.class.name)
current_collection = current_collection[val]
next
else
current_val = nil
break
end
else
current_val = nil
break
end
end
return current_val
end
|
.enabled_patches ⇒ Object
14
15
16
|
# File 'lib/rearmed.rb', line 14
def self.enabled_patches
@enabled_patches
end
|
.enabled_patches=(val) ⇒ Object
10
11
12
|
# File 'lib/rearmed.rb', line 10
def self.enabled_patches=(val)
@enabled_patches = val
end
|
.join(hash, delimiter = ', ', &block) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rearmed/methods.rb', line 38
def self.join(hash, delimiter=', ', &block)
unless block_given?
block = -> (k,v) {
"#{k}: #{v}"
}
end
str = ""
hash.each_with_index do |(k,v), i|
val = block.call(k,v)
unless val.is_a?(String)
val = val.to_s
end
str << val
if i+1 < hash.length
str << delimiter
end
end
return str
end
|
.natural_sort(array, options = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/rearmed/methods.rb', line 66
def self.natural_sort(array, options={})
if block_given?
Rearmed::Exceptions::BlockFoundError
else
array.sort do |a,b|
if options[:reverse] == true
self.naturalize_str(b.to_s) <=> self.naturalize_str(a.to_s)
else
self.naturalize_str(a.to_s) <=> self.naturalize_str(b.to_s)
end
end
end
end
|
.natural_sort_by(array) ⇒ Object
62
63
64
|
# File 'lib/rearmed/methods.rb', line 62
def self.natural_sort_by(array)
array.sort_by{|x| self.naturalize_str(yield(x))}
end
|
.only(hash, *keys) ⇒ Object
80
81
82
83
|
# File 'lib/rearmed/methods.rb', line 80
def self.only(hash, *keys)
keys.map!{|key| hash.convert_key(key)} if hash.respond_to?(:convert_key, true)
keys.each_with_object(hash.class.new){|k, new_hash| new_hash[k] = hash[k] if hash.has_key?(k)}
end
|
.to_bool(str) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/rearmed/methods.rb', line 85
def self.to_bool(str)
if str =~ /^(true|1|t|T)$/
true
elsif str =~ /^(false|0|f|F)$/
false
else
nil
end
end
|
.valid_float?(str) ⇒ Boolean
99
100
101
|
# File 'lib/rearmed/methods.rb', line 99
def self.valid_float?(str)
str =~ /(^(\d+)(\.)?(\d+)?$)|(^(\d+)?(\.)(\d+)$)/ ? true : false
end
|
.valid_integer?(str) ⇒ Boolean
95
96
97
|
# File 'lib/rearmed/methods.rb', line 95
def self.valid_integer?(str)
str =~ /^\d*$/ ? true : false
end
|