Sha256: bd2e3fb9599a92667807a23f2613eb9e4d2ab34bcbab54330ed422cf8c9f879f

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 KB

Contents

# The original Module class.
#
class Module
  
  def forward *methods
    to = extract_to_from_options methods,
           "Forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. forward :something, :to => :an_array_reader)."
    forwarding methods do |method|
      "def #{method}(*args, &block)\n#{to}.__send__(#{method.inspect}, *args, &block)\nend\n"
    end
  end
  
  def each_forward *methods
    to = extract_to_from_options methods,
           "Multi forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. each_forward :something, :to => :an_array_reader)."
    forwarding methods do |method|
      "def #{method}(*args, &block)\n#{to}.each{ |t| t.__send__(#{method.inspect}, *args, &block) }\nend\n"
    end
  end
  
  private
  
    def extract_to_from_options args, error_string
      options = args.pop
      unless options.is_a?(Hash) && to = options[:to]
        raise ArgumentError, "Multi forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. each_forward :something, :to => :an_array_reader)."
      end
      to
    end
  
    def forwarding methods, &method_definition
      methods.each do |method|
        module_eval method_definition[method], "(__FORWARDING__)", 1
      end
    end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
picky-4.14.0 lib/picky/extensions/module.rb
picky-4.13.1 lib/picky/extensions/module.rb
picky-4.13.0 lib/picky/extensions/module.rb
picky-4.12.13 lib/picky/extensions/module.rb
picky-4.12.12 lib/picky/extensions/module.rb
picky-4.12.11 lib/picky/extensions/module.rb
picky-4.12.10 lib/picky/extensions/module.rb
picky-4.12.8 lib/picky/extensions/module.rb
picky-4.12.7 lib/picky/extensions/module.rb
picky-4.12.6 lib/picky/extensions/module.rb
picky-4.12.5 lib/picky/extensions/module.rb
picky-4.12.4 lib/picky/extensions/module.rb
picky-4.12.3 lib/picky/extensions/module.rb
picky-4.12.2 lib/picky/extensions/module.rb