Sha256: 5b4b6cfebedb2abcea633764de3b6f2a150485585227044a10e52c12df6a4dca

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require "ostruct"
require "usable/version"

module Usable
  def usable_config
    @usable_config ||= Config.new
  end

  alias_method :config, :usable_config unless method_defined?(:config)

  def usable(mod, options = {})
    options.each { |k, v| usable_config.public_send "#{k}=", v }
    if block_given?
      yield usable_config
    end
    wrapped_mod = spec(mod).dup
    wrapped_mod.prepend build_null_mod(wrapped_mod)
    usable_config.modules[mod] = wrapped_mod
    if has_spec?(mod)
      send :include, mod
    else
      send :include, usable_config.modules[mod]
    end
  end

  # @description Stub out any "unwanted" methods
  def build_null_mod(mod)
    unwanted = usable_config.only ? mod.instance_methods - Array(usable_config.only) : []
    Module.new do
      unwanted.each do |method_name|
        define_method(method_name) { |*| }
      end
    end
  end

  def has_spec?(mod)
    mod.const_defined?(:Spec)
  end

  def spec(mod)
    if has_spec?(mod)
      mod.const_get(:Spec)
    else
      mod
    end
  end

  class Config < OpenStruct
    def available_methods
      modules.each_with_object({}) do |(_, mod_copy), result|
        mod_copy.instance_methods.each do |method_name|
          result[method_name] = mod_copy.instance_method method_name
        end
      end
    end

    def modules
      @modules ||= {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
usable-0.2.0 lib/usable.rb