Skip to content

Instantly share code, notes, and snippets.

@nelsonsar
nelsonsar / inmemory_storage.py
Created Nov 27, 2019
In memory Django storage based on FileSystemStorage implementation
View inmemory_storage.py
# 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
View bar.html
<p>I was included.</p>
@nelsonsar
nelsonsar / datetime_ext.rb
Last active Dec 7, 2017
Ruby DateTime iso8601 for week dates
View datetime_ext.rb
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 Oct 21, 2017
Explicação Law of Demeter
View lod.md

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.

View mocking_example.rb
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)