Skip to content

Instantly share code, notes, and snippets.

View blaix's full-sized avatar

Justin Blake blaix

View GitHub Profile
@blaix
blaix / dependency-injection.py
Created July 30, 2015 18:25
Dependency injection example in python
class MySqlDb(object):
def run(self, query):
# whatever...
# ----------------------------------------------------------------------
# Here, Foo can get bars, AND knows exactly what db implementation to use:
class Foo:
def __init__(self):
@blaix
blaix / framework.md
Last active September 18, 2015 20:56
What I want in a web dev framework

What I want:

Single-purpose "action" objects.

I want to look at a project's file structure and see what it does. Something like actions/create_article.rb that has a CreateArticle class who's only job is to create articles.

No controllers/viewsets that handle an entire set of CRUD operations on a resource.

Don't push request/response stuff into the business logic layer.

# single-purpose objects for handling requests:
class CreateArticle
attr_reader :repo, :validations
def initialize(repo:, validator:)
@repo = repo
@validator = validator
end
def call(params)
@blaix
blaix / model-design.py
Last active August 29, 2015 14:20
Does a @Property with side effects mean "use a method"? I say no, but it does tell us there's a problem with the design.
# Let's say we have a service class:
class Service(object):
def get_article(self, id):
return requests.get('/articles/' + id).json
# For the model, something like this is what we've been doing:
class Article(object):
def init(self, id)
self.id = id
self.data = {}
def my_decorator(some_argument):
def my_decorator_implementation(func):
@wraps(func)
def wrapped_func(self, *args, **kwargs):
# do stuff with func and some_argument
return wrapped_func
return my_decorator_implementation
@blaix
blaix / wren.rb
Last active August 29, 2015 14:17
A ruby web-dev framework that favors lots of small objects and dependency injection over DSLs and magic methods.
require "wren/application"
# The simplest Wren application has a route and a handler:
class HelloApp
extend Wren::Routing # provides get/put/post/etc methods
extend Wren::Application # provides handle and mount methods
# and a rack-compatible call method
handle get("/hello/:name"), with: -> (name:, request:, response:) {
@blaix
blaix / east-oriented-code.md
Created March 1, 2015 22:48
The 4 Rules of East-Oriented Code
@blaix
blaix / adrian.py
Created February 24, 2015 12:02
Unit vs Integration testing in python using mocks
# consider this simple password hasher (over-simplified of course):
def password_hash(self, salt, password):
return (salt + password).reverse
# the unit tests are simple input vs output style:
def test_password_hash_returns_reverse_concatenated_salt_and_password(self):
hashed = password_hash('foo', 'bar')
self.assertEqual(hashed, 'raboof')
@blaix
blaix / gist:5a6a199beab5de8b8ab9
Created January 22, 2015 21:31
DI container in ruby
class Factory
def foo(**kwargs)
kwargs[:bar] ||= bar
Foo.new(**kwargs)
end
def bar(**kwargs)
kwargs[:baz] ||= baz
Bar.new(**kwargs)
end
index 69fa316..932df6a 100644
--- a/tddjango/pages/tests/test_views.py
+++ b/tddjango/pages/tests/test_views.py
@@ -7,7 +7,9 @@ from pages.views import PageView
class TestPageView(TestCase):
def setUp(self):
self.page_repository = Mock()
- self.view = PageView.as_view(repository=self.page_repository)
+ self.page_presenter = Mock()
+ self.view = PageView.as_view(