Skip to content

Instantly share code, notes, and snippets.

View rewinfrey's full-sized avatar
🏄‍♂️
fix f = let x = f x in x

Rick Winfrey rewinfrey

🏄‍♂️
fix f = let x = f x in x
View GitHub Profile
@rewinfrey
rewinfrey / large_spec_file_as_mockist.rb
Last active August 29, 2015 13:57
Large spec file as mockist
shared_examples_for 'a user search engine' do
let(:payment_service) { service_factory.create(:payment) }
let(:db) { search_memory_repository }
def ids(users)
users.map { |user| user[:id] }
end
def user_search(options)
@rewinfrey
rewinfrey / mock_out_encapsulation.rb
Last active August 29, 2015 13:57
Breaking encapsulation via mocking
describe Payment::Coupon::RecordCoupon do
let(:coupon) { double(:coupon) }
let(:coupon_repository) { double(:coupon_repository) }
let(:vendor_subscription) { double(:vendor_subscription) }
let(:sign_up) { double(:sign_up, :subscription => subscription) }
let(:subscription) { double(:subscription) }
subject { described_class.new(coupon_repository) }
context '#execute' do
# source: http://bugroll.com/ratcheting.html
# spec/ratchets/maximum_number_of_callbacks_allowed_spec.rb
describe "The maximum number of callbacks allowed" do
it "cannot exceed Ratchets::MAX_CALLBACKS" do
expect(Ratchets::CallbackCounter.count).to be <= Ratchets::MAX_CALLBACKS
end
end
# lib/ratchets.rb
module Ratchets
@rewinfrey
rewinfrey / stack_trace_to_array.rb
Created March 27, 2014 01:24
Converts a cucumber stack trace to array of files containing errors
stack_trace = %q(cucumber features/account/features/reset_password.feature:39 # Scenario: Friend enters invalid token
cucumber features/account/features/koalas/account_settings.feature:19 # Scenario: Koala clicks to save updated account info, passes validation
cucumber features/account/features/koalas/account_settings.feature:37 # Scenario: Koala clicks to cancel
cucumber features/account/features/koalas/account_settings.feature:57 # Scenario: Koala clicks to edit password
cucumber features/account/features/koalas/account_settings.feature:67 # Scenario: Koala clicks to cancel account
cucumber features/account/features/koalas/account_settings.feature:81 # Scenario: Koala confirms account cancelation
cucumber features/account/features/koalas/account_settings.feature:91 # Scenario: Koala cancels account cancelation
cucumber features/account/features/koalas/dashboard.feature:19 # Scenario: Koala can differ replied from unreplied messages by specific icon
cucumber features/account/features/koalas/dashboard.feature
class User; end
class Candidate < User; end
class Owner < User; end
Candidate.ancestors # => [Candidate, User, Object, Kernel, BasicObject]
Owner.ancestors # => [Owner, User, Object, Kernel, BasicObject]
User.ancestors # => [User, Object, Kernel, BasicObject]
class Candidate
class << self
@rewinfrey
rewinfrey / heredoc_remove_leading_whitespace.rb
Last active August 29, 2015 14:02
Simple example for removing leading whitespace with Ruby's heredoc operator
# This is not as sophisticated as ActiveSupport's `strip_heredoc`, but is the simplest solution for
# removing leading whitespace.
# For more nuanced whitespace removal, take a look at http://apidock.com/rails/String/strip_heredoc
puts "With leading whitespace:"
puts <<-TEST
This is a test.
To remove leading spaces.
From Ruby's heredoc operator.
TEST
@rewinfrey
rewinfrey / rails_application_tuning.md
Last active August 29, 2015 14:04
Rails Large Application Tuning

Notes from Tuning Legacy Rails App: How to Make an Elephant Sprint

###Measuring performance

  • Monitor the values of specific code paths and graph them to see performance over time (response times as one example metric)
  • Automated tests that measure performance can fail based on a set threshold
    • If a given code path exceeds 20% of the existing response time, then the automated test fails, alerting ops and devs that a recent code change has negatively impacted performance beyond an pre-defined SLA or threshold
  • Need a production like environment
  • Make that performance test environment exclusive to performance testing (don't let regular usage or QA usage affect the test results)
  • Using NewRelic to compare boxes against each other
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix
;; and optionally can refer functions to the current ns.
;;
;; * :import refers Java classes to the current namespace.
;;
;; * :refer-clojure affects availability of built-in (clojure.core)
;; functions.
@rewinfrey
rewinfrey / struct_class.rb
Last active August 29, 2015 14:09
model like struct class
class Location < Struct.new(:city,
:state,
:zipcode)
def initialize(from_hash)
super(*from_hash.values_at(*members))
end
def display_city
city.capitalize
@rewinfrey
rewinfrey / create_new_rails_app.rb
Last active August 29, 2015 14:13
Creating New Meditation Rails App
# Regular comments are explanations
# Comments prefaced with '$' indicate commands to be entered in the terminal (operating system is assumed to be OSx 10.10.1)
# Comments prefaced with 'postgres=#' indicate commands to be entered in the postgresql console
# Ruby code uncommented is meant to be added to the specified file and can be copied and pasted (although I strongly encourage you to type it yourself)
# I'm using Rails 4.2 for this example
# $ gem install rails -v 4.2
# I'm assuming the use of postgresql for this application
# Let's initialize our meditation DB