Class: Waxx::X
- Inherits:
-
Struct
- Object
- Struct
- Waxx::X
- Defined in:
- waxx/x.rb
Overview
The X Struct gets instanciated with each request (x) “` x.req # The request object (Instance of Waxx::Req) x.res # The response object (Instance of Waxx::Res) x.usr # The user session cookie
x.usr['id'] # Get the id value.
x.usr['name'] = value # Set a user session value
x.ua # The User Agent / Client cookie
x.ua['la'] # Get the last activity time of the user agent
x.ua['name'] = value # Set a user session value
x.db # The hash of database connections (0 or more)
x.db.app.exec(sql, [arg1, arg2, argn]) # Run a query on the app database
x.meth # The request method as a symbol: :get, :post, :put, :patch, :delete # Path parameters example.com/app/act/ x.app # The (app or module) The first path parameter x.act # The act (defined in Object.runs()) x.oid # The third arg as an int. Given example.com/person/record/42.json, then oid is 42 x.args # An array of arguments after /app/act/. Given POST example.com/customer/bill/10/dev/350.json, then args is ['10','dev','350'] # When defining a the run proc, args are splatted into the function. So given the example request above and: module Customer
extend Waxx::Postgres
extend self
runs(
bill: {
desc: "Bill a customer",
acl: "internal",
post: -> (x, customer_id, category, amount) {
# The variables here are:
# customer_id = '10'
# category = 'dev'
# amount = '350'
# Note: All passed in args are strings
# but x.oid = 10 (as an int)
}
}
)
end x.ext # The extension of the request: .json, .html, .pdf, etc. Default defined in Waxx['extension'] # Background jobs (executed after the response is returned to the client. For example to deliver an email.) x.jobs # An array of jobs
# Jobs are added as procs with optional arguments (proc, *args).
x.job(->(x, id){ App::Email.deliver(x, id) }, x, id) “`
Instance Attribute Summary collapse
-
#act ⇒ Object
Returns the value of attribute act.
-
#app ⇒ Object
Returns the value of attribute app.
-
#args ⇒ Object
Returns the value of attribute args.
-
#db ⇒ Object
Returns the value of attribute db.
-
#ext ⇒ Object
Returns the value of attribute ext.
-
#jobs ⇒ Object
Returns the value of attribute jobs.
-
#meth ⇒ Object
Returns the value of attribute meth.
-
#oid ⇒ Object
Returns the value of attribute oid.
-
#req ⇒ Object
Returns the value of attribute req.
-
#res ⇒ Object
Returns the value of attribute res.
-
#ua ⇒ Object
Returns the value of attribute ua.
-
#usr ⇒ Object
Returns the value of attribute usr.
Instance Method Summary collapse
- #/(k) ⇒ Object
- #<<(str) ⇒ Object
- #[](k) ⇒ Object
- #group?(g) ⇒ Boolean
- #groups?(*args) ⇒ Boolean
- #job(j, *args) ⇒ Object
- #usr? ⇒ Boolean
- #write? ⇒ Boolean
Instance Attribute Details
#act ⇒ Object
Returns the value of attribute act
61 62 63 |
# File 'waxx/x.rb', line 61 def act @act end |
#app ⇒ Object
Returns the value of attribute app
61 62 63 |
# File 'waxx/x.rb', line 61 def app @app end |
#args ⇒ Object
Returns the value of attribute args
61 62 63 |
# File 'waxx/x.rb', line 61 def args @args end |
#ext ⇒ Object
Returns the value of attribute ext
61 62 63 |
# File 'waxx/x.rb', line 61 def ext @ext end |
#jobs ⇒ Object
Returns the value of attribute jobs
61 62 63 |
# File 'waxx/x.rb', line 61 def jobs @jobs end |
#meth ⇒ Object
Returns the value of attribute meth
61 62 63 |
# File 'waxx/x.rb', line 61 def meth @meth end |
#oid ⇒ Object
Returns the value of attribute oid
61 62 63 |
# File 'waxx/x.rb', line 61 def oid @oid end |
#req ⇒ Object
Returns the value of attribute req
61 62 63 |
# File 'waxx/x.rb', line 61 def req @req end |
#res ⇒ Object
Returns the value of attribute res
61 62 63 |
# File 'waxx/x.rb', line 61 def res @res end |
#usr ⇒ Object
Returns the value of attribute usr
61 62 63 |
# File 'waxx/x.rb', line 61 def usr @usr end |
Instance Method Details
#/(k) ⇒ Object
81 82 83 |
# File 'waxx/x.rb', line 81 def /(k) req.post[k.to_s] || req.get[k.to_s] end |
#<<(str) ⇒ Object
75 76 77 |
# File 'waxx/x.rb', line 75 def << str res << str.to_s end |
#[](k) ⇒ Object
78 79 80 |
# File 'waxx/x.rb', line 78 def [](k) req.post[k.to_s] || req.get[k.to_s] end |
#group?(g) ⇒ Boolean
90 91 92 93 |
# File 'waxx/x.rb', line 90 def group? g return false unless usr? usr['grp'].include? g.to_s end |
#groups?(*args) ⇒ Boolean
94 95 96 |
# File 'waxx/x.rb', line 94 def groups?(*args) args.inject(0){|total, g| total + (group?(g) ? 1 : 0)} == args.size end |
#job(j, *args) ⇒ Object
97 98 99 |
# File 'waxx/x.rb', line 97 def job(j, *args) jobs << [j, *args] end |
#usr? ⇒ Boolean
84 85 86 |
# File 'waxx/x.rb', line 84 def usr? not (usr['id'].nil? or usr['id'].to_i == 0) end |
#write? ⇒ Boolean
87 88 89 |
# File 'waxx/x.rb', line 87 def write? ['put', 'post', 'patch', 'delete'].include? meth end |