Written by

Question wx fg · Jul 11, 2017

How to create method by script?

hi

   I want to create one class and add one method in it, and then invoke it from terminal. I find this

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY…

But there is only examples for adding property.

Anyone can help me? Thanks

Comments

Herman Slagman · Jul 11, 2017
Do ##class(%Dictionary.ClassDefinition).%DeleteId("Test.Class")Set ClassDef=##class(%Dictionary.ClassDefinition).%New()Set ClassDef.Name="Test.Class"Set ClassDef.Super="%Persistent"Set ClassDef.ProcedureBlock=1Set MethDef=##class(%Dictionary.MethodDefinition).%New()Set MethDef.Name="Name"Set MethDef.ReturnType="%String"; Uncomment for ClassMethod; Set MethDef.ClassMethod=1Set MethDef.FormalSpec="First:%Boolean,Second:%String=""Default"""Do MethDef.Implementation.WriteLine($Char(9)_"If First Return 1")Do MethDef.Implementation.WriteLine($Char(9)_"Return Second")Set MethDef.parent=ClassDefDo ClassDef.Methods.Insert(MethDef)Do ClassDef.%Save()
0
wx fg  Jul 12, 2017 to Herman Slagman

Thanks

0
Eduard Lebedyuk  Jul 11, 2017 to Vitaliy Serdtsev

Please note that $$$TAB macro is actually a whitespace:

ClassMethod GetTab() [ CodeMode = expression ]
{
$$$TAB
}

Call to GetTab returns:

USER>zzdump ##class(Utils.Persistent).GetTab()
0000: 20

I usually use either $c(9) or user-defined macro for tabulation.

0
Vitaliy Serdtsev · Jul 11, 2017
#include %systemInclude
#include %occErrors
#include %occCPTJSgen
CreateClass() public {
  
  #dim ex As %Exception.AbstractException
  try {
    $$$AddAllRoleTemporaryInTry
    new $namespace
    
    s className="demo.test"
    
    d:##class(%Dictionary.ClassDefinition).%ExistsId(className##class(%Dictionary.ClassDefinition).%DeleteId(className)
    
    newClass=##class(%Dictionary.ClassDefinition).%New()
    newClass.Name=className
    newClass.ProcedureBlock=$$$YES
    s newClass.Super="%RegisteredObject"

    newProp=##class(%Dictionary.PropertyDefinition).%New()
    newProp.parent=newClass
    newProp.Name="p1"
    newProp.Type="%String"
    
    newMethod=##class(%Dictionary.MethodDefinition).%New()
    newMethod.parent=newClass
    newMethod.Name="Main"
    newMethod.ReturnType="%Status"
    newMethod.FormalSpec=$$$FormatText("a:%String=%1",$$$quote("Hello, World!"))
    newMethod.Implementation.WriteLine($$$TAB_"w a,!")
    newMethod.Implementation.WriteLine($$$TAB_"q $$$OK")
    
    $$$ThrowOnError(newClass.%Save())

    d $system.OBJ.Compile(className,"cu/multicompile=1")
     
    
  }catch(ex{
    "Error "ex.DisplayString(),!
  }
}
Result:
Class demo.test Extends %RegisteredObject ProcedureBlock ]

{

</FONT><FONT COLOR="#000080">Property </FONT><FONT COLOR="#000000">p1 </FONT><FONT COLOR="#000080">As %String</FONT><FONT COLOR="#000000">;

</FONT><FONT COLOR="#000080">Method </FONT><FONT COLOR="#000000">Main(</FONT><FONT COLOR="#ff00ff">a </FONT><FONT COLOR="#000080">As %String </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#800080">"Hello, World!"</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Status </FONT><FONT COLOR="#000000">{  </FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#800000">a</FONT><FONT COLOR="#000000">,!  </FONT><FONT COLOR="#0000ff">q $$$OK </FONT><FONT COLOR="#000000">}

}</FONT>

0
Vitaliy Serdtsev  Jul 11, 2017 to Eduard Lebedyuk

Thanks, fixed the code. Now <FONT COLOR="#0000ff">$$$TAB </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(9)</FONT>

0
Eduard Lebedyuk  Jul 11, 2017 to Vitaliy Serdtsev

I didn't know that

#include %occCPTJSgen

Redefines $$$TAB. Seems useful. Thanks.

0