Class: EnvironmentBuilder

Inherits:
UsesAttributes show all
Defined in:
lib/hosties/Reification.rb

Overview

Turn a description into a useful data structure - and it’s validated!

Instance Method Summary collapse

Methods inherited from UsesAttributes

#metaclass

Constructor Details

#initialize(type) ⇒ EnvironmentBuilder

Returns a new instance of EnvironmentBuilder.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hosties/Reification.rb', line 74

def initialize(type)
  if Hosties::EnvironmentDefinitions[type].nil? then
    raise ArgumentError, "Unrecognized environment type"
  end
  @hosts = {} # host type => array of hosts' data
  @type = type
  @definition = Hosties::EnvironmentDefinitions[@type]
  super(@definition) # Creates attribute code
  # More magic, this time create a parameterized host builder based 
  # on the type of hosts this environment wants. Poor man's currying
  @definition.hosts.each do |host_type|
    self.metaclass.send(:define_method, host_type) do |hostname, &block|
      begin
        builder = HostBuilder.new(host_type, hostname)
        builder.instance_eval(&block)
        if @hosts[host_type].nil? then @hosts[host_type] = [] end
        @hosts[host_type] << builder.finish
      rescue ArgumentError => ex
        #puts "Problem declaring host: #{ex}"
        raise ex
      end
    end
  end
end

Instance Method Details

#finishObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hosties/Reification.rb', line 99

def finish
  # Verify all of the required hosts were set
  @definition.hosts.each do |host_type| 
    raise ArgumentError, "Missing #{host_type} host" unless @hosts.include? host_type
  end
  retval = super.merge({ :hosts => @hosts })
  Hosties::Environments[@type] << retval
  # Add ourselves into the grouped listing if necessary
  attr = @definition.grouping
  unless attr.nil? then # TODO: This is really ugly
    if Hosties::GroupedEnvironments[@type].nil? then
      Hosties::GroupedEnvironments[@type] = Hash.new{|h,k| h[k] = []}
    end
    Hosties::GroupedEnvironments[@type][retval[attr]] << retval
  end
  retval
end