<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alastair&#039;s Axioms &#187; only</title>
	<atom:link href="http://blog.alastairdawson.com/tag/only/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alastairdawson.com</link>
	<description>Let us toast your non-idiocy</description>
	<lastBuildDate>Thu, 11 Aug 2011 23:13:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Sinatra before only filter</title>
		<link>http://blog.alastairdawson.com/2010/07/27/a-sinatra-before-only-filter/</link>
		<comments>http://blog.alastairdawson.com/2010/07/27/a-sinatra-before-only-filter/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 21:10:58 +0000</pubDate>
		<dc:creator>Alastair</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[before]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[only]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://blog.alastairdawson.com/?p=210</guid>
		<description><![CDATA[In a Sinatra application you can use the &#8216;before&#8217; filter to run something before every each event.

before do
  puts "I will run all the time..."
end

Here&#8217;s a module that will allow you to run code only before certain routes:

module Sinatra

  module BeforeOnlyFilter
    def before_only(routes, &#038;block)
      before [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.sinatrarb.com">Sinatra</a> application you can use the &#8216;before&#8217; filter to run something before every each event.</p>
<pre>
before do
  puts "I will run all the time..."
end
</pre>
<p>Here&#8217;s a module that will allow you to run code only before certain routes:</p>
<pre>
module Sinatra

  module BeforeOnlyFilter
    def before_only(routes, &#038;block)
      before do
        routes.map!{|x| x = x.gsub(/\*/, '\w+')}
        routes_regex = routes.map{|x| x = x.gsub(/\//, '\/')}
        instance_eval(&#038;block) if routes_regex.any? {|route| (request.path =~ /^#{route}$/) != nil}
      end
    end
  end

  register BeforeOnlyFilter

end
</pre>
<p>Use it like below, note that you insert &#8216;*&#8217; for routes that use variable placeholders like &#8216;:id&#8217;&#8230;</p>
<pre>
before_only(['/post', '/comment/*/user/*']) do
  puts "I will only run before the routes '/post' &#038; '/comment/*/user/*'..."
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.alastairdawson.com/2010/07/27/a-sinatra-before-only-filter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

