Skip to content

Instantly share code, notes, and snippets.

View spickermann's full-sized avatar
💻

Martin Spickermann spickermann

💻
View GitHub Profile
@spickermann
spickermann / sort.rb
Created February 7, 2018 08:26
Sort locale yml files (quick'n'dirty)
def deeply_sort_hash(object)
return object.sort if object.is_a?(Array)
return object unless object.is_a?(Hash)
hash = RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new
object.each { |k, v| hash[k] = deeply_sort_hash(v) }
sorted = hash.sort_by { |(k, v)| [v.is_a?(String) ? 0 : 1, k.to_s.downcase] }
hash.class[sorted]
end
@spickermann
spickermann / clear-sidekiq-jobs.sh
Created November 15, 2017 16:49 — forked from wbotelhos/clear-sidekiq-jobs.sh
Clear Sidekiq Jobs
# 1. Clear retry set
Sidekiq::RetrySet.new.clear
# 2. Clear scheduled jobs
Sidekiq::ScheduledSet.new.clear
# 3. Clear 'Processed' and 'Failed' jobs
@spickermann
spickermann / model_with_an_uuid.rb
Created March 15, 2016 18:38
Use in model with `include UUIDPrimaryKey`
RSpec.shared_examples 'a model with an uuid' do
subject(:instance) { FactoryGirl.create(described_class.name.downcase) }
it { should have_readonly_attribute(:id) }
describe '.find' do
it 'finds by uuid' do
expect(
described_class.find(instance.id)
).to eq(instance)

Keybase proof

I hereby claim:

  • I am spickermann on github.
  • I am spickermann (https://keybase.io/spickermann) on keybase.
  • I have a public key whose fingerprint is 38D8 D438 A43B D6F5 88B1 80BF D461 0A5C 9AC5 CB5D

To claim this, I am signing this object:

# spec/support/factory_girl.rb
# This file is here to limit the use of factories to only integration tests and not unit tests.
#
# If you really want to use a factory you can add the tag :factories to a test
#
module FactoryGirlBlocker
mattr_accessor :disabled
def self.with_disabled
self.disabled = true
@spickermann
spickermann / remove-passwords-from-pdf-files.rb
Created April 13, 2014 11:24
Copies pdf files from one directory into an other and removes on the fly the password from the pdf files that is requested to open them.
# Script using [qpdf](http://qpdf.sourceforge.net/) to
# remove the password from all pdf files in a given
# directory and its subdirectories.
#
# Install qpdf with brew
#
# brew install qpdf
#
require 'fileutils'
class Foo
def initialize(foo)
@foo = foo
end
def foo
@foo
end
def -@ # This is awesome
Foo(-foo)
end
def array_unshift
foo = [:foo, :bar, :bar]
foo.unshift(:something)
end
def array_concat
foo = [:something]
foo.concat([:foo, :bar, :bar])
end
@spickermann
spickermann / find_candidates.rb
Last active December 28, 2015 21:19
Helper method that returns methods that might have a problem when upgrading from Ruby 1.8 to Ruby 1.9 because of shadowing block variables. With a lot of false positives...
# foo = 1
# 5.times do |foo|
# puts foo
# end
# return foo #=> returns 5 in Ruby 1.8, but 1 in Ruby 1.9
def files
Dir.glob('**/*.rb')
end
@spickermann
spickermann / gist:6606858
Last active December 23, 2015 08:19
A decorator that can return faked data for defined fields
# depends on: https://github.com/masover/blankslate
#
# user = User.new(:name => 'foo')
# user.name #=> 'foo'
# Decorator.new(user).name #=> 'foo'
#
# but
# Decorator.new(user, :name => 'bar').name #=> 'bar'
class Decorator < BlankSlate