/doc/us/reference.md

http://github.com/keplerproject/orbit · Markdown · 212 lines · 135 code · 77 blank · 0 comment · 0 complexity · 37c6f89996e9d34e1aa3a32205fe0a73 MD5 · raw file

  1. ## Reference Manual
  2. This is a short reference of all of Orbit's (and Orbit apps) methods.
  3. ## Module `orbit`
  4. **orbit.new([*app*])** - creates a new Orbit application, returning it (as a module).
  5. If *app* is a string this is the application's name, and sets field \_NAME. If *app*
  6. is a table it is used instead of a fresh table. This means you can pass `orbit.new`
  7. to the `module` function
  8. **orbit.htmlify(*func*[, *func*...])** - modifies the environment of *func* to
  9. include functions that generate HTML
  10. ## Orbit apps
  11. **app.run(*wsapi\_env*)** - WSAPI entry point for application, generated by Orbit
  12. **app.real\_path** - the root of the application in the filesystem, defaults to
  13. the path inferred by WSAPI launchers (`wsapi.app\_path`), but can be overridden
  14. **app.mapper** - mapper used by `app:model`, defaults to an instance of `orbit.model`,
  15. but can be overridden
  16. **app.not\_found** - default handler, sends a 404 response to the client, can be overridden.
  17. The handler receives a `web` object
  18. **app.server\_error** - error handler, sends a 500 response with stack trace to the
  19. client, can be overridden. The handler receives a `web` object
  20. **app:dispatch\_get(*func*, *patt*[, *patt*...])** - installs *func* as a GET handler for
  21. the patterns *patt*. *func* receives a `web` object and the captures; this handler should return
  22. the contents of the response or **app.reparse** if you want to hand control back to
  23. the dispatcher and let it continue matching;
  24. **app:dispatch\_post(*func*, *patt*[, *patt*...])** - installs *func* as a POST handler for
  25. the patterns *patt*. *func* receives a `web` object and the captures; this handler should return
  26. the contents of the response or **app.reparse** if you want to hand control back to
  27. the dispatcher and let it continue matching;
  28. **app:dispatch\_put(*func*, *patt*[, *patt*...])** - installs *func* as a PUT handler for
  29. the patterns *patt*. *func* receives a `web` object and the captures; this handler should return
  30. the contents of the response or **app.reparse** if you want to hand control back to
  31. the dispatcher and let it continue matching;
  32. **app:dispatch\_delete(*func*, *patt*[, *patt*...])** - installs *func* as a DELETE handler for
  33. the patterns *patt*. *func* receives a `web` object and the captures; this handler should return
  34. the contents of the response or **app.reparse** if you want to hand control back to
  35. the dispatcher and let it continue matching;
  36. **app:dispatch\_wsapi(*func*, *patt*[, *patt*...])** - installs *func* as a WSAPI handler for
  37. the patterns *patt*. *func* receives the unmodified WSAPI environment
  38. **app:dispatch\_static(*patt*[, *patt*...])** - installs a static files handler for
  39. the patterns *patt*. This handler assumes the PATH\_INFO is a file relative to
  40. `app.real_path` and sends it to the client. The MIME type is detected from
  41. the extension (default application/octec-stream)
  42. **app:serve\_static(*web*, *filename*)** - returns the content of file *filename* (which can be
  43. anywhere in the filesystem), while setting the appropriate headers with the file's MIME
  44. type
  45. **app:htmlify(*patt*[, *patt*...])** - same as `orbit.htmlify`, but changes all of the
  46. app's module functions that match the patterns *patt*
  47. **app:model(...)** - calls `app.mapper:new(...)`, so the behaviour depends on the mapper
  48. you are using
  49. ## `web` methods
  50. The *web* objects inherit the functions of module `wsapi.util` as methods.
  51. **web.status** - status to be sent to server (default: "200 Ok")
  52. **web.headers** - headers to be sent to server, a Lua table (default has Content-Type as text/html)
  53. **web.response** - body to send to the server (default is blank)
  54. **web.vars** - original WSAPI environment
  55. **web.prefix** - app's prefix (if set in the app's module) or SCRIPT\_NAME
  56. **web.suffix** - app's suffix (if set in the app's module)
  57. **web.real\_path** - location of app in filesystem, taken from wsapi\_env.APP\_PATH, or app's module real\_path, or "."
  58. **web.doc\_root, web.path\_info, web.script\_name, web.path\_translated, web.method** - server's document root, PATH\_INFO,
  59. SCRIPT\_NAME, PATH\_TRANSLATED, and REQUEST\_METHOD (converted to lowercase)
  60. **web.GET, web.POST** - GET and POST variables
  61. **web.input** - union of web.GET and web.POST
  62. **web.cookies** - cookies sent by the browser
  63. **web:set\_cookie(*name*, *value*)** - sets a cookie to be sent back to browser
  64. **web:delete\_cookie(*name*)** - deletes a cookie
  65. **web:redirect(*url*)** - sets status and headers to redirect to *url*
  66. **web:link(*part*, [*params*])** - creates intra-app link, using web.prefix and web.suffix, and encoding *params*
  67. as a query string
  68. **web:static\_link(*part*)** - if app's entry point is a script, instead of path, creates a link to the app's vpath
  69. (e.g. if app.prefix is /foo/app.ws creates a link in /foo/*part*), otherwise same as web:link
  70. **web:empty(*s*)** - returns true if *s* is nil or an empty string (zero or more spaces)
  71. **web:content\_type(*s*)** - sets the content type of the reponse to *s*
  72. **web:empty\_param(*name*)** - returns true if input parameter *name* is empty (as web:empty)
  73. **web:page(*name*, [*env*])** - loads and renders Orbit page called *name*. If *name* starts with / it's relative to
  74. the document root, otherwise it's relative to the app's path. Returns rendered page. *env* is an optional environment
  75. with extra variables.
  76. **web:page_inline(*contents*, [*env*])** - renders an inline Orbit page
  77. ## Module `orbit.cache`
  78. **orbit.cache.new(*app*, [*base\_path*])** - creates a page cache for *app*, either in memory or at the filesystem
  79. path *base\_path* (**not** relative to the app's path!), returns the cache object
  80. **a\_cache(*handler*)** - caches *handler*, returning a new handler; uses the PATH\_INFO as a key to the cache
  81. **a\_cache:get(*key*)** - gets value stored in *key*; usually not used, use the previous function instead
  82. **a\_cache:set(*key*, *val*)** - stores a value in the cache; use a\_cache(*handler*) to encapsulate this behaviour
  83. **a\_cache:invalidate(*key*)** - invalidates a cache value
  84. **a\_cache:nuke()** - clears the cache
  85. ## Module `orbit.model`
  86. **orbit.model.new([*table\_prefix*], [*conn*], [*driver*], [*logging*])** - creates a new ORM mapper. *table\_prefix* (default "")
  87. is a string added to the start of model names to get table names; *conn* is the database connection (can be set
  88. later); *driver* is the kind of database (currently "sqlite3", the default, and "mysql"); *logging*
  89. sets whether you want logging of all queries to `io.stderr`. Returns a mapper instance,
  90. and all the parameters can be set after this instance is created (via a\_mapper.table\_prefix, a\_mapper.conn,
  91. a\_mapper.driver, and a\_mapper.logging)
  92. **orbit.model.recycle(*conn\_builder*, *timeout*)** - creates a connection using *conn\_builder*, a function
  93. that takes no arguments, and wraps it so a new connection is automatically reopened every *timeout* seconds
  94. **a\_mapper:new(*name*, [*tab*])** - creates a new model object; *name* is used together with a\_mapper.table\_prefix to
  95. form the DB table's name; fields and types are introspected from the table. *tab* is an optional table that
  96. is used as the basis for the model object if present
  97. **a\_model.model** - the mapper for this model
  98. **a\_model.name, a\_model.table\_name** - the name of the model and its backing table
  99. **a\_model.driver** - the DB driver used
  100. **a\_model.meta** - metainformation about the model, introspected from the table
  101. **a\_model:find(*id*)** - finds and returns the instance of the model with the passed *id* (keyed using
  102. the `id` column of the table (must be numeric)
  103. **a\_model:find\_first(*condition*, *args*)** - finds and returns the first instance of the model that
  104. matches *condition*; *args* can determine the order (args.order), specify which fields should be returned
  105. (args.fields, default is all of them), and inject fields from other tables
  106. (args.inject)
  107. Example: `books:find_first("author = ? and year_pub > ?", { "John Doe", 1995, order = "year_pub asc" })`
  108. **a\_model:find\_all(*condition*, *args*)** - finds and returns all instances of the model that
  109. matches *condition*; *args* can determine the order (args.order), specify which fields should be returned
  110. (args.fields, default is all of them), limit the number of returned rows (args.count), skip that many rows before beginning to return rows (args.offset),
  111. return only distinct rows (args.distinct), and inject fields from other tables (args.inject)
  112. Example: `books:find_all("author = ? and year_pub > ?", { "John Doe", 1995, order = "year_pub asc", count = 5, fields = { "id", "title" } })`
  113. **a\_model:new([*tab*])** - creates a fresh instance of the model, optionally using *tab* as initial
  114. values
  115. **a\_model:find\_by\_xxx(*args*)** - finds and returns the first instance of the model building the
  116. condition from the method name, a la Rails' ActiveRecord; *args* works the same way as in **find\_first**, above
  117. **a\_model:find\_all\_by\_xxx(*args*)** - finds and returns all instances of the model building the
  118. condition from the method name, a la Rails' ActiveRecord; *args* works the same way as in **find\_all**, above
  119. Example: `books:find_all_by_author_or_author{ "John Doe", "Jane Doe", order = "year_pub asc" }`
  120. **an\_instance:save([*force\_insert*])** - saves an instance to the DB, commiting changes or creating the backing record if the instance is new; if *force\_insert* is true always do an insert instead of an update
  121. If there's a row called `created_at` this row is set to the creation date of the record; if there's a row
  122. called `updated_at` this row is set to the last update's date.
  123. **an\_instance:delete()** - deletes an instance from the DB
  124. ## Module `orbit.routes`
  125. This module has a single function, **R**, and you can also call the module itself. This function takes a PATH\_INFO pattern where the path segments (the parts of the path separated by `/` or `.`) can be named parameters (`:name`), *splats* (\*), optional parameters (`?:name?`), or literals, and returns a pattern.
  126. A pattern is an object with two methods:
  127. **patt:match(*str*)** - tries to match *str* and returns a hash of named parameters; splats are returned in a `splat` array inside this hash.
  128. **patt:build(*params*)** - builds the original pattern string from a hash of named parameters
  129. Some examples of patterns, what they match, and the parameter hashes:
  130. * '/foo', matches '/foo', empty hash
  131. * '/foo/:bar/baz' matches '/foo/orbit/baz' or '/foo/xxx.yyy/baz' with hashes { bar = 'orbit' } and { bar = 'xxx.yyy' }
  132. * '/:name.:ext' matches '/xxx.yyy' or '/filename.ext' with hashes { name = 'xxx', ext = 'yyy' } and { name = 'filename', ext = 'ext' }
  133. * '/*/foo/*' matches '/bar/foo' or '/bar/foo/bling/baz/boom' with hashes { splat = { 'bar' } } and { splat = { 'bar', 'bling/baz/boom' } }
  134. * '/?:foo?/?:bar?' matches '/' or '/orbit' or '/orbit/routes' with hashes {} and { foo = 'orbit' } and { foo = 'orbit', bar = 'routes' }
  135. And several more, see `test/test_routes.lua` for a comprehensive test suite.