module RubyYacht::DSL::Base::ClassMethods

This module provides class methods for defining DSLs.

Public Instance Methods

add_attribute(name, default = nil, required = true) click to toggle source

This method adds a standard attribute to the DSL.

This will create a method that you can invoke with a single parameter, which provides the new value for the attribute.

### Parameters

  • **name: Symbol** The name of the attribute, which will also be

    the name of the method.
  • **default: Object** The default value for the attribute.

  • **required: Bool** Whether we should require the user to provide

    a value for the attribute.
# File lib/ruby_yacht/dsl/dsl.rb, line 82
def add_attribute(name, default = nil, required = true)
  add_generic_attribute name, name, default, required do |value|
    instance_variable_set("@#{name}", value)
  end
end
add_boolean(name) click to toggle source

This method adds an attribute to the DSL for managing a Boolean flag.

This will create a method that you can call to change the value from true to false.

The value will always be false by default.

### Parameters

  • **name: Symbol** The name of the method, which will also be the

    name of the attribute.
# File lib/ruby_yacht/dsl/dsl.rb, line 117
def add_boolean(name)
  add_generic_attribute name, name, false, false do
    instance_variable_set("@#{name}", true)
  end
end
add_generic_attribute(method_name, attribute_name, default = nil, required = true, &processing_block) click to toggle source

This method adds an attribute to the DSL.

This will define a method on the DSL class, and register the attribute with the class.

You generally won’t need to call this yourself, but it provides a shared implementation for the rest of the attribute-definition methods.

### Parameters

  • **method_name: Symbol** The name of the method to create.

  • **attribute_name: Symbol** The name of the instance variable that

    the method sets.
  • **default: Object** The default value that should be set

    for this attribute.
  • **required: Bool** Whether the user should be required to

    provide a value for this attribute.
  • **processing_block: Proc** The block that we should invoke when

    the method is called, to set the value
    of the attribute.
# File lib/ruby_yacht/dsl/dsl.rb, line 59
def add_generic_attribute(method_name, attribute_name, default = nil, required = true, &processing_block)
  define_method(method_name, &processing_block)
  if default != nil
    default_values[attribute_name] = default
  end
  if required
    required_attributes << attribute_name
  end
  all_attributes << attribute_name
end
add_list(name) click to toggle source

This method adds an attribute to the DSL for managing a list.

This will create a method that you can invoke with a single parameter, which will be added to the list.

### Parameters

  • **name: Symbol** The name of the method. The name of the

    attribute will be this name, followed by an s.
# File lib/ruby_yacht/dsl/dsl.rb, line 97
def add_list(name)
  add_generic_attribute name, "#{name}s".to_sym, [], false do |value|
    variable_name = "@#{name}s"
    list = instance_variable_get(variable_name)
    list << value
    instance_variable_set(variable_name, list)
  end
end
add_object(name, type, options = {}) click to toggle source

This method adds an object from another DSL to this DSL.

This will create a method that you can call with a block, which will allow you to use the other DSL object. You can also give it arbitrary other arguments, which will be given to the initializer for the DSL.

### Parameters

  • **name: Symbol** The name of the method, which will also be the

    name of the attribute.
  • **type: Class** The class type that provides the DSL when they

    call this method.
  • **options: Hash** Other options about the DSL. Right now the only

    option is *required*, which indicates whether
    they must define this configuraiton.
# File lib/ruby_yacht/dsl/dsl.rb, line 138
def add_object(name, type, options = {})
  unless options.has_key?(:required)
    options[:required] = true
  end
  add_generic_attribute name, name, nil, options[:required] do |*args, &config_block|
    variable_name = "@#{name}"
    object_config = type.new(*args)
    value = object_config.run(config_block).create_object
    instance_variable_set(variable_name, value)
  end
end
add_object_list(name, type) click to toggle source

This method adds a list of objects from another DSL to this DSL.

This will create a method that you can call with a block to create the other object and add it to the list. You can also give it arbitrary other arguments, which will be given to the initializer for the DSL.

### Parameters

  • **name: Symbol** The name of the method. The attribute name will

    be this, followed by an "s"
  • **type: Class** The class type that provides the DSL when they

    call this method.
# File lib/ruby_yacht/dsl/dsl.rb, line 162
def add_object_list(name, type)
  add_generic_attribute name, "#{name}s".to_sym, [], false do |*args, &config_block|
    variable_name = "@#{name}s"
    list = instance_variable_get(variable_name)
    object_config = type.new(*args)
    object_config.run(config_block)
    value = object_config.create_object
    list << value
    instance_variable_set(variable_name, list)
  end
end
all_attributes() click to toggle source

The attributes that this DSL type can accept.

# File lib/ruby_yacht/dsl/dsl.rb, line 18
def all_attributes
  @all_attributes ||= []
end
copied_attributes() click to toggle source

The attributes that we copy from this type to the real type the DSL creates.

# File lib/ruby_yacht/dsl/dsl.rb, line 29
def copied_attributes
  @copied_attributes
end
created_type() click to toggle source

The real type that this DSL creates.

# File lib/ruby_yacht/dsl/dsl.rb, line 34
def created_type
  @created_type
end
creates_object(type, attributes = nil) click to toggle source

This method specifies that this DSL class creates a real object of a certain type.

### Parameters

  • **type: Class** The type that this DSL creates.

  • **attributes: Array** The attributes that we copy from the DSL

    to the related object. If this is not
    provided, we will copy all of the
    attributes that this DSL has defined so far.
# File lib/ruby_yacht/dsl/dsl.rb, line 184
def creates_object(type, attributes = nil)
  attributes ||= self.all_attributes
  @copied_attributes = attributes
  @created_type = type
end
custom_attribute_method() click to toggle source

This method gets the attribute that server types can use to specify custom attributes for this DSL type.

The default is ‘server_attributes`.

# File lib/ruby_yacht/dsl/dsl.rb, line 194
def custom_attribute_method
  :server_attributes
end
default_values() click to toggle source

The default values that should be set on new DSL objects.

# File lib/ruby_yacht/dsl/dsl.rb, line 23
def default_values
  @default_values ||= {}
end
required_attributes() click to toggle source

The attributes that must be provided on this DSL type.

# File lib/ruby_yacht/dsl/dsl.rb, line 13
def required_attributes
  @required_attributes ||= []
end