Created
December 6, 2012 14:24
-
-
Save rebo/4224791 to your computer and use it in GitHub Desktop.
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
require 'alias_dci' | |
class Article < Struct.new(:subject,:body);end | |
class TextView | |
attr_reader :contents | |
def initialize | |
@contents = "" | |
end | |
end | |
class GenerateAppendView | |
include AliasDCI::Context | |
role :displayable_article do | |
def summary | |
"#{subject} : #{body}" | |
end | |
end | |
role :canvas do | |
def render | |
@contents << " << " unless @contents.empty? | |
@contents << "#{displayable_article.summary}" | |
end | |
end | |
def initialize(article, view) | |
rebind(article, view) | |
end | |
def rebind(article, view) | |
assign_named_roles(:displayable_article => article, :canvas => view, :force => true) | |
end | |
def render | |
in_context do | |
canvas.render | |
end | |
end | |
end | |
article = Article.new("Hi there...", "I like turtles!") | |
text_view = TextView.new | |
view_context = GenerateAppendView.new(article,text_view) | |
view_context.render | |
text_view.contents # => "Hi there... : I like turtles!" | |
puts text_view.contents | |
article.subject = "And Now I Say ..GoodBye" | |
article.body = "Cyas..." | |
view_context.render | |
text_view.contents # => "Hi there... : I like turtles! << And Now I Say ..GoodBye : Cyas..." | |
puts text_view.contents | |
# >> Hi there... : I like turtles! | |
# >> Hi there... : I like turtles! << And Now I Say ..GoodBye : Cyas... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment