Module: Usable

Defined in:
lib/usable.rb,
lib/usable/config.rb,
lib/usable/version.rb,
lib/usable/mod_extender.rb

Defined Under Namespace

Classes: Config, ModExtender

Constant Summary collapse

VERSION =
"1.1.1".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usable_configObject



10
11
12
# File 'lib/usable.rb', line 10

def usable_config
  @usable_config ||= Config.new
end

Instance Method Details

#usable(mod, options = {}) {|usable_config| ... } ⇒ ModExtender

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

Returns containing the original and modified module.

Examples:


class Example
  extend Usable
  usable VersionKit, only: :save_version
end

Yields:

Returns:

  • (ModExtender)

    containing the original and modified module



30
31
32
33
34
35
36
37
# File 'lib/usable.rb', line 30

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
  usable! mod if mod_ext.has_spec?
  mod_ext
end

#usable!(mod) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/usable.rb', line 41

def usable!(mod)
  mod_name = mod.name ? mod.name.split('::').last : "UsableMod#{Time.now.strftime('%s')}"
  const_name = "#{mod_name}Used"
  mod = mod.call if mod.respond_to? :call
  remove_const const_name if const_defined? const_name
  const_set const_name, mod
  usable_config.modules << mod
  send :include, mod
end