Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added some comments to maker.rb

  • Loading branch information...
commit 3fb96eda3a4262f3172d340f2cad1c4504e4a454 1 parent 8815f17
@nakajima authored
Showing with 36 additions and 7 deletions.
  1. +36 −7 lib/sinatras-hat/maker.rb
View
43 lib/sinatras-hat/maker.rb
@@ -27,10 +27,15 @@ def initialize(klass, overrides={})
with(options)
end
+ # Simply stores the app instance when #mount is called.
def setup(app)
@app = app
end
+ # Processes a request, using the action specified in actions.rb
+ #
+ # TODO The work of handling a request should probably be wrapped
+ # up in a class.
def handle(action, request)
request.error(404) unless only.include?(action)
protect!(request) if protect.include?(action)
@@ -40,10 +45,14 @@ def handle(action, request)
end
end
+ # Allows the DSL for specifying custom flow controls in a #mount
+ # block by altering the responder's defaults hash.
def after(action)
yield HashMutator.new(responder.defaults[action])
end
+ # The finder block is used when loading all records for the index
+ # action. It gets passed the model proxy and the request params hash.
def finder(&block)
if block_given?
options[:finder] = block
@@ -52,6 +61,9 @@ def finder(&block)
end
end
+ # The finder block is used when loading a single record, which
+ # is the case for most actions. It gets passed the model proxy
+ # and the request params hash.
def record(&block)
if block_given?
options[:record] = block
@@ -60,6 +72,8 @@ def record(&block)
end
end
+ # The authenticator block gets called before protected actions. It
+ # gets passed the basic auth username and password.
def authenticator(&block)
if block_given?
options[:authenticator] = block
@@ -68,6 +82,8 @@ def authenticator(&block)
end
end
+ # A list of actions that get generated by this maker instance. By
+ # default it's all of the actions specified in actions.rb
def only(*actions)
if actions.empty?
options[:only] ||= Set.new(options[:only])
@@ -76,6 +92,8 @@ def only(*actions)
end
end
+ # A list of actions to protect via basic auth. Protected actions
+ # will have the authenticator block called before they are handled.
def protect(*actions)
credentials.merge!(actions.extract_options!)
@@ -88,18 +106,24 @@ def protect(*actions)
end
end
+ # The path prefix to use for routes and such.
def prefix
options[:prefix] ||= model.plural
end
+ # An array of parent Maker instances under which this instance
+ # was nested.
def parents
@parents ||= parent ? Array(parent) + parent.parents : []
end
+ # Looks up the resource path for the specified arguments using this
+ # maker's Resource instance.
def resource_path(*args)
resource.path(*args)
end
+ # Default options
def options
@options ||= {
:only => Set.new(Maker.actions.keys),
@@ -112,19 +136,22 @@ def options
:authenticator => proc { |username, password| [username, password] == [:username, :password].map(&credentials.method(:[])) }
}
end
-
- def inspect
- "maker: #{klass}"
- end
-
+
+ # Generates routes in the context of the given app.
def generate_routes!
Router.new(self).generate(@app)
end
+ # The responder determines what kind of response should used for
+ # a given action.
+ #
+ # TODO It might be better off to instantiate a new one of these per
+ # request, instead of having one per maker instance.
def responder
@responder ||= Responder.new(self)
end
+ # Handles ORM/model related logic.
def model
@model ||= Model.new(self)
end
@@ -134,16 +161,18 @@ def logger
@logger ||= Logger.new(self)
end
+ private
+
+ # Generates paths for this maker instance.
def resource
@resource ||= Resource.new(self)
end
- private
-
def protected?(action)
protect.include?(action)
end
+ # Handles a request with logging and benchmarking.
def log_with_benchmark(request, action)
msg = [ ]
msg << "#{request.env['REQUEST_METHOD']} #{request.env['PATH_INFO']}"
Please sign in to comment.
Something went wrong with that request. Please try again.