Class: RedmineExtensions::PatchManager::EasyPatch

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_klass_to_patch, patching_module, options = {}) ⇒ EasyPatch

Returns a new instance of EasyPatch.



362
363
364
365
# File 'lib/redmine_extensions/patch_manager.rb', line 362

def initialize(original_klass_to_patch, patching_module, options = {})
  @original_klass_to_patch, @patching_module, @options = original_klass_to_patch, patching_module, options
  all_patching_modules << patching_module
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



360
361
362
# File 'lib/redmine_extensions/patch_manager.rb', line 360

def options
  @options
end

#original_klass_to_patchObject

Returns the value of attribute original_klass_to_patch.



360
361
362
# File 'lib/redmine_extensions/patch_manager.rb', line 360

def original_klass_to_patch
  @original_klass_to_patch
end

#patching_moduleObject

Returns the value of attribute patching_module.



360
361
362
# File 'lib/redmine_extensions/patch_manager.rb', line 360

def patching_module
  @patching_module
end

Class Method Details

.all_patching_modulesObject



352
353
354
# File 'lib/redmine_extensions/patch_manager.rb', line 352

def self.all_patching_modules
  @@all_patching_modules ||= []
end

Instance Method Details

#all_patching_modulesObject



356
357
358
# File 'lib/redmine_extensions/patch_manager.rb', line 356

def all_patching_modules
  self.class.all_patching_modules
end

#apply_patchObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/redmine_extensions/patch_manager.rb', line 375

def apply_patch
  return if (cond = @options[:if]) && cond.respond_to?(:call) && !cond.call

  pm_klass = easy_constantize(patching_module)
  # pm_klass.class_eval { unloadable }

  oktp_klass = easy_constantize(original_klass_to_patch)

  if oktp_klass.include?(pm_klass)
    puts "Patch (#{oktp_klass} #{pm_klass}) is already included!"
  else
    if @options[:prepend]
      oktp_klass.prepend pm_klass
    else
      oktp_klass.include pm_klass
    end
  end
rescue NameError => e
  patch_location = Module.const_source_location(patching_module)
  raise NameError, "#{e.message} in #{patch_location}"
end

#easy_constantize(name) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/redmine_extensions/patch_manager.rb', line 397

def easy_constantize(name)
  name.constantize
rescue NameError
  if RedmineExtensions::PatchManager.patches_locations.has_key?(name)
    RedmineExtensions::PatchManager.with_reloading_code do
      load RedmineExtensions::PatchManager.patches_locations[patching_module]
    end

    name.constantize
  else
    raise
  end
end

#inspectObject



371
372
373
# File 'lib/redmine_extensions/patch_manager.rb', line 371

def inspect
  to_s
end

#to_sObject



367
368
369
# File 'lib/redmine_extensions/patch_manager.rb', line 367

def to_s
  "#{@original_klass_to_patch} <= #{@patching_module}"
end