Skip to content

Instantly share code, notes, and snippets.

View keybase.md

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:

View dry_validation_and_struct.rb
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
Dry Transaction Inheritance Repro
View example.rb
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 Oct 18, 2017
Minimal Ruby Maybe monad
View maybe.rb
require 'singleton'
module Maybe
class Just < BasicObject
attr_reader :value
def initialize(value)
@value = value
end
@dnagir
dnagir / example.rb
Created Apr 19, 2016
ActiveRecord time truncation
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'
)
@dnagir
dnagir / application_helper.rb
Created Aug 18, 2015
Render a different variant in rails
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:
@dnagir
dnagir / output.sh
Created Jun 25, 2014
Unsafe Ruby vars
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.
@dnagir
dnagir / output.sh
Last active Aug 29, 2015
Chronic time_class (how come it doesn't break in Rubinius?)
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:
@dnagir
dnagir / eample.rb
Last active Aug 29, 2015
nil, nil, nil
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