Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

View GitHub Profile
@justinko
justinko / mongo_aggregation.rb
Created June 10, 2013 18:00
Demonstrate MongoDB aggregation with the Ruby Mongoid gem.
require 'mongoid'
require 'pp'
Mongoid.configure do |config|
config.allow_dynamic_fields = false
config.connect_to 'mongo_test', { consistency: :strong }
end
Mongoid.default_session.drop
@justinko
justinko / Plea.markdown
Created May 30, 2012 19:40
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

def find_in_batches(model)
current_batch, count = 0, model.count
while count > 0 do
model.all.skip(current_batch * 1000).limit(1000).each do |doc|
yield doc
end
count -= 1000
current_batch += 1
end
class SyncToAnalyticsService
ConnectionFailure = Class.new(StandardError)
def self.perform(data)
new(data).perform
end
def initialize(data)
@data = data.symbolize_keys
@account = Account.find(@data[:account_id])
require 'rspec/autorun'
class FlattenArray
def self.call(array)
new(array).call
end
def initialize(array)
@array = array
@flat_array = []
@justinko
justinko / gist:1179343
Created August 29, 2011 20:34
Run a rake task in a separate process in your Rails app
desc 'Execute a task in the background'
task :background, :task do |t, args|
rake_path = `which rake`.chomp
app_path = Dir.pwd
cmd = "#{rake_path} #{args.task} --trace --rakefile #{app_path}/Rakefile >> #{app_path}/log/rake.log 2>&1 &"
puts 'Executing:'
puts " #{cmd}"
system cmd
end

Keybase proof

I hereby claim:

  • I am justinko on github.
  • I am jko170 (https://keybase.io/jko170) on keybase.
  • I have a public key whose fingerprint is 01F0 22A4 834B B3C4 FF22 4029 EEF7 D932 65EA 5E49

To claim this, I am signing this object:

@justinko
justinko / gist:743458
Created December 16, 2010 14:37
Common gem configuration implementations
class MyGem
class << self
attr_accessor :color
end
def self.configure(&block)
instance_eval(&block)
end
end
@justinko
justinko / roman_numeral_calculator.rb
Last active June 4, 2017 21:34
Roman Numeral Calculator
require 'rspec/autorun'
class RomanNumeralCalculator
SYMBOL_MAPPING = {
1 => 'I',
4 => 'IV',
5 => 'V',
9 => 'IX',
10 => 'X',
40 => 'XL',
def merge_string(a, b)
[a, b].map(&:size).max.times.with_object('') do |(index), output|
[a, b].each {|var| output << (var[index] || '') }
end
end