Skip to content

Instantly share code, notes, and snippets.

@nelsonsar
nelsonsar / inmemory_storage.py
Created November 27, 2019 21:37
In memory Django storage based on FileSystemStorage implementation
# Depends on https://docs.pyfilesystem.org/en/latest/reference/memoryfs.html
import errno
from pathlib import PurePosixPath
from django.conf import settings
from django.core.files.storage import Storage
from django.core.files import File
from django.utils._os import abspathu, safe_join
from django.utils.encoding import filepath_to_uri, force_text
from django.utils.six.moves.urllib.parse import urljoin
<p>I was included.</p>
@nelsonsar
nelsonsar / datetime_ext.rb
Last active December 7, 2017 03:35
Ruby DateTime iso8601 for week dates
class DateTime
# Regex to match Week date from ISO8601 represented as YYYY-Www
# For more information about Week date see: https://en.wikipedia.org/wiki/ISO_8601#Week_dates
#
SHORT_FORMAT_WEEK_DATE_REGEX = /\A[0-9]{4}-W([0-4]\d|5[0-2])\z/.freeze
class << self
alias_method :ruby_iso8601, :iso8601
# Since Ruby supports the longer week date (YYYY-Www-D) and 2017-W29 is the
@nelsonsar
nelsonsar / lod.md
Created October 21, 2017 01:39
Explicação Law of Demeter

Law of Demeter

Motivação

Diminuir o acomplamento entre objetos.

Como?

A lei sugere que objetos só podem falar com seus "amigos" e não com "estranhos". Essa ideia faz com que um objeto não conheça a estrutura interna de outro objeto e, se por um caso surja essa necessidade a API deve ser modificada para evitar que isso aconteça.

@nelsonsar
nelsonsar / mocking_example.rb
Created October 28, 2016 15:19
Exemplo de mock
require 'spec_helper'
describe Employee do
describe '#salary' do
it 'return amount based on employee region' do
region = double
expect(region).to receive(:salary_by)
.with('Nelson').and_return(5000)
employee = Employee.new('Nelson', region)