Module | Blockenspiel::DSLSetupMethods |
In: |
lib/blockenspiel/dsl_setup.rb
|
These class methods are available after you have included the Blockenspiel::DSL module.
By default, a class that has DSL capability will automatically make all public methods available to parameterless blocks, except for the initialize method, any methods whose names begin with an underscore, and any methods whose names end with an equals sign.
If you want to change this behavior, use the directives defined here to control exactly which methods are available to parameterless blocks.
A DSL-friendly attr_accessor.
This creates the usual "name" and "name=" methods in the current class that can be used in the usual way. However, its implementation of the "name" method (the getter) also takes an optional parameter that causes it to behave as a setter. This is done because the usual setter syntax cannot be used in a parameterless block, since it is syntactically indistinguishable from a local variable assignment. The "name" method is exposed as a dsl_method.
For example:
dsl_attr_accessor :foo
enables the following:
my_block do |param| param.foo = 1 # Usual setter syntax works param.foo 2 # Alternate setter syntax also works puts param.foo # Usual getter syntax still works end my_block do # foo = 1 # Usual setter syntax does NOT work since it # looks like a local variable assignment foo 2 # Alternate setter syntax does work puts foo # Usual getter syntax still works end
A DSL-friendly attr_writer.
This creates the usual "name=" method in the current class that can be used in the usual way. However, it also creates the method "name", which also functions as a setter (but not a getter). This is done because the usual setter syntax cannot be used in a parameterless block, since it is syntactically indistinguishable from a local variable assignment. The "name" method is exposed as a dsl_method.
For example:
dsl_attr_writer :foo
is functionally equivalent to:
attr_writer :foo alias_method :foo, :foo= dsl_method :foo
which enables the following:
my_block do |param| param.foo = 1 # Usual setter syntax works param.foo 2 # Alternate setter syntax also works end my_block do # foo = 1 # Usual setter syntax does NOT work since it # looks like a local variable assignment foo(2) # Alternate setter syntax does work end
Make a particular method available to parameterless DSL blocks.
To explicitly make a method available to parameterless blocks:
dsl_method :my_method
To explicitly exclude a method from parameterless blocks:
dsl_method :my_method, false
To explicitly make a method available to parameterless blocks, but point it to a method of a different name on the target class:
dsl_method :my_method, :target_class_method
Control the behavior of methods with respect to parameterless blocks, or make a list of methods available to parameterless blocks in bulk.
To enable automatic exporting of methods to parameterless blocks. After executing this command, all public methods defined in the class will be available on parameterless blocks, until dsl_methods false is called:
dsl_methods true
To disable automatic exporting of methods to parameterless blocks. After executing this command, methods defined in this class will be excluded from parameterless blocks, until dsl_methods true is called:
dsl_methods false
To make a list of methods available to parameterless blocks in bulk:
dsl_methods :my_method1, :my_method2, ...
You can also point dsl methods to a method of a different name on the target class, by using a hash syntax, as follows:
dsl_methods :my_method1 => :target_class_method1, :my_method2 => :target_class_method2
You can mix non-renamed and renamed method declarations as long as the renamed (hash) methods are at the end. e.g.:
dsl_methods :my_method1, :my_method2 => :target_class_method2