Sha256: cc05c0f25168a1515b43b60f507b54fc03aee7b9eda7643bad99d2c00e2581da

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

module Usable
  class ModExtender
    attr_reader :name
    attr_accessor :copy, :mod, :options, :unwanted

    def initialize(mod, options = {})
      @mod = mod
      @options = options
      @options[:method] ||= :include
      @copy = mod
      @name = mod.name
      @unwanted = options[:only] ? @copy.instance_methods - Array(options[:only]) : []
      if @unwanted.any?
        @copy = @copy.dup
      end
    end

    # @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
    def call(target)
      unwanted.each do |method_name|
        copy.send :remove_method, method_name
      end
      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
      target.send options[:method], copy
    end

    def mod_name
      if name
        name.split('::').last
      else
        "UsableMod#{Time.now.strftime('%s')}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
usable-3.2.0 lib/usable/mod_extender.rb
usable-3.1.0 lib/usable/mod_extender.rb
usable-3.0.0 lib/usable/mod_extender.rb
usable-2.2.1 lib/usable/mod_extender.rb
usable-2.2.0 lib/usable/mod_extender.rb