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.


287
288
289
290
291
292
293
294
295
# File 'lib/ruby_yacht/dsl/dsl.rb', line 287

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

#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.


307
308
309
310
311
312
313
314
# File 'lib/ruby_yacht/dsl/dsl.rb', line 307

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.


202
203
204
# File 'lib/ruby_yacht/dsl/dsl.rb', line 202

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.


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

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.

267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ruby_yacht/dsl/dsl.rb', line 267

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
  instance_eval(&block)
  self
end