-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subclasses of this class can easily delegate presenter methods to a backing object. A parameter with the same name as the class (e.g. `product` for `ProductPresenter`) must be passed and its value will receive delegated calls.
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'curly/presenter' | ||
|
||
module Curly | ||
class DelegatePresenter < Curly::Presenter | ||
def self.delegates(*methods) | ||
methods.each do |method| | ||
class_eval <<-RUBY | ||
def #{method} | ||
@#{delegatee_name}.#{method} | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
def self.inherited(subclass) | ||
self.presented_names += [subclass.delegatee_name] | ||
end | ||
|
||
def self.delegatee_name | ||
name.split("::").last.underscore.sub(/_presenter$/, "") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
describe Curly::DelegatePresenter do | ||
class PersonPresenter < Curly::DelegatePresenter | ||
delegates :name, :age | ||
end | ||
|
||
it "allows exposing methods on the passed object" do | ||
context = double(:context) | ||
person = double(:person, name: "Jane", age: 25) | ||
presenter = PersonPresenter.new(context, person: person) | ||
|
||
expect(presenter.name).to eq "Jane" | ||
expect(presenter.age).to eq 25 | ||
end | ||
end |