Skip to content

Commit

Permalink
Use Erubis to remove/convert Erb before parsing JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Gibbs committed Oct 19, 2011
1 parent 479f5ed commit 97d0c92
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Gemfile.lock
@@ -1,12 +1,14 @@
PATH
remote: .
specs:
ecology (0.0.11)
ecology (0.0.12)
erubis
multi_json

GEM
remote: http://rubygems.org/
specs:
erubis (2.7.0)
minitest (2.3.0)
mocha (0.9.12)
multi_json (1.0.3)
Expand Down
2 changes: 0 additions & 2 deletions TODO
@@ -1,5 +1,3 @@
* Use Erubis before evaluating

* On Ecology.read calls, make sure ecology filename hasn't changed (and warn if it has)

* Have Ecology.read try to do a read relative to the executable's directory first rather than relative to cwd.
Expand Down
1 change: 1 addition & 0 deletions ecology.gemspec
Expand Up @@ -28,6 +28,7 @@ EOS
s.require_paths = ["lib"]

s.add_dependency "multi_json"
s.add_dependency "erubis"

s.add_development_dependency "bundler", "~> 1.0.10"
s.add_development_dependency "scope", "~> 0.2.1"
Expand Down
26 changes: 21 additions & 5 deletions lib/ecology.rb
Expand Up @@ -39,10 +39,15 @@ def read(ecology_pathname = nil)
mutex.synchronize do
return if @ecology_initialized

file_path = ENV['ECOLOGY_SPEC'] || ecology_pathname || default_ecology_name
if File.exist?(file_path)
@data = {}
contents = merge_with_overrides(file_path)
filelist = [ENV["ECOLOGY_SPEC"], ecology_pathname, default_ecology_name]
filelist.detect do |file_path|
if file_path && (File.exist?(file_path) || File.exist?(file_path + ".erb"))
@data = {}
contents = merge_with_overrides(file_path)
true
else
false
end
end

@application ||= File.basename($0)
Expand Down Expand Up @@ -103,7 +108,18 @@ def publish_event(event)
end

def merge_with_overrides(file_path)
contents = File.read(file_path)
if File.exist?(file_path + ".erb")
contents = File.read(file_path + ".erb")

require "erubis"
var_hash = {
:ecology_version => Ecology::VERSION,
:filename => "#{file_path}.erb",
}
contents = Erubis::Eruby.new(contents).result(var_hash)
else
contents = File.read(file_path)
end
file_data = MultiJson.decode(contents);

return unless file_data
Expand Down
2 changes: 2 additions & 0 deletions test/ecology_test.rb
Expand Up @@ -16,6 +16,7 @@ class EcologyTest < Scope::TestCase

should "respect the ECOLOGY_SPEC environment variable" do
ENV['ECOLOGY_SPEC'] = '/tmp/bobo.txt'
File.expects(:exist?).with('/tmp/bobo.txt.erb').returns(false).at_least_once
File.expects(:exist?).with('/tmp/bobo.txt').returns(true)
File.expects(:read).with('/tmp/bobo.txt').returns('{ "application": "foo_app" }')
Ecology.read
Expand All @@ -31,6 +32,7 @@ class EcologyTest < Scope::TestCase
$0 = "whatever_app.rb"

ENV['ECOLOGY_SPEC'] = nil
File.expects(:exist?).with("whatever_app.ecology.erb").returns(false).at_least_once
File.expects(:exist?).with("whatever_app.ecology").returns(false)

Ecology.read
Expand Down
46 changes: 46 additions & 0 deletions test/erubis_test.rb
@@ -0,0 +1,46 @@
require File.join(File.dirname(__FILE__), "test_helper.rb")
require "multi_json"

class ErubisTest < Scope::TestCase
setup do
Ecology.reset
end

context "with a .erb ecology" do
setup do
set_up_ecology(<<ECOLOGY_CONTENTS, "some_app.ecology.erb")
{
"application": "SomeApp",
"domain": {
"property1" :
<% if ENV["BOBO"] %>
37
<% else %>
42
<% end %>
},
"bobo": <%= MultiJson.encode(ENV["BOBO"]) %>
}
ECOLOGY_CONTENTS

ENV["BOBO"] = nil
end

should "Parse conditionally with Erubis" do
ENV["BOBO"] = "true"
Ecology.read
assert_equal 37, Ecology.property("domain::property1")
end

should "Parse conditionally with Erubis when a variable is unset" do
Ecology.read
assert_equal 42, Ecology.property("domain::property1")
end

# should "Return values from Erubis" do
# ENV["BOBO"] = { "a" => "b", "c" => "d" }
# Ecology.read
# assert_equal { "a" => "b", "c" => "d" }, Ecology.property("bobo")
# end
end
end
10 changes: 9 additions & 1 deletion test/test_helper.rb
Expand Up @@ -10,7 +10,15 @@

class Scope::TestCase
def set_up_ecology(file_contents, filename = "some.ecology")
ENV["ECOLOGY_SPEC"] = filename
match = filename.match(/^(.*)\.erb$/)
if match
ENV["ECOLOGY_SPEC"] = match[1]
File.stubs(:exist?).with(match[1]).returns(false)
else
ENV["ECOLOGY_SPEC"] = filename
end

File.stubs(:exist?).with(filename + ".erb").returns(false)
File.stubs(:exist?).with(filename).returns(true)
File.expects(:read).with(filename).returns(file_contents).at_least_once
end
Expand Down
2 changes: 1 addition & 1 deletion test/trigger_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), "test_helper.rb")

class EnvironmentTest < Scope::TestCase
class TriggerTest < Scope::TestCase
setup do
Ecology.reset
end
Expand Down

0 comments on commit 97d0c92

Please sign in to comment.