Created
September 16, 2017 12:17
-
-
Save kopylovvlad/09131c9d7e6928a156e24a3186dc4e6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module TagCreator | |
def self.render(tag_object) | |
return '' if tag_object.nil? | |
return tag_object if tag_object.is_a?(::String) or tag_object.is_a?(::Integer) | |
str = "<#{tag_object.tag_name}>" | |
str += TagCreator.render(tag_object.content) | |
str += "</#{tag_object.tag_name}>" | |
str | |
end | |
end | |
class Tag | |
attr_reader :tag_name, :content | |
def initialize(tag_name, content) | |
@tag_name = tag_name | |
@content = content | |
end | |
end | |
class DivTag | |
attr_reader :content | |
def initialize(content) | |
@content = content | |
end | |
end | |
class DivTagAdapter | |
attr_reader :div_tag | |
def initialize(div_tag) | |
@div_tag = div_tag | |
end | |
def tag_name | |
'div' | |
end | |
def content | |
div_tag.content | |
end | |
end | |
p_tag = Tag.new('p', 'it is just paragraph') | |
puts TagCreator.render(p_tag) | |
# <p>it is just paragraph</p> | |
p_tag2 = Tag.new( | |
'div', | |
Tag.new('small', 'it is small text') | |
) | |
puts TagCreator.render(p_tag2) | |
# <div><small>it is small text</small></div> | |
div_tag = DivTag.new('hello from div') | |
puts TagCreator.render(DivTagAdapter.new(div_tag)) | |
# <div>hello from div</div> | |
div_tag = DivTag.new(Tag.new('small', 'hello')) | |
puts TagCreator.render(DivTagAdapter.new(div_tag)) | |
# <div><small>hello</small></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment