Class: Bocuse::File

Inherits:
Object
  • Object
show all
Defined in:
lib/bocuse/file.rb

Overview

Represents a bocuse File.

This is usually a file that contains one or more of the following directives:

* node
* template

It is used to generate a config hash from the given files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, project, context) ⇒ File

Returns a new instance of File.



16
17
18
19
20
# File 'lib/bocuse/file.rb', line 16

def initialize(path, project, context)
  @path = path
  @project = project
  @context = context
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/bocuse/file.rb', line 14

def path
  @path
end

Instance Method Details

#evaluateObject

Returns a config hash.

Call to_h on the returned configuration to get the config hash.

Will return nil if there is no configuration to speak of inside the file. Use “node” or “template” to define a configuration.



29
30
31
32
33
# File 'lib/bocuse/file.rb', line 29

def evaluate
  ::File.open path, 'r' do |file|
    self.instance_eval file.read, file.path
  end
end

#node(name) ⇒ Object

The files read by #evaluate will trigger these methods.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bocuse/file.rb', line 37

def node name
  node_context = Bocuse::NodeContext.new(name, @context)
  configuration = Bocuse::Configuration.new

  unit = Bocuse::Unit.new(Proc.new, @project)
  unit.call(configuration, node_context)

  @project.register_node name, configuration
  
  return configuration
end

#templateObject



48
49
50
51
52
53
54
55
# File 'lib/bocuse/file.rb', line 48

def template
  # Delay template evaluation until someone tries to call include_template.
  # At that time, we'll have a configuration object to have the template
  # manipulate. 
  unit = Bocuse::Unit.new(Proc.new, @project)
  @project.register_template path, unit
  unit
end