Class: Pachinko

Inherits:
Object
  • Object
show all
Defined in:
lib/pachinko.rb

Overview

Example format of a patch: class YourPatch < Pachinko

# name: any string naming this patch
def name
  'Example Patch Creating FabricatedClass'
end
# relevant?: a method that returns true only if the patch needs to be applied
def relevant?
  !defined?(FabricatedClass)
end
# PATCH: A constant which contains a lambda which applies the patch
# Reason for this is that you can't reopen classes in methods...
PATCH = ->{
  class ::FabricatedClass; end
}

end

Constant Summary collapse

PATCH_PRIORITY =

Allows you to prioritize certain patch files first, such as core object patches

lambda { |p| p =~ /\/(hash|string|array|object)/i ? ' ' : p }

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.last_patchObject

Returns the value of attribute last_patch.



39
40
41
# File 'lib/pachinko.rb', line 39

def last_patch
  @last_patch
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



24
25
26
# File 'lib/pachinko.rb', line 24

def results
  @results
end

Class Method Details

.development_mode?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pachinko.rb', line 45

def development_mode?
  defined?(Rails) && defined?(::Rails.env) && ::Rails.env.development?
end

.last_resultsObject



55
56
57
# File 'lib/pachinko.rb', line 55

def last_results
  last_patch.results
end

.mute?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pachinko.rb', line 34

def self.mute?
  ENV['MUTE_PACHINKO']
end

.run(*args) ⇒ Object



41
42
43
# File 'lib/pachinko.rb', line 41

def run(*args)
  new.run(*args)
end

.run_if_neededObject



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

def run(*args)
  new.run(*args)
end

.test_mode?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/pachinko.rb', line 48

def test_mode?
  if defined?(Rails) && defined?(::Rails.env)
    ::Rails.env.test?
  else
    true
  end
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/pachinko.rb', line 30

def applied?
  @applied
end

#irrelevant?Boolean

Overridable in subclasses, although not recommended Should return true if the patch was applied (which in most cases means it’s also no longer applicable)

Returns:

  • (Boolean)


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

def irrelevant?
  !relevant?
end

#nameObject

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/pachinko.rb', line 63

def name
  raise NotImplementedError, "Your patch doesn't define a 'name' method... please override in your patch class"
end

#relevant?Boolean

This method returns a boolean that actually checks the mechanics of whatever the patch is supposed to fix. It should return true if the patch still needs to be applied, and false after it has been applied or if it doesn’t need to be applied.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def relevant?
  raise NotImplementedError, "Your patch doesn't define a 'relevant?' method which tests whether it is necessary... please override in your patch class"
end

#run(force_plain_output = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pachinko.rb', line 79

def run(force_plain_output = false)
  @force_plain_output = force_plain_output
  @success = false
  @applied = false
  output_msgs = []
  output_msgs_warning = []
  if relevant?
    apply
    @applied = true
    # Check to see if the patch is no longer needed (i.e., the test is valid, and the patch worked)
    if irrelevant?
      output_msgs << success_message
      @success = true
    else
      output_msgs_warning << relevancy_assertion_wrong_message
    end
  else
    output_msgs_warning << patch_not_applied_message
  end
  if !output_msgs.empty? && (Pachinko.development_mode? || force_plain_output)
    log_success output_msgs.join("\n") unless Pachinko.mute?
  end
  if !output_msgs_warning.empty?
    log_warning output_msgs_warning.join("\n")
  end
  Pachinko.last_patch = self
  @results = output_msgs_warning | output_msgs
  self
end

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/pachinko.rb', line 26

def success?
  @success
end