Module: Sinatra::Pebblebed::Helpers
- Defined in:
- lib/pebblebed/sinatra.rb
Instance Method Summary collapse
- #current_identity ⇒ Object
- #current_identity_is?(identity_id) ⇒ Boolean
- #current_session ⇒ Object (also: #checkpoint_session)
- #limit_offset_collection(collection, options) ⇒ Object
-
#part(partspec, params = {}) ⇒ Object
Render the markup for a part.
- #parts_script_include_tags ⇒ Object
- #parts_stylesheet_include_tags ⇒ Object
- #pebbles ⇒ Object
- #require_god ⇒ Object
- #require_identity ⇒ Object
- #require_parameters(parameters, *keys) ⇒ Object
Instance Method Details
#current_identity ⇒ Object
35 36 37 |
# File 'lib/pebblebed/sinatra.rb', line 35 def current_identity pebbles.checkpoint.me end |
#current_identity_is?(identity_id) ⇒ Boolean
45 46 47 48 49 50 |
# File 'lib/pebblebed/sinatra.rb', line 45 def current_identity_is?(identity_id) require_identity unless (current_identity.id == identity_id || current_identity.god) halt 403, "Private resource" end end |
#current_session ⇒ Object Also known as: checkpoint_session
26 27 28 |
# File 'lib/pebblebed/sinatra.rb', line 26 def current_session params[:session] || request.['checkpoint.session'] end |
#limit_offset_collection(collection, options) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/pebblebed/sinatra.rb', line 62 def limit_offset_collection(collection, ) limit = ([:limit] || 20).to_i offset = ([:offset] || 0).to_i collection = collection.limit(limit+1).offset(offset) last_page = (collection.size <= limit) = {:limit => limit, :offset => offset, :last_page => last_page} collection = collection[0..limit-1] [collection, ] end |
#part(partspec, params = {}) ⇒ Object
Render the markup for a part. A partspec takes the form “<kit>.<partname>”, e.g. “base.post”
9 10 11 12 |
# File 'lib/pebblebed/sinatra.rb', line 9 def part(partspec, params = {}) params[:session] ||= current_session pebbles.parts.markup(partspec, params) end |
#parts_script_include_tags ⇒ Object
14 15 16 17 18 |
# File 'lib/pebblebed/sinatra.rb', line 14 def @script_include_tags ||= pebbles.parts.javascript_urls.map do |url| "<script src=\"#{url.to_s}\"></script>" end.join end |
#parts_stylesheet_include_tags ⇒ Object
20 21 22 23 24 |
# File 'lib/pebblebed/sinatra.rb', line 20 def @stylesheet_include_tags ||= pebbles.parts.stylesheet_urls.map do |url| "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"#{url.to_s}\">" end.join end |
#pebbles ⇒ Object
31 32 33 |
# File 'lib/pebblebed/sinatra.rb', line 31 def pebbles @pebbles ||= ::Pebblebed::Connector.new(checkpoint_session, :host => request.host) end |
#require_god ⇒ Object
52 53 54 55 |
# File 'lib/pebblebed/sinatra.rb', line 52 def require_god require_identity halt 403, "Current identity #{current_identity.id} is not god" unless current_identity.god end |
#require_identity ⇒ Object
39 40 41 42 43 |
# File 'lib/pebblebed/sinatra.rb', line 39 def require_identity unless current_identity.respond_to?(:id) halt 403, "No current identity." end end |
#require_parameters(parameters, *keys) ⇒ Object
57 58 59 60 |
# File 'lib/pebblebed/sinatra.rb', line 57 def require_parameters(parameters, *keys) missing = keys.map(&:to_s) - (parameters ? parameters.keys : []) halt 409, "missing parameters: #{missing.join(', ')}" unless missing.empty? end |