Skip to content

Instantly share code, notes, and snippets.

class Peach
private_class_method :new
def self.enum_for(*args)
new(*args)
end
def initialize(list, pool_size = 5)
@queue = list.inject(Queue.new, :<<)
@pool_size = pool_size
@wycleffsean
wycleffsean / gist:cc12c4dbc872bb77fea4a00c33d72fa8
Last active May 28, 2019 21:56 — forked from unnitallman/gist:944011
sqlite with activerecord outside rails
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
AllCops:
Exclude:
- 'spec/dummy/**/*'
- 'tmp/**/*'
Lint/HandleExceptions:
Exclude:
- 'spec/support/database.rb'
Style/TrailingCommaInArguments:
Enabled: false
Metrics/BlockLength:
class SalesDashboardYTDByCustomer < ActiveRecord::Base
include Tablelessable
column :user_id
column :customer_id
column :customer_name
column :actual_sales
column :planned_sales
column :sales_difference
column :actual_profit
@wycleffsean
wycleffsean / example.rb
Created August 11, 2017 07:41
Ruby Peach
def self.gzip_to_s3
s3_bucket = Aws::S3::Resource.new(region:'us-east-1')
.bucket('basf-content-server')
total = SalesDataDocument.count
docs = SalesDataDocument.all
Peach.enum_for(docs).each do |doc, queue|
progress = total - queue.length
Rails.logger.info <<-LOG if (progress % 20).zero?
storing document ##{progress} of #{total}
LOG
@wycleffsean
wycleffsean / grouping.rb
Created June 6, 2017 21:42
Grouping by expression
require 'securerandom'
require 'pp'
Ex = Struct.new(:a,:b,:c,:d)
module Enumerable
def group(&block)
groups = Hash.new
each do |item|
matches = []
@wycleffsean
wycleffsean / fiber_sleep.rb
Last active February 4, 2019 21:54
Fiber Sleep
require 'fiber'
require 'concurrent' # gem install concurrent-ruby
Thread.abort_on_exception = true
class Async
def self.perform(&block)
instance = new
instance.instance_eval(&block)
until instance.dispatched.value.zero? do
unless instance.yields.empty?
@wycleffsean
wycleffsean / accio.rake
Created December 9, 2016 17:10
Heroku Accio DB
namespace :accio do
desc "Drop/restore database from Heroku backup"
task :db do
Rake::Task['accio:download_backup'].invoke
Rake::Task['accio:restore_backup'].invoke
end
desc "Download database dump from Heroku"
task :download_backup => [:logger_no_env, '~/.netrc'] do |t, args|
return Rails.logger.error('curl not installed') unless \
@wycleffsean
wycleffsean / dump_seeds.rake
Created September 1, 2016 17:25
Dump entire database into seeds
namespace :db do
task dump_seeds: :environment do
Rails.application.eager_load!
path = 'db/fixtures/gen/%s.rb'
ObjectSpace.each_object(Class)
.select{|c| c < ActiveRecord::Base && !c.abstract_class? }
.each do |c|
# skip if it's a view
next if c.new.readonly?
next unless c.attribute_names.include?('id')
@wycleffsean
wycleffsean / keybase.md
Created August 4, 2016 03:29
keybase.md

Keybase proof

I hereby claim:

  • I am wycleffsean on github.
  • I am sean_carey (https://keybase.io/sean_carey) on keybase.
  • I have a public key ASDMtr0za7DM5LVpJAR2Y5cvKrT-vz9MESI7MstPnInMIAo

To claim this, I am signing this object: