docs
[sinatra.git] / lib / sinatra / dsl.rb
blob9aeea40789f6e8f07893363aa123e46e6733ca39
2 module Sinatra
4   module Dsl
6     # Define an Event that responds to a +path+ on GET method
7     # 
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.
9     #
10     # Example:
11     #   # Going RESTful
12     #
13     #   get '/' do
14     #     .. show stuff ..
15     #   end
16     #   
17     #   post '/' do
18     #     .. add stuff ..
19     #     redirect '/'
20     #   end
21     #   
22     #   put '/:id' do
23     #     .. update params[:id] ..
24     #     redirect '/'
25     #   end
26     #   
27     #   delete '/:id' do
28     #     .. delete params[:id] ..
29     #     redirect '/'
30     #   end
31     #
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
33     #
34     def get(path, &block)
35       Sinatra::Event.new(:get, path, &block)
36     end
38     # Same as get but responds to POST
39     def post(path, &block)
40       Sinatra::Event.new(:post, path, &block)
41     end
43     # Same as get but responds to PUT
44     #
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
46     def put(path, &block)
47       Sinatra::Event.new(:put, path, &block)
48     end
50     # Same as get but responds to DELETE
51     #
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)
55     end
57     # Run given block after each Event's execution
58     # Example:
59     #   after_attend do
60     #     logger.debug "After event attend!"
61     #   end
62     def after_attend(filter_name = nil, &block)
63       Sinatra::Event.after_attend(filter_name, &block)
64     end
65   
66     # Add methods to each event for use during execution
67     #
68     # Example:
69     #   helpers do
70     #     def foo
71     #       'foo!'
72     #     end
73     #   end
74     #   
75     #   get '/bar' do
76     #     foo
77     #   end
78     #   
79     #   get_it '/bar' # => 'foo!'
80     #
81     def helpers(&block)
82       Sinatra::EventContext.class_eval(&block)
83     end
85     # Maps a path to a physical directory containing static files
86     #
87     # Example:
88     #   static '/p', 'public'
89     #
90     def static(path, root)
91       Sinatra::StaticEvent.new(path, root)
92     end
93     
94     # Execute block if in development mode (Used for configuration)
95     def development
96       yield if Sinatra::Options.environment == :development
97     end
98   
99     # Execute block if in production mode (Used for configuration)
100     def production
101       yield if Sinatra::Options.environment == :production
102     end
103   
104     # Execute block if in test mode (Used for configuration)
105     def test
106       yield if Sinatra::Options.environment == :test
107     end
108   
109     # Define named layouts (default name is <tt>:layout</tt>)
110     # 
111     # Examples:
112     #   # Default layout in Erb
113     #   layout do
114     #     '-- <%= yield %> --'
115     #   end
116     #   
117     #   # Named layout in Haml
118     #   layout :for_haml do
119     #     '== XXXX #{yield} XXXX'
120     #   end
121     #   
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
124     #   
125     #   def layout(name = :layout, options = {})
126     #     Layouts[name] = unless block_given?
127     #       File.read("%s/%s" % [options[:views_directory] || 'views', name])
128     #     else
129     #       yield
130     #     end
131     #   end
132     #
133     # Cool trick:
134     #   
135     #   # Send a one-time layout to renderer method
136     #   get '/cooltrick' do
137     #     erb 'wicked' do
138     #       'Cool <%= yield %> Trick'
139     #     end
140     #   end
141     #   
142     #   get_it '/cooltrick' # => 'Cool wicked Trick'
143     #
144     def layout(name = :layout, options = {})
145       Layouts[name] = unless block_given?
146         File.read("%s/%s" % [options[:views_directory] || 'views', name])
147       else
148         yield
149       end
150     end  
151   
152     # Turn sessions <tt>:on</tt> or <tt>:off</tt>
153     #  
154     # NOTE:  There is currently no way to turn it on or off per Event... patches anyone?)
155     def sessions(on_off)
156       Sinatra::Session::Cookie.use = on_off
157     end
159   end
163 include Sinatra::Dsl