This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'sequel' | |
def sql_to_csv(sql, db: Sequel.connect(ENV['DATABASE_URL'])) | |
rel = db[sql] | |
CSV.generate('', force_quotes: ENV['NO_QUOTE'].nil?) do |csv| | |
csv << rel.columns | |
rel.each {|row| csv << row.values } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Constructor | |
def constructor(*names) | |
define_method(:initialize) do |*args| | |
names.zip(args).each do |name, value| | |
instance_variable_set("@#{name}", value) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo < ActiveRecord::Base | |
after_save :do_something, unless: :other_situation? | |
class << self | |
def other_situation | |
begin | |
Thread.current[:other_situation] = true | |
yield | |
ensure | |
Thread.current[:other_situation] = nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Doublet | |
def doublet(name, &block) | |
let(name) { double.tap(&block) } | |
end | |
def doublet!(name, &block) | |
let!(name) { double.tap(&block) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CoffeeConstructor | |
def constructor(*ivars) | |
define_method :initialize do |*args| | |
raise(ArgumentError) unless ivars.size == args.size | |
ivars.zip(args).each do |ivar, val| | |
instance_variable_set(ivar, val) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Censorable | |
extend ActiveSupport::Concern | |
class MonitorRequestFailed < RuntimeError | |
... | |
end | |
included do | |
has_many :monitor_requests, as: :entity, dependent: :destroy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Retriable | |
class RepeatedError < RuntimeError | |
def initialize(max, *args) | |
@errors = [] | |
@max = max | |
super(*args) | |
end | |
def <<(e) | |
@errors << e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['foo', 'bar'].inject(Paste.scoped) {|rel, tag| | |
rel.where(id: Tagging.joins(:tag).select(:paste_id).where('tags.name = ?', tag)) | |
}.to_sql | |
# => | |
# SELECT "pastes".* | |
# FROM "pastes" | |
# WHERE "pastes"."id" IN (SELECT paste_id FROM "taggings" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE (tags.name = 'foo')) | |
# AND "pastes"."id" IN (SELECT paste_id FROM "taggings" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE (tags.name = 'bar')) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def foo(num) | |
%w[one two three][num - 1] | |
end | |
end | |
describe A do | |
describe "#foo" do | |
RSpec::Matchers.define :do_foo do |args| | |
match do |instance| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NestedFetch | |
def nested_fetch(*keys) | |
keys.inject(self) do |hash, key| | |
(v = hash[key]).nil? ? break : v | |
end | |
end | |
end | |
Hash.class_eval { include NestedFetch } |
NewerOlder