Sha256: bf0b60d6f37ae70f3485ef92fe00299446280690526d2a8eb5e9485d673ab708

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module MAbbre
  ##
  # This module is added to the method lookup path and interprets abbreviations for tracked methods.
  module Interpreter
    private

    ##
    # call-seq:
    #   method_missing(name, *args)
    #
    # If a suitable candidate for abbreviation +name+ is found it will be called using +args+. Otherwise it will let
    # +super+ handle the missing method.
    def method_missing(name, *args)
      matched = nil

      if self.class.tracked_methods(MAbbre).one? {|m| matched = m if m =~ /\A#{name}/ }
        send(matched, *args)
      else
        super
      end
    end

    ##
    # call-seq:
    #   respond_to_missing?(name, include_all) => true or false
    #
    # Checks if this object responds to abbreviation +name+. The +include_all+ parameter is not used but it will
    # be passed to +super+ if no suitable candidate is found.
    #
    # Returns +true+ if this object responds to +name+, or whatever +super+ returns otherwise.
    def respond_to_missing?(name, include_all)
      self.class.tracked_methods(MAbbre).one? {|m| m =~ /\A#{name}/ } or super
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mabbre-1.0.2 lib/mabbre/interpreter.rb
mabbre-1.0.1 lib/mabbre/interpreter.rb
mabbre-1.0.0 lib/mabbre/interpreter.rb