Module: Rearmed

Defined in:
lib/rearmed.rb,
lib/rearmed/methods.rb,
lib/rearmed/version.rb,
lib/rearmed/exceptions.rb

Defined Under Namespace

Modules: Exceptions

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Class Method Details

.casecmp?(str1, str2) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
# File 'lib/rearmed/methods.rb', line 3

def self.casecmp?(str1, str2)
  if str1.is_a?(String) && str2.is_a?(String)
    str1.casecmp(str2) == 0
  end
end

.dig(collection, *values) ⇒ Object



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
37
38
39
40
41
42
# File 'lib/rearmed/methods.rb', line 9

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_patchesObject



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

.hash_compact(hash) ⇒ Object



44
45
46
# File 'lib/rearmed/methods.rb', line 44

def self.hash_compact(hash)
  hash.reject{|_, value| value.nil?}
end

.hash_join(hash, delimiter = ', ', &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rearmed/methods.rb', line 48

def self.hash_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

.hash_only(hash, *keys) ⇒ Object



70
71
72
73
# File 'lib/rearmed/methods.rb', line 70

def self.hash_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

.hash_to_struct(hash) ⇒ Object



75
76
77
# File 'lib/rearmed/methods.rb', line 75

def self.hash_to_struct(hash)
  Struct.new(*hash.keys).new(*hash.values)
end

.natural_sort(collection, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rearmed/methods.rb', line 83

def self.natural_sort(collection, options={})
  if block_given?
    Rearmed::Exceptions::BlockFoundError
  else
    collection.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(collection) ⇒ Object



79
80
81
# File 'lib/rearmed/methods.rb', line 79

def self.natural_sort_by(collection)
  collection.sort_by{|x| self.naturalize_str(yield(x))}
end

.select_map(collection) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/rearmed/methods.rb', line 97

def self.select_map(collection)
  if block_given?
    collection.select{|x| yield(x) }.map{|x| yield(x) }
  else
    collection.select.map
  end
end

.to_bool(str) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rearmed/methods.rb', line 105

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/rearmed/methods.rb', line 119

def self.valid_float?(str)
  str =~ /(^(\d+)(\.)?(\d+)?$)|(^(\d+)?(\.)(\d+)$)/ ? true : false
end

.valid_integer?(str) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rearmed/methods.rb', line 115

def self.valid_integer?(str)
  str =~ /^\d*$/ ? true : false
end