class Module

Public Instance Methods

attr_reader?(*attrs) click to toggle source

Generate accessor method with question mark

@param [String, Symbol…] attrs Attributes to generate setter for @return [void]

# File lib/olelo/extensions.rb, line 7
def attr_reader?(*attrs)
  attrs.each do |a|
    module_eval "def #{a}?; !!@#{a}; end"
  end
end
attr_setter(*attrs) click to toggle source

Generate attribute setter

A setter accepts an argument to set a value. It acts as a getter without an argument.

@see Ruby facets @param [String, Symbol…] attrs Attributes to generate setter for @return [void]

# File lib/olelo/extensions.rb, line 22
def attr_setter(*attrs)
  code, made = '', []
  attrs.each do |a|
    code << "def #{a}(*a); a.size > 0 ? (@#{a}=a[0]; self) : @#{a} end\n"
    made << a.to_sym
  end
  module_eval(code)
  made
end
redefine_method(name, &block) click to toggle source

Redefine a module method

Replaces alias_method_chain and allows to call overwritten method via super.

@param [Symbol, String] name of method @yield New method block @return [void]

# File lib/olelo/extensions.rb, line 41
def redefine_method(name, &block)
  if instance_methods(false).any? {|x| x.to_s == name.to_s }
    method = instance_method(name)
    mod = Module.new do
      define_method(name) {|*args| method.bind(self).call(*args) }
    end
    remove_method(name)
    include(mod)
  end
  include(Module.new { define_method(name, &block) })
end