View maybe.rb
require 'singleton'
module Maybe
class Just < BasicObject
attr_reader :value
def initialize(value)
@value = value
end
View example.rb
gem 'activerecord', '4.2.5'
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => 'x'
)
View application_helper.rb
module ApplicationHelper
def with_variant(new_variant, &block)
old_variants = lookup_context.variants
begin
lookup_context.variants = [new_variant]
return block.call
ensure
lookup_context.variants = old_variants
end
end
View shared_subject.rb
# spec/support/shared_subject.rb
module RSpecSharedSubjectMacros
# Usage:
# 1. Replace 'subject' with 'shared_subject' to share the first result acroos the whole context
# 2. (optional) replace 'before(:each)' blocks with 'shared_setup'
# 3. Done. The subject will be shared within one, single context (nested will be re-setup again)
#
# DANGER NOTE: Make sure you accept the disadvantages of this, such as:
View output.sh
> ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
> ruby test.rb
Run options: --seed 22679
# Running:
F.
Finished in 2.320485s, 0.8619 runs/s, 172.3778 assertions/s.
View output.sh
> rvm use ruby-2.1.1
Using /Users/dnagir/.rvm/gems/ruby-2.1.1
> ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
> ruby test.rb
Run options: --seed 49028
# Running:
View eample.rb
# In the context of Rails app (mailers)
# Somewhere in email helper
def from_address_by(person_name)
return nil unless person_name
address = Mail::Address.new AppConfig.email.from # ex: "john@example.com"
address.display_name = person_name # ex: "John Doe"
address.format # returns "John Doe <john@example.com>"
end
View timezone.rb
# spec/support/timezone.rb
module TimeZoneHelpers
extend ActiveSupport::Concern
def self.randomise_timezone!
offsets = ActiveSupport::TimeZone.all.group_by(&:formatted_offset)
zones = offsets[offsets.keys.sample] # Random offset to better vary the time zone differences
Time.zone = zones.sample # Random zone from the offset (can be just 1st, but let's do random)
puts "Current rand time zone: #{Time.zone}. Repro: Time.zone = #{Time.zone.name.inspect}"
end
View README.md
View cloudinary_upload.rb
module Jobs
class CloudinaryUpload < Struct.new(:uploader_class, :model, :field, :file_name, :content_type, :data)
include Logging
def upload
uploader = uploader_class.new(model, field)
store = Cloudinary::CarrierWave::Storage.new(uploader)
io = Cloudinary::Blob.new(data, original_filename: file_name, content_type: content_type)
uploader.cache!(io)
store.store!(uploader.file)