by Jim Gay: http://confreaks.tv/videos/rubyconf2014-eastward-ho-a-clear-path-through-ruby-with-oo
- Always return
self
. - Objects may (only) query themselves.
- Factories are exempt (e.g.
new
) - Break the rules sparingly (e.g. value objects)
def test_returns_2_when_x_is_1_and_y_is_1(self): | |
self.assertEqual(2, sum(1, 2)) | |
def test_returns_1_when_x_is_2_and_y_is_neg1(self): | |
self.assertEqual(1, sum(2, -1)) | |
# etc.... | |
# versus: |
jblake@sauron:~$ man help | |
No manual entry for help | |
jblake@sauron:~$ help man | |
-bash: help: no help topics match `man'. Try `help help' or `man -k man' or `info man'. |
def test_subdirectory_gets_linked_to_parent(self): | |
# When a Site with ResourceDirectories is serialized and passed in for | |
# duplication, we have to re-link the parent child hierarchy since | |
# primary keys have changed. | |
... |
class Observable(object): | |
"""Mixin for use cases that can fire events to be handled by observers""" | |
def __init__(self): | |
self._observers = [] | |
def attach_observer(self, observer): | |
self._observers.append(observer) | |
def notify_observers(self, event, *args, **kwargs): |
class PropertyObserverExample | |
def number | |
@number ||= 0 | |
end | |
def number=(new_number) | |
old_number = @number | |
puts "About to change to #{new_number}" | |
@number = new_number | |
puts "Just changed from #{old_number} to #{new_number}!" |
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( |
class Factory | |
def foo(**kwargs) | |
kwargs[:bar] ||= bar | |
Foo.new(**kwargs) | |
end | |
def bar(**kwargs) | |
kwargs[:baz] ||= baz | |
Bar.new(**kwargs) | |
end |
# 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') |
by Jim Gay: http://confreaks.tv/videos/rubyconf2014-eastward-ho-a-clear-path-through-ruby-with-oo
self
.new
)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:) { |