Skip to content

Instantly share code, notes, and snippets.

View cheeyeo's full-sized avatar
💭
Researching on use of transformers in computer vision

Chee Yeo cheeyeo

💭
Researching on use of transformers in computer vision
View GitHub Profile
@cheeyeo
cheeyeo / example.rb
Created August 20, 2015 20:59
ActiveRecord Transactions
ActiveRecord::Base.transaction do
@order.destroy!
@user.save!
end
# calling the bang methods will raise an exception and cause the transaction not to occur if errors exist. calling save and non-bang will not raise
# any errors at all
* Each transaction opens up a new database connection
@cheeyeo
cheeyeo / transactions.markdown
Last active August 29, 2015 14:27 — forked from jcasimir/transactions.markdown
Transactions

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

@cheeyeo
cheeyeo / error.rb
Created August 19, 2015 22:28
error handling
begin
update_purchase_captured_at!(purchase, captured_at)
rescue Exception => e
Honeybadger.notify(e)
Rails.logger.error(%Q{\
[#{Time.zone.now}][rake purchases:update_captured_at] Error updating \
captured_at for purchase #{purchase.id}: #{e.class}: #{e.message}
This means the funds got captured, but the order will *not* be fulfilled!
@cheeyeo
cheeyeo / gist:7aa4a5cf0db050d6216d
Last active September 13, 2021 21:29 — forked from beanieboi/gist:ad526faf063181f336a2
Codeship Nginx Config for Heroku
daemon off;
# Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections 1024;
}
class User
attr_accessor :name, :pet_name
end
class Post
end
class Factory < BasicObject
def initialize
@attributes = {}
# app/models/importer.rb
class Importer
include ActiveModel::Validations
include ActiveModel::Conversion
VALID_IMPORT_TYPES = ['contact']
validates :import_type, inclusion: { in: VALID_IMPORT_TYPES }
attr_reader :parser, :import_type
@cheeyeo
cheeyeo / xml_parser.rb
Last active August 29, 2015 14:27 — forked from kmile/xml_parser.rb
A small nokogiri xml reader DSL.
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#
@cheeyeo
cheeyeo / benchmark
Last active August 29, 2015 14:26 — forked from danneu/benchmark
Ox vs Nokogiri: DOM and SAX parsing comparison
# I'm no benchmark guru. Just did a bunch of:
$ time ruby <filename>
# Note: This is just an 80mb XML file with 38,000 nodes.
ox_dom.rb 4.56s user 0.78s system 93% cpu 5.714 total (550mb)
ox_dom.rb 4.58s user 0.79s system 87% cpu 6.126 total (550mb)
ox_dom.rb 4.60s user 0.80s system 87% cpu 6.140 total (550mb)
nokigiri_dom.rb 11.75s user 1.02s system 94% cpu 13.518 total (895mb)
nokigiri_dom.rb 11.36s user 1.02s system 93% cpu 13.211 total (895mb)
module Kernel
def system(*args)
rd, wr = IO.pipe
# Create a new subprocess that will just exec the requested program.
pid = fork do
# The sub-process closes its copy of the reading end of the pipe
# because it only needs to write.
rd.close
@cheeyeo
cheeyeo / CSV Duplication Remover
Last active August 29, 2015 14:26 — forked from murphyslaw/CSV Duplication Remover
A ruby script that removes duplicate rows in a csv file. Duplicates are found based on an identifier column and a criteria column, which are configurable.
#!/usr/bin/ruby -w
require 'csv'
require 'active_support/core_ext'
class Parser
attr_accessor :input_folder
attr_accessor :output_folder
attr_accessor :filename