Module: RubyYacht::DSL::Base

Overview

This module provides the core functionality for providing configuration DSLs.

Classes that provide DSLs should include this module and extend the ClassMethods module.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#check_required_attributesObject

This method checks that all of the required attributes have been set on the object.

If they haven't, this will raise an exception.



298
299
300
301
302
303
304
305
306
# File 'lib/ruby_yacht/dsl/dsl.rb', line 298

def check_required_attributes
  attributes = self.singleton_class.required_attributes || []
  attributes.each do |name|
    value = instance_variable_get("@#{name}")
    if value == nil || value == ''
      raise "Missing required attribute #{name} for #{self.class}"
    end
  end
end

#check_server_type(server_type, container_type) ⇒ Object

This method checks that a server type has been registered.

If it has not, this will raise an exception.

Parameters

  • server_type: Symbol The server type that has been set on this DSL instance.
  • container_type: Symbol The container type that the server type must be registered for.


318
319
320
321
322
323
# File 'lib/ruby_yacht/dsl/dsl.rb', line 318

def check_server_type(server_type, container_type)
  result = RubyYacht.configuration.find_server_type(server_type)
  unless result && result.container_type == container_type
    raise "#{self.class.name} has invalid #{container_type} server type `#{server_type}`"
  end
end

#create_objectObject

This method creates an object with the attributes from this instance.

We will check that all the required attributes have been set first, and will raise an exception if they haven't.

The created type, and the attributes we set on it, will be defined by the call to the class method creates_object.

Returns

This will return the newly created object.



335
336
337
338
339
340
341
342
# File 'lib/ruby_yacht/dsl/dsl.rb', line 335

def create_object
  check_required_attributes
  object = self.class.created_type.new
  self.singleton_class.copied_attributes.each do |name|
    object.send("#{name}=", instance_variable_get("@#{name}"))
  end
  object
end

#initializeObject

This initializer creates a new DSL instance.



200
201
202
# File 'lib/ruby_yacht/dsl/dsl.rb', line 200

def initialize
  load_custom_attributes
end

#load_custom_attributesObject

This method adds attributes to this DSL instance's singleton DSL, based on the attributes defined by the app types that have been loaded.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ruby_yacht/dsl/dsl.rb', line 206

def load_custom_attributes
  full_class = self.class
  self.singleton_class.instance_eval do
    @copied_attributes = (full_class.copied_attributes || []).dup
    @all_attributes = full_class.all_attributes.dup
    @required_attributes = full_class.required_attributes.dup
    @default_values = full_class.default_values.dup
  end

  if self.class.created_type
    RubyYacht.configuration.server_types.each do |server_type|
      next if @server_type && server_type.name != @server_type
      if server_type.respond_to?(self.class.custom_attribute_method)
        attributes = server_type.send(self.class.custom_attribute_method)
        attributes.each do |attribute|
          attribute = attribute.merge(name: "#{server_type.name}_#{attribute[:name]}")
          load_custom_attribute(attribute)
        end
      end
    end
  end
end

#run(block) ⇒ Object

This method runs a block with DSL methods.

The block will reun in the instance context of the DSL object, so you can call the DSL methods directly.

It will also copy the default values into the attributes on the DSL object.

Parameters

  • block: Proc The block to run.


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ruby_yacht/dsl/dsl.rb', line 265

def run(block)
  defaults = self.singleton_class.default_values || {}
  defaults.each do |name, value|
    copy =
      case value
      when TrueClass, FalseClass, Fixnum, Symbol
        value
      else
        value.dup
      end
    instance_variable_set("@#{name}", copy)
  end
  if block
    instance_eval(&block)
  end
  
  type = RubyYacht.configuration.find_server_type(@server_type)
  if type
    type.server_defaults.each do |server_defaults|
      server_defaults.each do |key, value|
        if instance_variable_get("@#{key}") == nil
          instance_variable_set("@#{key}", value)
        end
      end
    end
  end
  self
end