Module: RegexMethod

Defined in:
lib/regex_method.rb,
lib/regex_method/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/regex_method.rb', line 11

def method_missing(method, *args, &block)
  self.class.regex_methods.each do |regex_method_name, proc|
    if matched_data = method.to_s.match(Regexp.new(regex_method_name))
      regex_method_args = matched_data[1..-1].concat(args)
      return proc.call(*regex_method_args)
    end
  end
  super
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/regex_method.rb', line 28

def self.included(base)
  base.extend ClassMethods
  base.class_eval do 
    class << self
      attr_accessor :regex_methods
    end
    self.regex_methods = {}
  end
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/regex_method.rb', line 21

def respond_to?(method)
  self.class.regex_methods.each do |regex_method_name, proc|
    return true if method.to_s.match(Regexp.new(regex_method_name))
  end
  super
end