Module: Rearmed

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

Defined Under Namespace

Modules: Enumerable, Exceptions, Hash, String

Constant Summary collapse

VERSION =
"2.0.1"

Class Method Summary collapse

Class Method Details

.apply_patches!Object



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

def self.apply_patches!
  if @applied 
    raise ::Rearmed::Exceptions::PatchesAlreadyAppliedError.new
  else
    patches_folder = File.expand_path('../rearmed/monkey_patches', __FILE__)
    Dir[File.join(patches_folder, '*.rb')].each do |filename| 
      require filename
    end

    @applied = true 
  end
end

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

def self.dig(collection, *values)
  if collection.is_a?(::Hash) || collection.is_a?(::Array)
    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))
          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 [::String, ::Symbol].include?(val.class)
          current_collection = current_collection[val]
          next
        else
          current_val = nil
          break
        end
      else
        current_val = nil
        break 
      end
    end

    return current_val
  else
    raise TypeError.new("Invalid object passed to #{__method__}, must be a Hash or Array")
  end
end

.enabled_patchesObject



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

def self.enabled_patches
  @enabled_patches
end

.enabled_patches=(val) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rearmed.rb', line 22

def self.enabled_patches=(val)
  if @applied
    raise ::Rearmed::Exceptions::PatchesAlreadyAppliedError.new
  else
    if [nil, {}].include?(val)
      @enabled_patches = Marshal.load(Marshal.dump(DEFAULT_PATCHES)) 
    elsif val == :all
      @enabled_patches = val
    elsif val.is_a?(::Hash)
      @enabled_patches = {}

      DEFAULT_PATCHES.keys.each do |k|
        methods = val[k] || val[k.to_sym]
        if methods
          if methods.is_a?(::Hash) || methods == true
            @enabled_patches[k] = methods
          else
            raise TypeError.new('Invalid value within the hash passed to Rearmed.enabled_patches=')
          end
        else
          @enabled_patches[k] = {}
        end
      end
    else
      raise TypeError.new('Invalid value passed to Rearmed.enabled_patches=')
    end
  end
end