lib/cfer/block.rb in cfer-0.5.0.pre.rc1 vs lib/cfer/block.rb in cfer-0.5.0.pre.rc2
- old
+ new
@@ -1,6 +1,8 @@
require 'docile'
+require 'json'
+require 'yaml'
module Cfer
# Represents the base class of a Cfer DSL
class Block < ActiveSupport::HashWithIndifferentAccess
# Evaluates a DSL directly from a Ruby block, calling pre- and post- hooks.
@@ -25,10 +27,26 @@
# Evaluates a DSL from a Ruby script file
# @param args [Array<Object>] (see: #build_from_block)
# @param file [File] The Ruby script to evaluate
def build_from_file(*args, file)
- build_from_string File.read(file), file
+ build_from_block(*args) do
+ include_file(file)
+ end
+ end
+
+ def include_file(file)
+ Preconditions.check(file).is_not_nil
+ raise Cfer::Util::FileDoesNotExistError, "#{file} does not exist." unless File.exists?(file)
+
+ case File.extname(file)
+ when '.json'
+ deep_merge! JSON.parse(IO.read(file))
+ when '.yml', '.yaml'
+ deep_merge! YAML.load_file(file)
+ else
+ instance_eval File.read(file), file
+ end
end
# Executed just before the DSL is evaluated
def pre_block
end