class Usable::ModExtender

Attributes

copy[RW]
mod[RW]
name[R]
options[RW]
unwanted[RW]

Public Class Methods

new(mod, options = {}) click to toggle source
# File lib/usable/mod_extender.rb, line 6
def initialize(mod, options = {})
  @mod = mod
  @options = options
  @options[:method] ||= :include
  @copy = mod
  @name = mod.name
  @unwanted = find_unwanted_methods(options[:only])
  if @unwanted.any?
    @copy = @copy.dup
  end
end

Public Instance Methods

call(target) click to toggle source

@description Directly include a module whose methods you want made available in usables.available_methods

Gives the module a name when including so that it shows up properly in the list of ancestors
# File lib/usable/mod_extender.rb, line 20
def call(target)
  unwanted.each(&method(:remove_from_module))
  if copy.name.nil?
    const_name = "#{mod_name}Used"
    target.send :remove_const, const_name if target.const_defined? const_name, false
    target.const_set const_name, copy
  end
  target.usables.add_module copy if target.respond_to?(:usables)
  target.send options[:method], copy
end
find_unwanted_methods(only) click to toggle source
# File lib/usable/mod_extender.rb, line 39
def find_unwanted_methods(only)
  return [] unless only
  if :constants == only
    @copy.instance_methods
  else
    @copy.instance_methods - Array(only)
  end
end
mod_name() click to toggle source
# File lib/usable/mod_extender.rb, line 31
def mod_name
  if name
    name.split('::').last
  else
    "UsableMod#{Time.now.strftime('%s')}"
  end
end
remove_from_module(method_name) click to toggle source
# File lib/usable/mod_extender.rb, line 48
def remove_from_module(method_name)
  begin
    copy.send :remove_method, method_name
  rescue NameError => e
    # Expected sometimes, like it could be raised trying to remove a superclass method
    Usable.logger.debug("#{e.class}: #{e.message}")
  end
  # Block access to superclass method, and prevent it from being copied to the target
  copy.send :undef_method, method_name if copy.instance_methods.include?(method_name)
end