Created
September 30, 2015 12:27
-
-
Save ArturT/f202b534d4a6f63cae43 to your computer and use it in GitHub Desktop.
Example of BasePresenter and BaseListPresenter
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
# BASE PRESENTERS | |
class BasePresenter | |
def initialize(item) | |
@item = item | |
end | |
def to_hash | |
raise NotImplementedError | |
end | |
def to_json | |
to_hash.to_json | |
end | |
private | |
attr_reader :item | |
end | |
class BaseListPresenter | |
def to_hash(items) | |
items.map { |item| item_presenter.new(item) } | |
end | |
def to_json | |
to_hash.to_json | |
end | |
def item_presenter | |
raise NotImplementedError | |
end | |
end | |
# BADGES PRESENTERS | |
class BadgePresenter < BasePresenter | |
def to_hash | |
{ | |
id: item.id, | |
name: item.name | |
} | |
end | |
end | |
class BadgeListPresenter < BaseListPresenter | |
def item_presenter | |
BadgePresenter | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment