Skip to content

Instantly share code, notes, and snippets.

@olvap
Last active August 29, 2015 14:05
Show Gist options
  • Save olvap/973de1c3f782feb3d2a6 to your computer and use it in GitHub Desktop.
Save olvap/973de1c3f782feb3d2a6 to your computer and use it in GitHub Desktop.
class Plantilla
attr_accessor :body, :title, :strategy
def initialize strategy
@strategy = strategy.new(self)
end
def to_s
@strategy.output
end
end
class Format
def initialize plantilla
@plantilla = plantilla
end
end
class FormatHtml < Format
def output
"<html>
<head>
#{ format_title }
</head>
<body>
#{ format_body }
</body>
</html>"
end
def format_title
"<h1> #{ @plantilla.title } </h1>"
end
def format_body
@plantilla.body.join("</br>")
end
end
class FormatPlainText < Format
def output
"#{ format_title }#{ format_body }"
end
def format_title
@plantilla.title
end
def format_body
@plantilla.body.join("\n")
end
end
p = Plantilla.new FormatHtml
p.title = 'Hello World!'
p.body = ['What', 'a', 'nice', 'day!']
puts p
p = Plantilla.new FormatPlainText
p.title = 'Hello World!'
p.body = ['What', 'a', 'nice', 'day!']
puts p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment