Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

method_missing_router

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

method_missing_router

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

MethodMissingRouter

MethodMissingRouter provides a DSL for dynamic methods backed by method_missing. You define a regular expression => method route and MethodMissingRouter spits out the tedious boilerplate code for implementing method_missing and respond_to_missing?

Basic usage

To route class methods use class_route_missing /SOME_REGEX/, options

To route instance methods use route_missing /SOME_REGEX/, options

Working With Regex Matches

You can configure the router to pass your regex matches to the target method by setting ":pass_match" or ":pass_matches" in the options hash

Examples (from /examples/example_spec.rb)

describe "an example of instance routing with multiple matches" do
  class SpaceShip
    include MethodMissingRouter
    route_missing /^fire\_the\_(.+)_and_(.+)/, :fire_weapon, :pass_matches => true

    def fire_weapon(weapon_names, count)
      "firing #{count} #{weapon_names.first} and #{weapon_names.last}"
    end
  end

  it 'passes along the name of the weapon since we set pass matches' do
    ship = SpaceShip.new
    ship.fire_the_missiles_and_bacon(5).should == "firing 5 missiles and bacon"
  end
end


describe "an example of class routing with a single match" do
  class LetterCounter
    include MethodMissingRouter
    class_route_missing /([a-z])s/, :count_letter, :pass_match => true

    def self.count_letter(letter, string)
      string.count(letter)
    end
  end

  it 'passes along the letter since we set pass match' do
    LetterCounter.as('a string and another string').should == 3
  end
end


describe "an example of instance routing that doesn't override the message" do
  class Turtle
    include MethodMissingRouter
    route_missing /some_message/, :announce

    def announce(message)
      "message was #{message}"
    end
  end

  it 'passes along the original method name since we did not ask to pass the match(es)' do
    Turtle.new.some_message_fred.should == "message was some_message_fred"
  end
end

FAQs

Package last updated on 12 Dec 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc