This file contains hidden or 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
# 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 {} |
This file contains hidden or 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
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}" |
This file contains hidden or 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 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? | |
This file contains hidden or 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
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 |
This file contains hidden or 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
var _ = require('lodash'); | |
var Q = require('q'); | |
var AWS = require('aws-sdk-q'); | |
function SnsPublisher(topic, options) { | |
var topicOptions = { | |
params: { | |
TopicArn: topic | |
} | |
}; |
This file contains hidden or 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
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 } |
This file contains hidden or 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
# 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 } |
This file contains hidden or 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 "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 |
This file contains hidden or 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 "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 |
This file contains hidden or 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
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 |