View sample_vcr_config.rb
VCR.configure do |c|
c.cassette_library_dir = 'spec/vcr_cassettes'
c.hook_into :webmock # or :fakeweb
c.configure_rspec_metadata!
# Let's you set default VCR mode with VCR=all for re-recording
# episodes. :once is VCR default
record_mode = ENV["VCR"] ? ENV["VCR"].to_sym : :once
c.default_cassette_options = { :record => record_mode }
View gist:99b96429783754dc1810d922364922e8
threads Integer(ENV['PUMA_MIN_THREADS'] || 16), Integer(ENV['PUMA_MAX_THREADS'] || 16)
preload_app!
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
workers Integer(ENV['PUMA_WORKERS'] || 3)
before_fork do
View concurrent_action_method-rails5.rb
# an action method in a Rails controller, Rails5
def show
futures = 2.times.collect do |n|
Concurrent::Future.execute do
# NEED to wrap here, in case _any_ autoloading is going to happen,
# need to tell Rails so it can be done thread-safely. Or else we might
# get a deadlock.
Rails.application.executor.wrap do
SomeExpensiveNetworkOrIoWorker.new(n).call
end
View concurrent_action_method.rb
# NOT OKAY for Rails5, WILL deadlock if dev-mode class-reloading
# an action method in a Rails controller
def show
futures = 2.times.collect do |n|
Concurrent::Future.execute do
SomeExpensiveNetworkOrIoWorker.new(n).call
end
end
View shipping_spec.rb
require 'spec_helper'
require 'carmen'
# Really more of a set of integration tests, we're proving that tax is actually
# calculated how we want on an order
describe ByZipTaxCalculator do
let!(:taxable_category) { create(:tax_category, name: "Sales and Use Tax", is_default: true) }
let!(:non_taxable_category) { create(:tax_category, name: "Non Taxable") }
before do
View demo.rb
def my_method
yield
"method"
end
my_method { return 'block' }
# => raises `LocalJumpError: unexpected return`
View rspec_around_handles_ensure.rb
require 'rspec'
require 'rspec/autorun'
describe "foo" do
around do |example|
puts "This is before #{example.description}"
example.run
puts "This is after #{example.description}"
View slug.rb
module Slug
def self.customize(field: :name)
Module.new do
define_method :to_param do
public_send(field).downcase.gsub /\W+/, '-'
end
end
end
end
View quadrant.rb
class Todo
attr_accessor :title, :description
attr_reader :priority, :urgency
def initialize(priority: 1, urgency: 1)
@priority, @urgency = priority, urgency
raise ArgumentError, "urgency must be between 1 and 10" unless (1..10).cover?(@urgency)
raise ArgumentError, "priority must be between 1 and 10" unless (1..10).cover?(@priority)
end
def quadrant
View gist:e8ca8fae17eb3324d26d
class Todos
attr_accessor :priority, :urgency, :title, :description
def initialize(priority = 1, urgency = 1)
@priority, @urgency = priority, urgency
raise ArgumentError, "urgency must be between 1 and 10" unless 1..10 === @urgency
raise ArgumentError, "priority must be between 1 and 10" unless 1..10 === @priority
end
def quadrant
@quadrant ||=