Sha256: f8e1e55b43f62cf7ead13597478d5ed3c38896dc4900343d8d4cceae7228cc56
Contents?: true
Size: 1.69 KB
Versions: 9
Compression:
Stored size: 1.69 KB
Contents
require 'rext/object/metaclass' class Module ## # Shortcut for including an anonymous module. def chainable &block include Module.new(&block) end ## # Shortcut for extending with an anonymous module. def extendable &block extend Module.new(&block) end ## # Call +method+ when conditions are met. # # This method provides a simple means of # utilizing method_missing, and should # be used in most cases. # # When using a regular expression with # the :if option, all captures are passed # to the handling method. # # === Examples # # class Foo # call_method :find_by, :if => /^find_by_(\w+)/ # def find_by attr, *args # "finding by #{attr} with #{args.join(', ')}" # end # end # # foo = Foo.new # foo.find_by_name('bar') # => "finding by name with bar" # # === Options # # :if regexp or proc # def call_method method, options = {} chainable { define_method :method_missing do |meth, *args| if options[:if].is_a?(Regexp) && meth.to_s =~ options[:if] send method, *($~.captures + args) elsif options[:if].respond_to?(:call) && options[:if].call(meth, *args) send method, *args else super end end } end ## # Equivalent to defining self.included and # instance evaluating the module passed. # # === Examples # # def self.included mod # mod.instance_eval do # include InstanceMethods # end # end # # setup do # include InstanceMethods # end # def setup &block meta_def :included do |mod| mod.instance_eval &block end end end
Version data entries
9 entries across 9 versions & 1 rubygems