Module: Inclusive::Class

Defined in:
lib/inclusive.rb

Overview

This will extend your class by default if you ‘include Inclusive`, but if for some reason you don’t want the ‘packages` instance method in your class, you can simply `extend Inclusive::Class`

Instance Method Summary collapse

Instance Method Details

#packages(method_name) ⇒ void Also known as: public_packages

This method returns an undefined value.

Use this as a decorator for an instance method which you will use to access your package imports.

This new method will be set to private, unless you use ‘public_packages` instead of `packages`.

Examples:

method definition

packages def utilities = [Package1, Package2]

def some_logic_here
  utilities.do_stuff # from Package1
  utilities.convert(x) # from Package2
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/inclusive.rb', line 84

def packages(method_name)
  old_method_name = :"__old_#{method_name}__"
  ivar_name = :"@_#{method_name}"

  alias_method old_method_name, method_name

  define_method method_name do
    return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)

    packages_to_extend = send(old_method_name)
    ModuleWithPackages.dup.tap do |mod|
      mod.module_eval do
        def self.name = "ModuleWithPackages" # preserve module name
      end
      packages_to_extend.each { |package| mod.extend_with_package(package) }
      instance_variable_set(ivar_name, mod)
    end
  end

  private method_name unless __callee__ == :public_packages
end