Module: Rearmed

Defined in:
lib/rearmed.rb,
lib/rearmed/version.rb,
lib/generators/rearmed/setup_generator.rb

Defined Under Namespace

Classes: BlockFoundError, BothArgAndBlockError, NoArgOrBlockGivenError, SetupGenerator

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.dig(collection, *values) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rearmed.rb', line 92

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



46
47
48
# File 'lib/rearmed.rb', line 46

def self.enabled_patches
  @enabled_patches
end

.enabled_patches=(val) ⇒ Object



42
43
44
# File 'lib/rearmed.rb', line 42

def self.enabled_patches=(val)
  @enabled_patches = val
end

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rearmed.rb', line 73

def self.natural_sort(array, options={})
  if block_given?
    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



69
70
71
# File 'lib/rearmed.rb', line 69

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

.only(hash, *keys) ⇒ Object



87
88
89
90
# File 'lib/rearmed.rb', line 87

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/rearmed.rb', line 58

def self.to_bool(str)
  if str =~ /^true$/
    true
  elsif str =~ /^false$/
    false
  else
    #raise(ArgumentError.new "incorrect element #{str}")
    nil
  end
end

.valid_float?(str) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rearmed.rb', line 54

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

.valid_integer?(str) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rearmed.rb', line 50

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