View bounded_hash.rb
# frozen_string_literal: true
require "delegate"
require "set"
# Hash that can only hold a certain number of keys, keys expire on a LRU basis
class BoundedHash < DelegateClass(Hash)
NO_OP = proc {}
View be_uniq.rb
RSpec::Matchers.define :be_uniq do
match do |actual|
values_match? actual.uniq, actual
end
failure_message do |actual|
diff = actual.dup
actual.uniq.each { |value| diff.delete_at diff.index(value) }
diff = diff.uniq
"expected that #{actual.inspect} to be uniq, but found the following repeated elements: #{diff.inspect}"
View fluent.rb
module Fluent
def fluent_accessor(*attr_names)
attr_names.each do |attr_name|
define_method attr_name do |*val|
return instance_variable_get("@#{attr_name}") if val.empty?
raise ArgumentError, "Expected 0..1 arguments, got #{val.size}" if val.size > 1
value = val.first
value = yield(value) if block_given?
View xor.txt
ternary = foo ? !bar : bar
xor = !!(foo ^ bar)
neq = foo != bar
foo | bar | xor | ternary | neq
-------------------------------------
true | true | false | false | false
false | false | false | false | false
true | false | true | true | true
false | true | true | true | true
View sns_publisher.js
var _ = require('lodash');
var Q = require('q');
var AWS = require('aws-sdk-q');
function SnsPublisher(topic, options) {
var topicOptions = {
params: {
TopicArn: topic
}
};
View each_day.rb
def days(range)
Enumerator.new do |yielder|
now = range.begin
begin
yielder << now if now <= range.end
end while (now = now + 1.day) <= range.end
end
end
days((Time.now - 3.days)..(Time.now)).each { |day| puts day }
View tracked_let.rb
# transaction/truncation strategies won't work when you have a
# capybara feature and you must preserve what's already in the
# database
#
# Usage:
# include TrackedLet
#
# track(:foo) { User.create }
# track!(:foo) { User.create }
# specify { expect(track(User.create)).to be_persisted }
View mail_chimp.rb
require "active_support/core_ext"
require "delegate"
require "mailchimp"
require "singleton"
class MailingList::MailChimp
attr_reader :user, :member_info
def initialize(user, member_fetcher = member_info_api)
@user = user
View mailing_list.rb
require "delegate"
require "singleton"
module MailingList
class SafeResponse < SimpleDelegator
def self.safe_delegate(*methods)
methods.each do |method|
define_method(method, ->(*args, &block) {
value = __getobj__.send(method, *args, &block)
value ? SafeResponse.new(value) : BlackHole.instance
View timecop_helper.rb
RSpec.configure do |config|
config.around(:each, :timecop_freeze) do |example|
time = example.metadata[:timecop_freeze]
time = Time.current.change(usec: 0) if TrueClass === time
Timecop.freeze(time) do
example.call
end
end
end