Created
October 22, 2013 04:37
-
-
Save rbmrclo/7095300 to your computer and use it in GitHub Desktop.
Liquid
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 Liquify | |
attr_accessor :content, :template | |
class ::InvalidInputException < Exception; end; | |
def initialize(content) | |
@content = content | |
end | |
def template | |
@template ||= Liquid::Template.parse @content | |
end | |
def variables | |
template.root.nodelist.select{|node| node.respond_to? :name}.map{|n| n.name} | |
end | |
def substitute(params = {}) | |
unless params.class == Hash || params.class == HashWithIndifferentAccess | |
raise InvalidInputException | |
end | |
template.render hashify params | |
end | |
def variables_in?(params = []) | |
unless params.class == Array | |
raise InvalidInputException | |
end | |
variables & params.map{|m| m.to_s} == variables | |
end | |
def variables_not_in?(params = []) | |
!variables_in? params | |
end | |
private | |
def hashify(params = {}) | |
hash = {} | |
params.each do |k,v| | |
hash[k.to_s] = v | |
end | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment