6 # Define an Event that responds to a +path+ on GET method
8 # The +path+ can be a template (i.e. '/:foo/bar/:baz'). When recognized, it will add <tt>:foo</tt> and <tt>:baz</tt> to +params+ with their values.
23 # .. update params[:id] ..
28 # .. delete params[:id] ..
32 # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
35 Sinatra::Event.new(:get, path, &block)
38 # Same as get but responds to POST
39 def post(path, &block)
40 Sinatra::Event.new(:post, path, &block)
43 # Same as get but responds to PUT
45 # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
47 Sinatra::Event.new(:put, path, &block)
50 # Same as get but responds to DELETE
52 # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
53 def delete(path, &block)
54 Sinatra::Event.new(:delete, path, &block)
57 # Run given block after each Event's execution
60 # logger.debug "After event attend!"
62 def after_attend(filter_name = nil, &block)
63 Sinatra::Event.after_attend(filter_name, &block)
66 # Add methods to each event for use during execution
79 # get_it '/bar' # => 'foo!'
82 Sinatra::EventContext.class_eval(&block)
85 # Maps a path to a physical directory containing static files
88 # static '/p', 'public'
90 def static(path, root)
91 Sinatra::StaticEvent.new(path, root)
94 # Execute block if in development mode (Used for configuration)
96 yield if Sinatra::Options.environment == :development
99 # Execute block if in production mode (Used for configuration)
101 yield if Sinatra::Options.environment == :production
104 # Execute block if in test mode (Used for configuration)
106 yield if Sinatra::Options.environment == :test
109 # Define named layouts (default name is <tt>:layout</tt>)
112 # # Default layout in Erb
114 # '-- <%= yield %> --'
117 # # Named layout in Haml
118 # layout :for_haml do
119 # '== XXXX #{yield} XXXX'
122 # # Loads layout named <tt>:"foo.erb"</tt> from file (default behaviour if block is omitted)
123 # layout 'foo.erb' # looks for foo.erb. This is odd an is being re-thought
125 # def layout(name = :layout, options = {})
126 # Layouts[name] = unless block_given?
127 # File.read("%s/%s" % [options[:views_directory] || 'views', name])
135 # # Send a one-time layout to renderer method
136 # get '/cooltrick' do
138 # 'Cool <%= yield %> Trick'
142 # get_it '/cooltrick' # => 'Cool wicked Trick'
144 def layout(name = :layout, options = {})
145 Layouts[name] = unless block_given?
146 File.read("%s/%s" % [options[:views_directory] || 'views', name])
152 # Turn sessions <tt>:on</tt> or <tt>:off</tt>
154 # NOTE: There is currently no way to turn it on or off per Event... patches anyone?)
156 Sinatra::Session::Cookie.use = on_off