Class: Cfer::Block

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/cfer/block.rb

Overview

Represents the base class of a Cfer DSL

Instance Method Summary collapse

Instance Method Details

#build_from_block(*args, &block) ⇒ Object

Evaluates a DSL directly from a Ruby block, calling pre- and post- hooks.

Parameters:

  • args (Array<Object>)

    Extra arguments to be passed into the block.



10
11
12
13
14
15
# File 'lib/cfer/block.rb', line 10

def build_from_block(*args, &block)
  pre_block
  Docile.dsl_eval(self, *args, &block) if block
  post_block
  self
end

#build_from_file(*args, file) ⇒ Object

Evaluates a DSL from a Ruby script file

Parameters:

  • args (Array<Object>)

    (see: #build_from_block)

  • file (File)

    The Ruby script to evaluate



31
32
33
34
35
# File 'lib/cfer/block.rb', line 31

def build_from_file(*args, file)
  build_from_block(*args) do
    include_file(file)
  end
end

#build_from_string(*args, str, file) ⇒ Object

Evaluates a DSL from a Ruby string

Parameters:

  • args (Array<Object>)

    Extra arguments to be passed into the block

  • str (String)

    The Cfer source template to evaluate

  • file (File)

    The file that will be reported in any error messages



21
22
23
24
25
26
# File 'lib/cfer/block.rb', line 21

def build_from_string(*args, str, file)
  build_from_block(*args) do
    instance_eval str, file
  end
  self
end

#include_file(file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cfer/block.rb', line 37

def include_file(file)
  Preconditions.check(file).is_not_nil
  raise Cfer::Util::FileDoesNotExistError, "#{file} does not exist." unless File.exist?(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

#post_blockObject

Executed just after the DSL is evaluated



56
57
# File 'lib/cfer/block.rb', line 56

def post_block
end

#pre_blockObject

Executed just before the DSL is evaluated



52
53
# File 'lib/cfer/block.rb', line 52

def pre_block
end