Skip to content

Instantly share code, notes, and snippets.

View MatUrbanski's full-sized avatar

Mateusz Urbański MatUrbanski

View GitHub Profile
@MatUrbanski
MatUrbanski / dollar.rb
Created July 21, 2022 11:48 — forked from JoelQ/dollar.rb
Implementing value object semantics as an RSpec shared_example.
class Dollar
attr_reader :cents
def initialize(cents:)
@cents = cents
end
def hash
[self.class, cents].hash
end
@MatUrbanski
MatUrbanski / my_operation_monads.rb
Created January 22, 2021 10:58
my_operation_monads.rb
class MyOperation
include Dry::Monads
def call(data)
validate(data).bind(method(:log)).bind(method(:persist))
end
def validate(data)
if data.valid?
Success(name: data.name, age: data.user_age)
@MatUrbanski
MatUrbanski / .irbc
Created September 30, 2020 11:59
My .irbc file
require 'irb/completion'
require 'rubygems'
ActiveRecord::Base.logger.level = 1 if defined?(ActiveRecord)
IRB.conf[:SAVE_HISTORY] = 1000
# Overriding Object class
class Object
# Easily print methods local to an object's class
def lm
@MatUrbanski
MatUrbanski / benchmark.rb
Created July 22, 2020 15:46 — forked from jodosha/benchmark.rb
Ruby Method Overloading
require "benchmark/ips"
require_relative "./method_overloading"
class Foo
include MethodOverloading
def call(number)
"foo #{number}"
end
end
@MatUrbanski
MatUrbanski / naive_encrypted_attributes_for_rails_active_model.rb
Last active July 12, 2020 15:21
Native encrypted attributes for Rails ActiveModel
# app/lib/encryptor.rb
class Encryptor
def initialize(key: Rails.application.secret_key_base)
@key = key[0...32]
end
def encrypt(unencrypted_string)
_cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt
_cipher.key = @key
@MatUrbanski
MatUrbanski / gemsepc.rb
Created March 2, 2020 10:33
gemspec.rb
# frozen_string_literal: true
require_relative "lib/emoticon/version"
Gem::Specification.new do |spec|
spec.name = "emoticon"
spec.version = Emoticon::VERSION
spec.summary = "Display emoticons in your terminal"
spec.description = <<~DESC
Display emoticons in your terminal. Communicate with your
@MatUrbanski
MatUrbanski / errors_trick.rb
Created February 11, 2019 14:08
errors trick
class SomeAPIService
def self.call
perform_action
rescue SomeAPIAccountExpired
send_email_with_notification_about_expired_account
rescue SomeAPIInvalidAPIKey
send_email_with_notification_about_invalid_api_key
rescue SomeAPIUnauthorizedAction
Rollbar.error('log some useful info')
end
@MatUrbanski
MatUrbanski / my_sigils.ex
Created February 22, 2017 16:56 — forked from blackode/my_sigils.ex
Custom Sigils
defmodule MySigils do
@moduledoc ~S"""
Genrating the custom sigils.
"""
@doc ~S"""
This converts the given strings in to the path by joining each string with /.
If you provide an option `u` it will treat the first string as domain and prepend
that string with https://www. and add the rest of strings as path.
## Examples
@MatUrbanski
MatUrbanski / rspec_expectations_cheat_sheet.rb
Created August 9, 2016 17:42
rspec expectations cheat sheet
RSpec.describe 'Common, built-in expectation matchers' do
example 'Equality' do
expect('x'+'y').to eq('xy') # a == b
expect('x'+'y').to eql('xy') # a.eql?(b)
expect('x'+'y').not_to be('xy') # a.equal?(b)
end
example 'Strings' do
expect('abcd').to include('bc')
expect('abcd').to start_with 'ab'
@MatUrbanski
MatUrbanski / custom_matcher.rb
Created August 9, 2016 17:36
rspec custom matcher
# spec/spec_helper.rb
RSpec.configure do |config|
# [...] some configs [...]
RSpec::Matchers.define :be_the_same_monster_as do |expected_monster|
match do |actual_monster|
actual_monster.name == expected_monster.name &&
actual_monster.skin_color == expected_monster.skin_color &&