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 / 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 / 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 / upsert.rb
Created June 26, 2016 18:37 — forked from aalavandhan/upsert.rb
Rails Upsert
module ActiveRecordExtension
extend ActiveSupport::Concern
def self.upsert(attributes)
begin
create(attributes)
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation => e
find_by_primary_key(attributes['primary_key']).
update(attributes)
end
@MatUrbanski
MatUrbanski / message_handler.rb
Created June 26, 2016 18:21 — forked from aalavandhan/message_handler.rb
A simple rails utility which cahces data from config/locales/*.yml files as a hash and returns a hash for each specified language file. It also parses and reloads the YAML files everytime they are changed.
module Util
class MessageHandler
@@data = {}
class << self
def data_in(language)
load! if !@@data[language] or (files_changed? and Rails.env.development?)
@@data[language][language.to_s]
@MatUrbanski
MatUrbanski / Dropbox.coffee
Created April 2, 2016 19:18 — forked from hugodias/Dropbox.coffee
Upload images from Dropbox to Carrierwave on Ruby on Rails
class Dropbox
constructor: ->
@options =
success: @success
cancel: ->
# Do something
linkType: 'direct'
multiselect: true
extensions: [
'.jpg'
@MatUrbanski
MatUrbanski / dog.rb
Created February 17, 2016 15:16 — forked from shiroyasha/dog.rb
Method tracer for Ruby classes
class Dog
attr_writer :name
def initialize(name)
@name = name
end
def bark
puts "patrick"
end
@MatUrbanski
MatUrbanski / default_file_writer.rb
Created February 1, 2016 18:37 — forked from KamilLelonek/default_file_writer.rb
Default FileFriter in Ruby
class DefaultFileWriter
def write(path, contents)
IO.write(path, contents)
end
end
@MatUrbanski
MatUrbanski / no_overwrite_file_writer.rb
Created February 1, 2016 18:37 — forked from KamilLelonek/no_overwrite_file_writer.rb
No overwrite FileWriter in Ruby
class NoOverwriteFileWriter
def initialize(default_file_writer)
@default_file_writer = default_file_writer
end
def write(path, contents)
fail_if_file_exist(path)
default_file_writer.write(path, contents)
end
@MatUrbanski
MatUrbanski / no_null_file_writer.rb
Created February 1, 2016 18:36 — forked from KamilLelonek/no_null_file_writer.rb
No null FileWriter in Ruby
class NoNullFileWriter
def initialize(default_file_writer)
@default_file_writer = default_file_writer
end
def write(path, contents)
fail_if_empty_contents(contents)
default_file_writer.write(path, contents)
end