Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Restructure environments some to allow parent environment paths to be…
Browse files Browse the repository at this point in the history
… relative to the file rather than cwd; add functional examples for environments
  • Loading branch information
David Bresson committed Dec 4, 2016
1 parent 14dee48 commit c6f029d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions examples/environments/child.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
environment 'child', 'simple.rb' do |e|
e.bar = 'baz'
end
3 changes: 3 additions & 0 deletions examples/environments/simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
environment 'simple' do |e|
e.foo = "bar"
end
33 changes: 21 additions & 12 deletions lib/bedouin/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
module Bedouin
class Environment < OpenStruct
def self.parse(filename)
DSL.new.instance_eval File.read(filename)
DSL.new(filename).evaluate
end

def self.resolve_parent(parent)
def initialize(n,base={})
super(base)
name = n
end

class DSL
def initialize(filename)
@filename=filename
end

def evaluate
self.instance_eval(File.read(@filename), @filename)
end

def resolve_parent(parent)
parent_hash = case parent
when String, File
Environment.parse(parent)
when String
parent_path = File.expand_path(parent, File.dirname(@filename))
Environment.parse(parent_path)
when NilClass
nil
else
Expand All @@ -20,16 +35,10 @@ def self.resolve_parent(parent)
end

parent_hash
end

def initialize(n,base={})
super(base)
name = n
end
end

class DSL
def environment(name,parent=nil,&block)
parent_hash = Environment.resolve_parent(parent)
parent_hash = resolve_parent(parent)
e = Environment.new(name,parent_hash)
e.instance_eval &block if block_given?
return e
Expand Down
6 changes: 3 additions & 3 deletions spec/bedouin/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
hashlike.define_singleton_method(:to_h) do
{ hashlike: "foo" }
end
expect(Bedouin::Environment.resolve_parent(hashlike).to_h).to eq({ hashlike: "foo" })
expect(Bedouin::Environment::DSL.new('foo').resolve_parent(hashlike).to_h).to eq({ hashlike: "foo" })
end

it "raises an ArgumentError for parent objects that aren't hash-like or strings/files" do
expect { Bedouin::Environment.resolve_parent(Object.new) }.to raise_error(ArgumentError)
it "raises an ArgumentError for parent objects that aren't hash-like or path strings that parse to hash-like" do
expect { Bedouin::Environment::DSL.new('foo').resolve_parent(Object.new) }.to raise_error(ArgumentError)
end
end

0 comments on commit c6f029d

Please sign in to comment.