Module: DTC::Utils::Meta

Included in:
Visitor::DSL::RecursiveDSLContextBlank
Defined in:
lib/dtc/utils/meta.rb

Instance Method Summary collapse

Instance Method Details

#advise(*method_names, &block) ⇒ Object

Replaces provided instance methods, specified by name or regexp, with a call that provides the original method as an argument.

To define a pass-through method, use:

class Test
  extend DTC::Utils::Meta
  def test
  end
  advise :test do |original, *args, &blk|
    original.call(*args, &blk)
  end
end


30
31
32
33
# File 'lib/dtc/utils/meta.rb', line 30

def advise *method_names, &block
  method_names = method_names.map { |e| e.is_a?(Regexp) ? instance_methods.select { |m| m =~ e } : e }
  method_names.flatten.each { |e| advise_method(e.to_sym, &block) }
end

#blank_class(*exceptions) ⇒ Object

Undefines all methods except those provided as arguments, or if empty, ‘class`.

To undefine even class, specify as argument: ‘:no_really_i_dont_even_want_class`



11
12
13
14
15
# File 'lib/dtc/utils/meta.rb', line 11

def blank_class *exceptions
  exceptions = exceptions.flatten.map(&:to_sym)
  exceptions = [:class] if exceptions.empty? && exceptions != [:no_really_i_dont_even_want_class]
  instance_methods.each { |meth| undef_method(meth) unless meth =~ /\A__/ || meth == :object_id || exceptions.index(meth.to_sym) }
end