Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add POC for supporting ERB templating in YAML file #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/locomotive/steam/adapters/filesystem/yaml_loader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'erb'

module Locomotive::Steam
module Adapters
module Filesystem
Expand Down Expand Up @@ -34,11 +36,39 @@ def _load(path, frontmatter = false, &block)
end
end

def parse_erb(data)
def inject(file_path)
File.open(File.join(path, file_path)).read
end
ERB.new(data).result(binding)
end

def parse_erb_struct(vals)
if vals.is_a? Array
vals.each do | val |
parse_erb_struct(val)
end
else
vals.each do | key, val |
if val.is_a? String
vals[key] = parse_erb(val)
else
parse_erb_struct(val)
end
end
end
end

def safe_yaml_load(yaml, template, path, &block)
return {} if yaml.blank?

erb_activated = parse_erb(yaml) != yaml

begin
HashConverter.to_sym(YAML.load(yaml)).tap do |attributes|
if erb_activated
parse_erb_struct(attributes)
end
block.call(attributes, template) if block_given?
end
rescue Exception => e
Expand All @@ -49,7 +79,7 @@ def safe_yaml_load(yaml, template, path, &block)
def safe_json_load(path)
return {} unless File.exists?(path)

json = File.read(path)
json = parse_erb(File.read(path))

begin
MultiJson.load(json)
Expand Down