Class: Waxx::X

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#actObject

Returns the value of attribute act

Returns:

  • (Object)

    the current value of act



61
62
63
# File 'waxx/x.rb', line 61

def act
  @act
end

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



61
62
63
# File 'waxx/x.rb', line 61

def app
  @app
end

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



61
62
63
# File 'waxx/x.rb', line 61

def args
  @args
end

#dbObject

Returns the value of attribute db

Returns:

  • (Object)

    the current value of db



61
62
63
# File 'waxx/x.rb', line 61

def db
  @db
end

#extObject

Returns the value of attribute ext

Returns:

  • (Object)

    the current value of ext



61
62
63
# File 'waxx/x.rb', line 61

def ext
  @ext
end

#jobsObject

Returns the value of attribute jobs

Returns:

  • (Object)

    the current value of jobs



61
62
63
# File 'waxx/x.rb', line 61

def jobs
  @jobs
end

#methObject

Returns the value of attribute meth

Returns:

  • (Object)

    the current value of meth



61
62
63
# File 'waxx/x.rb', line 61

def meth
  @meth
end

#oidObject

Returns the value of attribute oid

Returns:

  • (Object)

    the current value of oid



61
62
63
# File 'waxx/x.rb', line 61

def oid
  @oid
end

#reqObject

Returns the value of attribute req

Returns:

  • (Object)

    the current value of req



61
62
63
# File 'waxx/x.rb', line 61

def req
  @req
end

#resObject

Returns the value of attribute res

Returns:

  • (Object)

    the current value of res



61
62
63
# File 'waxx/x.rb', line 61

def res
  @res
end

#uaObject

Returns the value of attribute ua

Returns:

  • (Object)

    the current value of ua



61
62
63
# File 'waxx/x.rb', line 61

def ua
  @ua
end

#usrObject

Returns the value of attribute usr

Returns:

  • (Object)

    the current value of 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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


87
88
89
# File 'waxx/x.rb', line 87

def write?
  ['put', 'post', 'patch', 'delete'].include? meth
end