Created
September 1, 2014 12:00
-
-
Save mkrogh/95b9e53eba98f800361e to your computer and use it in GitHub Desktop.
A simple template for smaller template files.
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
class SimpleTemplate | |
attr_accessor :token_start, :token_end | |
def initialize(file) | |
@content = open(file).read | |
@token_start = "{{" | |
@token_end = "}}" | |
@token_status = :on | |
end | |
def replace(token,content) | |
@content.gsub!(wrap_token(token), content) | |
self | |
end | |
def replace_once(token,content) | |
@content.sub!(wrap_token(token), content) | |
self | |
end | |
def token(status) | |
@token_status = status | |
self | |
end | |
def save_file(file_name) | |
open(file_name, "w") do |file| | |
file.write(@content) | |
end | |
end | |
def build | |
@content | |
end | |
private | |
def wrap_token(token) | |
if @token_status == :on | |
/#{@token_start}#{token}#{@token_end}/ | |
else | |
/#{token}/ | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment