Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am dnagir on github.
  • I am dnagir (https://keybase.io/dnagir) on keybase.
  • I have a public key ASBRKxPaRBzw3Jtnf172oWrOYt26nFkIkKk1fQBviIFkHwo

To claim this, I am signing this object:

require 'dry/validation'
require 'dry/types'
require 'dry/struct'
module Types
include Dry::Types()
end
class OrderContract < Dry::Validation::Contract
params do
@dnagir
dnagir / example.rb
Last active May 3, 2019 02:20
Dry Transaction Inheritance Repro
require 'bundler/inline'
gemfile do
gem 'dry-validation', '=1.0.0.rc1'
gem 'dry-transaction', '=0.13.0'
gem 'minitest', '=5.11.3'
end
require 'dry-validation'
require 'dry/transaction'
@dnagir
dnagir / maybe.rb
Last active October 18, 2017 00:58
Minimal Ruby Maybe monad
require 'singleton'
module Maybe
class Just < BasicObject
attr_reader :value
def initialize(value)
@value = value
end
@dnagir
dnagir / example.rb
Created April 19, 2016 08:53
ActiveRecord time truncation
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'
)
@dnagir
dnagir / application_helper.rb
Created August 18, 2015 06:15
Render a different variant in rails
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
# 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:
@dnagir
dnagir / output.sh
Created June 25, 2014 04:57
Unsafe Ruby vars
> 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.
@dnagir
dnagir / output.sh
Last active August 29, 2015 14:02
Chronic time_class (how come it doesn't break in Rubinius?)
> 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:
@dnagir
dnagir / eample.rb
Last active August 29, 2015 14:02
nil, nil, nil
# 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