25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Method Generators<br />

In addition to these objects, an array variable, %parameter, is provided. This array contains<br />

the values of any class parameters indexed by parameter name. For example,<br />

%parameter("MYPARM"), contains the value of the MYPARM class parameter for the current<br />

class. This variable is provided as an easier alternative to using the list of parameter definitions<br />

available via the %class object.<br />

18.4 Implementing Method Generators<br />

To implement a method generator, do the following:<br />

1. Define a method and set its CodeMode keyword to “objectgenerator” .<br />

2. In the body of the method, write code that will generate the actual method code when<br />

the class is compiled. This code will used the %code object to write out the code. It will<br />

most likely use the other available objects as inputs to decide what code to generate.<br />

The following is an example of a method generator that creates a method that lists the names<br />

of all the properties of the class it belongs to:<br />

ClassMethod ListProperties() [ CodeMode = objectgenerator ]<br />

{<br />

For i = 1:1:%compiledclass.Properties.Count() {<br />

Set prop = %compiledclass.Properties.GetAt(i).Name<br />

Do %code.WriteLine(" Write """ _ prop _ """,!")<br />

}<br />

Do %code.WriteLine(" Quit")<br />

Quit $$$OK<br />

}<br />

This generator will create a method with an implementation similar to:<br />

Write "Name",!<br />

Write "SSN",!<br />

Quit<br />

Note the following about the method generator code:<br />

1. It uses the WriteLine method of the %code object to write lines of code to a stream<br />

containing the actual implementation for the method. (You can also use the Write method<br />

to write text without an end-of-line character).<br />

2. Each line of generated code has a leading space character. This is required as <strong>Caché</strong><br />

ObjectScript does not allow commands within the first space of a line. This would not<br />

be the case if our method generator is creating Basic or Java code.<br />

158 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!