{"id":8350,"date":"2012-09-24T23:40:23","date_gmt":"2012-09-24T18:10:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8350"},"modified":"2012-09-25T02:18:43","modified_gmt":"2012-09-24T20:48:43","slug":"gdsl-awesomeness-understanding-context-and-contributors-in-detail","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-understanding-context-and-contributors-in-detail\/","title":{"rendered":"GDSL Awesomeness &#8211; Understanding Context And Contributors in Detail"},"content":{"rendered":"<p>GDSL Scripts are based on two main concepts: <strong>contexts and contributors.<\/strong> Hence we will discuss about these in detail here.<\/p>\n<p>&nbsp;<\/p>\n<p>In GroovyDSL <strong>context<\/strong> is an entity which is supposed to give an answer to the question &#8220;Where?&#8221; this behavior will be available. Contexts are first-order citizens in GroovyDSL, so they may be stored in local script variables and passed as parameters.<\/p>\n<p>&nbsp;<\/p>\n<p>Context can be defined as:<\/p>\n<p>[java]<br \/>\ndef ctx = context(ctype: ,<br \/>\n                  filetypes: [*],<br \/>\n                  scope: )<br \/>\n[\/java]<\/p>\n<p><strong>Context<\/strong> is a predefined method in the GDSL Scripts.<\/p>\n<p>&nbsp;<\/p>\n<p>When no argument is passed in context it means <strong><em>&#8220;everywhere&#8221;<\/em><\/strong>.<br \/>\nBelow we describe possible values of optional parameters:<\/p>\n<ul>\n<li><strong>Reference class type <em>&#8220;ctype&#8221;<\/em><\/strong>: A string, representing a fully qualified name of a class type to be augmented with the new behavior. In case this argument is missing then <em>java.lang.Object<\/em> class type is taken as a type to be augmented.<\/li>\n<li><strong>Supported file types <em>&#8220;filetypes&#8221;*<\/em><\/strong> A possible empty list of comma-separated file extensions passed as strings or GString instances , this will restrict the behavior to the files with the passed extensions.<\/li>\n<li><strong>Script type <em>&#8220;scriptType&#8221;<\/em><\/strong>:  A string denoting the script type ID, e.g. &#8216;gant&#8217;, &#8216;gradle&#8217;, &#8216;default&#8217;. Differs from <em>&#8220;fileTypes&#8221;<\/em> in that it doesn&#8217;t depend on the extension. For example, the Gant scripts in Grails usually have groovy extension.<\/li>\n<li><strong>Path pattern <em>&#8220;pathRegexp&#8221;<\/em><\/strong>:  A regexp that should match the path to the Groovy file where your script contributes to. The path always uses forward \/ slashes.<\/li>\n<li><strong>Context scope <em>&#8220;scope&#8221;<\/em><\/strong>:  A scope of a current context. If no scope passed to a context, it is assumed to capture all possible places, where other conditions are applicable. It may be of three different kinds:\n<ol>\n<li><strong>scriptScope(name : )<\/strong> The scope of a behavior defined in this context is available in a script body as well as in its inner closures, for instance, ones which are passed as method arguments. scriptScope() method takes one optional parameter  which should be a string, GString or a regular expression. In a presence of this argument appropriate script names will be matched against it. By default the value of this parameter is an all-matching regular expression.<\/li>\n<li><strong>classScope(name : )<\/strong> Restricts the scope of an actual behavior to the class with some particular properties, such as  (rules for class naming filter are same as for ScriptScope).<\/li>\n<\/li>\n<li><strong>closureScope(isArg: Boolean)<\/strong> This scope describes Groovy&#8217;s closure blocks. There is an important case of closure, passed as a parameter to a method, replacing its delegate, which allows to invoke callee&#8217;s unqualified methods in a body of closure. The typical example of such a behavior is identity() method of groovy.lang.GroovyObject class. This case may be captured by passing an optional named parameter isArg to the closure.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1>Contributors<\/h1>\n<p>Contributors are entities which consume contexts as initialization parameters and answer to the question &#8220;What?&#8221;. <\/p>\n<p>In contributors we define the properties or methods to inject.<\/p>\n<p>&nbsp;<\/p>\n<p> New contributor may be defined by an invocation of a method contributor(), taking two parameters:<\/p>\n<ol>\n<li>A list of contexts to be used to contribute a new behavior<\/li>\n<li>Zero-argument closure, adding new behavior to the reference expression &#8220;in the focus&#8221; of a context<\/ol>\n<\/li>\n<p>&nbsp;<\/p>\n<p>The code fragment below shows the definition of a contributor, taking a list of three contexts, defined earlier, namely ctx1 and ctx2 and one closure, which adds unconditionally the method foo with a parameter of type java.lang.String and return type int to the target expression of a context.<br \/>\n[java]<br \/>\ncontributor [ctx1, ctx2], {<br \/>\n    method name: &quot;foo&quot;, params: [bar: &quot;java.lang.String&quot;], type: &quot;int&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<h1>Read Further in the &#8220;GDSL AWESOMENESS&#8221; Series<\/h1>\n<ol>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-introduction-to-gdsl-in-intellij-idea\">GDSL Awesomeness &#8211; Introduction to GDSL in IntelliJ Idea<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-understanding-context-and-contributors-in-detail\">GDSL Awesomeness &#8211; Understanding Context And Contributors in Detail<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-defining-dynamic-properties-in-a-class\">GDSL Awesomeness &#8211; Defining dynamic property in a class<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-defining-dynamic-method-in-a-class\">GDSL Awesomeness &#8211; Defining dynamic method in a class<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-adding-hinting-for-missingmethod-implementation\">GDSL Awesomeness &#8211; Adding Hinting for missingMethod implementation<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-setting-a-global-context\">GDSL Awesomeness &#8211; Setting a global context<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-delegating-closure-calls\">GDSL Awesomeness &#8211; Delegating Closure Calls<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-defining-methods-in-a-closure-passed-to-method-of-particular-name\">GDSL Awesomeness &#8211; Defining methods in a closure passed to method of particular name<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-defining-properties-in-a-closure-passed-to-method-of-particular-name\">GDSL Awesomeness &#8211; Defining Properties in a closure passed to method of particular name<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-contributing-to-classes-which-match-a-pathname-pattern\">GDSL Awesomeness &#8211; Contributing to classes which match a path\/name pattern<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-contributing-methods-with-current-class-return-types-or-parameters\">GDSL Awesomeness &#8211; contributing methods with current class return types or parameters<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-autocomplete-script-for-grails-mail-plugin-sendmail-closure\">GDSL Awesomeness &#8211; AutoComplete Script for grails-Mail-Plugin &#8220;sendMail&#8221; closure<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-getting-code-of-a-method-or-field-in-gdsl-script\">GDSL Awesomeness &#8211; Getting Code of a method or Field in GDSL Script<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/gdsl-awesomeness-advanced-example-showing-domain-properties-implementation\">GDSL Awesomeness &#8211; Advanced Example showing domain properties implementation<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Hope It Helped.<br \/>\n<b>Kushal Likhi<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GDSL Scripts are based on two main concepts: contexts and contributors. Hence we will discuss about these in detail here. &nbsp; In GroovyDSL context is an entity which is supposed to give an answer to the question &#8220;Where?&#8221; this behavior will be available. Contexts are first-order citizens in GroovyDSL, so they may be stored in [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":160},"categories":[7],"tags":[1057,9,692,691,1059,1060,1058],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8350"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8350"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8350\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}