Module: Happy::Controller::Actions
- Included in:
- Happy::Controller
- Defined in:
- lib/happy/controller/actions.rb
Instance Method Summary collapse
- #cache_control(s) ⇒ Object
- #content_type(type) ⇒ Object
- #halt!(message = :done, what = nil) ⇒ Object
- #header(name, value) ⇒ Object
- #layout(name) ⇒ Object
- #max_age(t, options = {}) ⇒ Object
-
#only_if_path_matches ⇒ Object
private
Execute the provided block, unless there are still bits of unprocessed path left (which indicates that the current path is not the path the user requested.).
- #redirect!(to, status = 302) ⇒ Object
- #run(thing, options = {}, &blk) ⇒ Object
- #serve!(data, options = {}) ⇒ Object
Instance Method Details
#cache_control(s) ⇒ Object
70 71 72 |
# File 'lib/happy/controller/actions.rb', line 70 def cache_control(s) header :cache_control, s end |
#content_type(type) ⇒ Object
52 53 54 |
# File 'lib/happy/controller/actions.rb', line 52 def content_type(type) header :content_type, type end |
#halt!(message = :done, what = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/happy/controller/actions.rb', line 34 def halt!( = :done, what = nil) only_if_path_matches do throw , what || response end end |
#header(name, value) ⇒ Object
74 75 76 77 |
# File 'lib/happy/controller/actions.rb', line 74 def header(name, value) name = name.to_s.dasherize.humanize if name.is_a?(Symbol) response[name] = value end |
#layout(name) ⇒ Object
48 49 50 |
# File 'lib/happy/controller/actions.rb', line 48 def layout(name) response.layout = name end |
#max_age(t, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/happy/controller/actions.rb', line 56 def max_age(t, = {}) = { :public => true, :must_revalidate => true }.merge() s = [] s << 'public' if [:public] s << 'must-revalidate' if [:must_revalidate] s << "max-age=#{t.to_i}" cache_control s.join(', ') end |
#only_if_path_matches ⇒ Object (private)
Execute the provided block, unless there are still bits of unprocessed path left (which indicates that the current path is not the path the user requested.)
100 101 102 |
# File 'lib/happy/controller/actions.rb', line 100 def only_if_path_matches yield if unprocessed_path.empty? end |
#redirect!(to, status = 302) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/happy/controller/actions.rb', line 40 def redirect!(to, status = 302) only_if_path_matches do header :location, url_for(to) response.status = status halt! end end |
#run(thing, options = {}, &blk) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/happy/controller/actions.rb', line 79 def run(thing, = {}, &blk) if thing.is_a?(Class) && thing.ancestors.include?(Happy::Controller) # Happy controllers! thing.new(self, , &blk).route elsif thing.respond_to?(:call) # Rack apps! throw :done, thing.call(request.env) elsif thing.respond_to?(:to_s) thing.to_s else raise "Don't know how to run #{thing.inspect} :(" end end |
#serve!(data, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/happy/controller/actions.rb', line 4 def serve!(data, = {}) only_if_path_matches do # Don't serve is data is not a string. return unless data.is_a?(String) # Mix in default options = { :layout => response.layout }.merge() # Add status code from options response.status = .delete(:status) if .has_key?(:status) # Extract layout layout = .delete(:layout) # Treat remaining options as headers .each { |k, v| header k, v } # Apply layout, if available if layout data = render(layout) { data } end # Set response body and finish request response.body = [data] halt! end end |