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!"
63 # before_attend :authorize # authorize is a helper method defined using helpers
65 # Stop execution using - throw :halt
67 # throw :halt, 401 unless has_access?
69 # Throw a Symbol to execute a helper method
70 # Throw a String to render it as the content
71 # Throw a Fixnum to set the status
73 def before_attend(filter_name = nil, &block)
74 Sinatra::Event.before_attend(filter_name, &block)
77 # Run given block after each Event's execution
80 # logger.debug "After event attend!"
83 # after_attend :clean_up # clean_up is a helper method defined using helpers
85 def after_attend(filter_name = nil, &block)
86 Sinatra::Event.after_attend(filter_name, &block)
89 # Add methods to each event for use during execution
102 # get_it '/bar' # => 'foo!'
105 Sinatra::EventContext.class_eval(&block)
108 # Maps a path to a physical directory containing static files
111 # static '/p', 'public'
113 def static(path, root)
114 Sinatra::StaticEvent.new(path, root)
117 # Execute block if in development mode (Used for configuration)
119 yield if Sinatra::Options.environment == :development
122 # Execute block if in production mode (Used for configuration)
124 yield if Sinatra::Options.environment == :production
127 # Execute block if in test mode (Used for configuration)
129 # yield if Sinatra::Options.environment == :test
132 # Define named layouts (default name is <tt>:layout</tt>)
135 # # Default layout in Erb
137 # '-- <%= yield %> --'
140 # # Named layout in Haml
141 # layout :for_haml do
142 # '== XXXX #{yield} XXXX'
145 # # Loads layout named <tt>:"foo.erb"</tt> from file (default behaviour if block is omitted)
146 # layout 'foo.erb' # looks for foo.erb. This is odd an is being re-thought
148 # def layout(name = :layout, options = {})
149 # Layouts[name] = unless block_given?
150 # File.read("%s/%s" % [options[:views_directory] || 'views', name])
158 # # Send a one-time layout to renderer method
159 # get '/cooltrick' do
161 # 'Cool <%= yield %> Trick'
165 # get_it '/cooltrick' # => 'Cool wicked Trick'
167 def layout(name = :layout, options = {})
168 Layouts[name] = unless block_given?
169 File.read("%s/%s" % [options[:views_directory] || 'views', name])
175 # Turn sessions <tt>:on</tt> or <tt>:off</tt>
177 # NOTE: There is currently no way to turn it on or off per Event... patches anyone?)
179 Sinatra::Session::Cookie.use = on_off