Sha256: 849d35107ddb978643af13baf6e4601a100bb4456c1ed465b57d277e03b0c732

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require "ostruct"
require "delegate"
require "usable/version"

module Usable

  autoload :ModExtender, 'usable/mod_extender'
  autoload :Config, 'usable/config'

  def usable_config
    @usable_config ||= Config.new
  end
  attr_writer :usable_config

  # @description Configures the +available_methods+ of a module using the given options or block and then includes it on
  #   the target class. Checks if there is a module named UsableSpec within the given mods namespace and uses the instance of
  #   methods of that as the +available_methods+
  #
  # @example
  #
  #   class Example
  #     extend Usable
  #     usable VersionKit, only: :save_version
  #   end
  #
  # @note Hides methods
  # @note We include the primary mod when there is a UsableSpec set because any instance method defined on the mod are not
  #   configurable and should therefore takes precedence over those defined in the UsableSpec
  def usable(mod, options = {})
    options.each { |k, v| usable_config.public_send "#{k}=", v }
    yield usable_config if block_given?
    mod_ext = ModExtender.new mod, usable_config
    usable! mod_ext.call
    usable! mod if mod_ext.has_spec?
  end

  # @description Directly include a module whose methods you want made available in +usable_config.available_methods+
  def usable!(mod)
    usable_config.modules << mod
    send :include, mod
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
usable-1.0.0 lib/usable.rb