Skip to content

Instantly share code, notes, and snippets.

@ArturT
Created September 30, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArturT/f202b534d4a6f63cae43 to your computer and use it in GitHub Desktop.
Save ArturT/f202b534d4a6f63cae43 to your computer and use it in GitHub Desktop.
Example of BasePresenter and BaseListPresenter
# 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