Skip to content

Instantly share code, notes, and snippets.

View codematix's full-sized avatar

Ranganath Kini codematix

View GitHub Profile

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@indexzero
indexzero / v8-0412.js
Created January 3, 2012 05:30
Properties of Error instances in the latest V8 included by node@0.6.6 are not enumerable. And thus cannot be serialized through JSON.stringify
> var err = new Error('wtf?');
> JSON.stringify(err)
'{"stack":"Error: wtf?\\n at [object Context]:1:11\\n at Interface.<anonymous> (repl.js:179:22)\\n at Interface.emit (events.js:64:17)\\n at Interface._onLine (readline.js:153:10)\\n at Interface._line (readline.js:408:8)\\n at Interface._ttyWrite (readline.js:585:14)\\n at ReadStream.<anonymous> (readline.js:73:12)\\n at ReadStream.emit (events.js:81:20)\\n at ReadStream._emitKey (tty_posix.js:307:10)\\n at ReadStream.onData (tty_posix.js:70:12)","message":"wtf?"}'
> Object.keys(err);
[ 'stack', 'arguments', 'type', 'message' ]
> Object.getOwnPropertyDescriptor(err, 'stack');
{ get: [Function], set: [Function], enumerable: true, configurable: true }
> process.versions.v8
'3.1.8.26'
@jboner
jboner / latency.txt
Last active July 30, 2024 02:24
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@MohamedAlaa
MohamedAlaa / tmux-cheatsheet.markdown
Last active July 30, 2024 04:25
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
<?php
/*
* This is custom gateway for "Citrus Payement Sloution"
* @created: 5 MARCH 2013
* @author: Omprakash
*/
$nzshpcrt_gateways[$num]['name'] = 'Citrus Pay Gateway';
$nzshpcrt_gateways[$num]['internalname'] = 'citrus_pay_gateway';
$nzshpcrt_gateways[$num]['function'] = 'gateway_citrus_pay_gateway';
@guifromrio
guifromrio / teamcity-agent-ubuntu.md
Last active October 21, 2022 12:35
Instructions to Setup Teamcity Agent on EC2 Ubuntu 12.04.2 Linux with NodeJS and PhantomJS
@sandcastle
sandcastle / install-teamcity.md
Last active December 7, 2023 18:02
Install TeamCity 9.0.3 on Ubuntu with Nginx
{
"caret_style": "solid",
"color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
"enable_tab_scrolling": false,
"file_exclude_patterns":
[
"*.pyc",
"*.pyo",
"*.exe",
"*.dll",
@codematix
codematix / url-regex.md
Last active August 29, 2015 14:22
Regular Expressions to match URL components

Documentation

  1. Scheme - The scheme name consists of a sequence of characters beginning with a letter and followed by any combination of letters, digits, +, ., or -. Although schemes are case-insensitive, the canonical form is lowercase and documents that specify schemes must do so with lowercase letters. The scheme name is followed by a colon :.
  2. Host Name - Hostname labels may contain only the ASCII letters a through z (in a case-insensitive manner), the digits 0 through 9, and the -. While a hostname may not contain other characters, such as the underscore character _, other DNS names may contain the underscore.
  3. Port Number - A port number is a 16-bit unsigned integer, thus ranging from 0 to 65535.
  4. Path - If present, may optionally begin with a single forward slash /. It may not begin with two slash characters //. The path is a sequence of segments (conceptually similar to directories, though not necessarily representing them) separated by a forward s
@shibukawa
shibukawa / app.js
Created April 13, 2016 03:41
mithril lazy loading
var m = require("mithril");
m.mount(document.querySelector("#menu"), {
view: function() {
return m("ul", [
m("li", m('a[href="/a"]', {config: m.route}, "module A")),
m("li", m('a[href="/b"]', {config: m.route}, "module B")),
m("li", m('a[href="/c"]', {config: m.route}, "module C"))
]);
}