Class: Cfer::Block
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- Cfer::Block
- Defined in:
- lib/cfer/block.rb
Overview
Represents the base class of a Cfer DSL
Direct Known Subclasses
Instance Method Summary collapse
-
#build_from_block(*args, &block) ⇒ Object
Evaluates a DSL directly from a Ruby block, calling pre- and post- hooks.
-
#build_from_file(*args, file) ⇒ Object
Evaluates a DSL from a Ruby script file.
-
#build_from_string(*args, str, file) ⇒ Object
Evaluates a DSL from a Ruby string.
- #include_file(file) ⇒ Object
-
#post_block ⇒ Object
Executed just after the DSL is evaluated.
-
#pre_block ⇒ Object
Executed just before the DSL is evaluated.
Instance Method Details
#build_from_block(*args, &block) ⇒ Object
Evaluates a DSL directly from a Ruby block, calling pre- and post- hooks.
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
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
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_block ⇒ Object
Executed just after the DSL is evaluated
56 57 |
# File 'lib/cfer/block.rb', line 56 def post_block end |
#pre_block ⇒ Object
Executed just before the DSL is evaluated
52 53 |
# File 'lib/cfer/block.rb', line 52 def pre_block end |