README.md in blank_slate-1.0.0 vs README.md in blank_slate-1.1.0
- old
+ new
@@ -5,10 +5,13 @@
This gem allows you to create null objects without resorting to
method_missing to catch every message passed. Using method_missing
means that your objects will respond to any method regardless of
the behavior you designed in your other related classes.
+By default, a BlankSlate class will define methods to return `nil` so
+you don't have to.
+
## Installation
Add this line to your application's Gemfile:
gem 'blank_slate'
@@ -39,11 +42,11 @@
# Create a blank slate but provide a specific value for a certain method
GuestPresenter = BlankSlate(UserPresenter) do
def welcome
- "Sign-in or sign-up, cowboy!"
+ "Sign-in or sign-up, cowpoke!"
end
end
def Present(user)
user.authenticated? ? UserPresenter.new(user) : GuestPresenter.new(user)
@@ -55,9 +58,49 @@
presenter.recent_searches # unauthenticated outputs nothing
presenter.bad_method # raises an error in either case
```
In the example above the call to `bad_method` would have continued with a null object (using method_missing) but failed for the good object. With BlankSlate, we get the behavior we want from both our regular class and the null stand-in.
+
+### Common ancestor
+
+BlankSlate will preserve the methods of the ancestor of the inherited class.
+
+```ruby
+ class Visitor
+ attr_reader :auth_info
+ def authenticated?
+ auth_info.to_h.has_key?('token')
+ end
+ end
+
+ class User < Visitor
+ def name
+ auth_info.to_h['name']
+ end
+
+ def email
+ auth_info.to_h['email']
+ end
+ end
+
+ class Guest < BlankSlate(Visitor)
+ def name
+ "Guest"
+ end
+ end
+
+ user = User.new
+ user.auth_info = {'name' => 'Jim', 'email' => 'test@example.com', 'credentials' => 'valid stuff' }
+ user.name #=> "Jim"
+ user.authenticated? #=> true
+
+ guest = Guest.new
+ guest.auth_info = { }
+ guest.name #=> "Guest"
+ guest.authenticated? #=> false
+
+```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)