Skip to content

Instantly share code, notes, and snippets.

View Ke-'s full-sized avatar

Ke Ke-

  • Zeroloop Limited
  • New Zealand
View GitHub Profile
@Ke-
Ke- / every.lasso
Last active December 25, 2015 20:19
Useful for gaining control of active_tick within threads.
<?lassoscript
/*
Description:
Restricts code from executing more frequently than the specified number of seconds.
Returns the number of seconds to wait until blocks should be executed.
Example:
define my_thread => thread {
@Ke-
Ke- / keycolumn.lasso
Last active December 27, 2015 02:19
keycolumn_value
protect => {inline}
define keycolumn_value() => {
local(c = column(keycolumn_name))
void != #c? return #c
local(curMap = inline_scopeGet)
#curMap && #curMap->size > 0 ? #c = #curMap->find(::currentinline)->dsInfo->forcedRowID
#c->isa(::integer) && #c < 0 ? #c = 2147483648 + (2147483648 + #c)
@Ke-
Ke- / set_user_password.lasso
Last active December 27, 2015 04:19
Set a Lasso user's password
define set_user_password(user::string,password::string) => {
#password = security_registry->encodePassword('Lasso Security',#user,#password)
inline(
-database = 'security_auth_registry',
-sql = 'UPDATE users SET password_ha1 = "'+#password->encodesql+'"
WHERE name = "'+#user->encodesql+ '" '
) => {}
}
// Usage:
@Ke-
Ke- / ds_cart.lasso
Last active December 30, 2015 05:49
Public DS Cart
define cart_id => var(cart_id) || $cart_id := lasso_uniqueid
define current_cart => var(current_cart) || $current_cart := cart->oncreate('id' = cart_id)
define cart => type {
parent activerow
public ds => ds(::example.carts)
public created_column => 'created'
public modified_column => 'modified'
// Inject construct similar to Ruby's inject.
define trait_foreach->inject(p::any) => {
local(gb) = givenblock
#gb->detach
.foreach => {
#p = #gb(#p,#1) || #p
}
return #p
}
@Ke-
Ke- / Hex to RGB
Last active August 29, 2015 14:04
define char2int(p::string,mult::integer=1) => {
match(#p) => {
case('a')
return 10 * #mult
case('b')
return 11 * #mult
case('c')
return 12 * #mult
case('d')
return 13 * #mult
define sys_info => (
'sys_getthreadcount: ' + sys_getthreadcount +'\n'
'sys_getheapsize: ' + sys_getheapsize +'\n'
'sys_getheapfreebytes: ' + sys_getheapfreebytes +'\n'
'sys_getbytessincegc: ' + sys_getbytessincegc +'\n'
'sys_time: ' + sys_time '\n'
'sys_getegid: ' + sys_getegid '\n'
'sys_geteuid: ' + sys_geteuid '\n'
'sys_getlogin: ' + sys_getlogin '\n'
'sys_getpid: ' + sys_getpid '\n'
@Ke-
Ke- / gist:aaee6ee0dff54f14fc9e
Last active August 29, 2015 14:07
Active Statement Example
local(
query = .ds->table(
::mytable
)->select(
#columns
)->where(
#where
)->orderby(
#orderby
)->limit(
@Ke-
Ke- / gist:13a4ac6efbd3c3bc1d61
Last active August 29, 2015 14:08
Log slow requests
define sys_slow_value => 1000.0
define sys_slow_output(p=null) => {
local(s) = micros
define_atend({
local(t) = (micros - #s) / 1000.0
#t > sys_slow_value && web_request ? log_critical('SLOW: ' + client_url + ' — ' + #t + ' milliseconds')
})
}
@Ke-
Ke- / cache_method.lasso
Last active May 9, 2016 22:13
Used either for a singleton effect or just for performance.
// Cache method result on a thread level
define cache_method(c::capture = givenblock) => {
#c->isnota(::capture) ? return #c
local(v) = #c->self ? #c->self->type->asstring + '.' + #c->methodname | #c->methodname
var(#v)->isa(::null) ? var(#v) = #c->detach & invoke
return var(#v)
}