Module: RubyYacht::DSL::Base::ClassMethods

Overview

This module provides class methods for defining DSLs.

Instance Method Summary collapse

Instance Method Details

#add_attribute(name, default = nil, required = true) ⇒ Object

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.


82
83
84
85
86
# 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) ⇒ Object

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.


117
118
119
120
121
# 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) ⇒ Object

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.


59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

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.


97
98
99
100
101
102
103
104
# 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 = {}) ⇒ Object

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.


138
139
140
141
142
143
144
145
146
147
148
# 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) ⇒ Object

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.


162
163
164
165
166
167
168
169
170
171
172
# 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_attributesObject

The attributes that this DSL type can accept.



18
19
20
# File 'lib/ruby_yacht/dsl/dsl.rb', line 18

def all_attributes
  @all_attributes ||= []
end

#copied_attributesObject

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



29
30
31
# File 'lib/ruby_yacht/dsl/dsl.rb', line 29

def copied_attributes
  @copied_attributes
end

#created_typeObject

The real type that this DSL creates.



34
35
36
# File 'lib/ruby_yacht/dsl/dsl.rb', line 34

def created_type
  @created_type
end

#creates_object(type, attributes = nil) ⇒ Object

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.


184
185
186
187
188
# 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_methodObject

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

The default is server_attributes.



194
195
196
# File 'lib/ruby_yacht/dsl/dsl.rb', line 194

def custom_attribute_method
  :server_attributes
end

#default_valuesObject

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



23
24
25
# File 'lib/ruby_yacht/dsl/dsl.rb', line 23

def default_values
  @default_values ||= {}
end

#required_attributesObject

The attributes that must be provided on this DSL type.



13
14
15
# File 'lib/ruby_yacht/dsl/dsl.rb', line 13

def required_attributes
  @required_attributes ||= []
end