more than one defaction in the body? - krl

All, can I run more than one defaction in the body of the rule? or can I only run one?

You can define as many actions in the pre block of a rule as you want. You can have as many actions in the action block of a rule as you want (just enclose the action block in curly braces). For example,
rule first_rule {
select when pageview ".*" setting ()
pre {
notify_one = defaction() { notify("notify_one", "First defaction"); };
notify_two = defaction() { notify("notify_two", "Second defaction"); };
}
{
notify_one();
notify_two();
}
}
So I think the answer to your question is yes.

Your question is a little confusing, but I'll give it a run.
Running actions defined with defaction is just like running system defined actions.
If you want to run more then one action in a rule, you need to wrap them in {} like so:
rule foo {
select when pageview ".*"
{
notify("cheese", "brie");
notify("apple", "golden delicious");
}
}

I seem to recall that a defaction has an implicit, optional 'pre' section, followed by the action(s). To include multiple actions you do need {} as Sam says.
act1 = defaction() {
{
notify("Defaction Demo", "<ul id='demo_id'></ul>");
append("#demo-id", "<li>cheese: brie</li>");
append("#demo-id", "<li>apple: golden delicious</li>");
}
};
That works out to defaction() { { ... } }; but the extra curly braces are required if you want more than one action in a defaction.
See also http://docs.kynetx.com/docs/Defaction

Related

How to optimize multiple if statements in Powershell?

This is my code :
Function CleanUpOracle
{
if ($Requete)
{
$Requete.Dispose()
}
if ($ExecuteRequete)
{
$ExecuteRequete.Dispose()
}
if ($Transaction)
{
$Transaction.Dispose()
}
if ($OracleConnexion)
{
$OracleConnexion.close()
$OracleConnexion.Dispose()
}
if ($Log.id)
{
$Log.PSObject.Properties.Remove('id')
}
}
I'm testing if a variable exist then {do something}
But in the future I'll many variable to test, I don't want to have hundred lines with that.
How can I optimize this? Maybe with a switch but how?
Thanks !
If you want to conditionally call Dispose against multiple items you can stream them from a list into the ForEach-Object (whose alias is %):
#($Requete, $ExecuteRequete, $Transaction, $OracleConnexion) |
% {if($_) {$_.Dispose()} }
NOTE: I've split this onto multiple lines for readability.

Free Shipping Cart rules aren't supported by socolissimoflexibilite (french carrier)

I'm trying to understand why the free shipping cartrule I just created doesn't work on all the available shipping methods.
I have one shipping method that becomes free, and the other (socolissimoflexibilite) doesn't change.
Could someone give me a hint, explanation, or some config/data/code to check on socolissimoflexibilite shipping method?
My first investigations lead me to the sales/quote_address_rate collection: one rate is well changed to 0.00€, but not the other.
I also checked the quote shipping address: its free_shipping field is set to 1.
Ok I found the answer :
We had a module (socolissimoflexibilite) extending the module socolissimosimplicite.
socolissimoflexibilite rewrites the collectRates() function, and freeShipping isn't supported anymore. I had to rewrite this part by myself.
For people getting the same issues with this module, here is the trick :
In Addonline_SoColissimoFlexibilite_Model_Carrier_ShippingMethod class, the collectRates() function must be replaced with this code :
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
$rates = parent::collectRates($request);
$shippingAddress = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
if ($shippingAddress && $shippingAddress->getData('soco_product_code') == 'RDV') {
foreach ($rates->getAllRates() as $rate) {
if ($rate->getCarrier()===$this->_code) {
$rate->setPrice($rate->getPrice()+(int)Mage::getStoreConfig('carriers/socolissimoflexibilite/rdv_fees'));
}
}
}
if($shippingAddress->getFreeShipping()) {
foreach ($rates->getAllRates() as $rate) {
if($rate->getCarrier() === $this->_code) {
$rate->setPrice(0) ;
$rate->setCost(0) ;
}
}
}
return $rates;
}
And now, freeShipping rules will be supported by socolissimoflexibilite !

How to tell eclipse to not format parts of a codefile (pressing Strg + Shift + F)

I really love the autofromat feature. I makes your code more readable and in case of JavaScript tells you, when there are synatcs errors (missing brackets etc.).
However sometimes the formatting makes the code harder to read. e.g. when it puts a long array inizalisation into a single line. In that case I don't want him to format it, but rather leave it ofer multiple lines.
E.g.
define([
'jquery',
'aloha',
'aloha/plugin',
'ui/ui',
'ui/scopes',
'ui/button',
'ui/toggleButton',
'ui/port-helper-attribute-field',
'ui/text'
// 'css!youtube/css/youtube.css'
],
function(
$,
Aloha,
Plugin,
Ui,
Scopes,
Button,
ToggleButton,
AttributeField)
{
this array should stay like this and don't become this:
define(['jquery', 'aloha', 'aloha/plugin', 'ui/ui', 'ui/scopes', 'ui/button', 'ui/toggleButton', 'ui/port-helper-attribute-field', 'ui/text' ], function($, Aloha, Plugin, Ui, Scopes, Button, ToggleButton, AttributeField) {
Is there a special tag, to tell eclipse not to format the code?
OK, it took me some time to find the right setting so I will post a toturial here.
Go to Window Preferences and Search the Formatter you are using. In my case it was under 'Aptana Studia' -> 'Formatter'. (Depending on your Package this differs, e.g. the Java Formatter is under 'Java' -> 'Code Style' -> 'Formater').
Noww create a new Build profile since you can't override the old one.
Now enable the Formatter tags.
Now you can use the
- #formatter:on
- #formatter:off
tags to disable code formatting.
Example:
this code:
function hello() { return 'hello';
}
//#formatter:off
/*
|\ _,,,---,,_
/,`.-'`' -. ;-;;,_
|,4- ) )-,_..;\ ( `'-'
'---''(_/--' `-'\_) fL
*/
//#formatter:on
function
world() {
return 'world';
}
Will get formatted to like this
function hello() {
return 'hello';
}
//#formatter:off
/*
|\ _,,,---,,_
/,`.-'`' -. ;-;;,_
|,4- ) )-,_..;\ ( `'-'
'---''(_/--' `-'\_) fL
*/
//#formatter:on
function world() {
return 'world';
}
Note how the function definition is formatted correct, while the ascii art isn't
Credits:
Katja Christiansen for his comment
https://stackoverflow.com/a/3353765/639035 : for a similar answer
Try to make an empty comment after each line:
define([ //
'jquery', //
'aloha', //
'aloha/plugin', //
'ui/ui', //
'ui/scopes', //
'ui/button', //
'ui/toggleButton', //
...
Not nice, but I think it will work.

Assigning actions to a variable

In answering Aaron's recent question, I'd like to do something like the following:
rule first_rule {
select when pageview "exampley.com/\?name=(.*)" setting (username)
pre {
isjoe = username eq "joe";
myaction = defaction() {
thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();
thisaction();
};
}
{
notify("Will it work?", "Methinks you are #{username}");
myaction();
}
}
However, the defaction never seems to work. It doesn't like that I'm trying to assign an action to a variable and then return that variable.
What am I doing wrong?
You are really close.
You can't call an action till the end of the defaction. You need to create a defaction that delays execution till the right time.
Change:
thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();
to
thisaction = isjoe => defaction(){notify("Hello, World", "Hi there, Joe!");} | noop;
Note the added defaction and I removed the parens from noop.
This concept is is similar to javascript closures.

KRL and Yahoo Local Search

I'm trying to use Yahoo Local Search in a Kynetx Application.
ruleset avogadro {
meta {
name "yahoo-local-ruleset"
description "use results from Yahoo local search"
author "randall bohn"
key yahoo_local "get-your-own-key"
}
dispatch { domain "example.com"}
global {
datasource local:XML <- "http://local.yahooapis.com/LocalSearchService/V3/localsearch";
}
rule add_list {
select when pageview ".*" setting ()
pre {
ds = datasource:local("?appid=#{keys:yahoo_local()}&query=pizza&zip=#{zip}&results=5");
rs = ds.pick("$..Result");
}
append("body","<ul id='my_list'></ul>");
always {
set ent:pizza rs;
}
}
rule add_results {
select when pageview ".*" setting ()
foreach ent:pizza setting pizza
pre {
title = pizza.pick("$..Title");
}
append("#my_list", "<li>#{title}</li>");
}
}
The list I wind up with is
. [object Object]
and 'title' has
{'$t' => 'Pizza Shop 1'}
I can't figure out how to get just the title. It looks like the 'text content' from the original XML file turns into {'$t' => 'text content'} and the '$t' give problems to pick().
When XML datasources and datasets get converted into JSON, the text value within an XML node gets assigned to $t. You can pick the text of the title by changing your pick statement in the pre block to
title = pizza.pick("$..Title.$t");
Try that and see if that solves your problem.
Side notes on things not related to your question to consider:
1) Thank you for sharing the entire ruleset, what problem you were seeing and what you expected. Made answering your question much easier.
2) The ruleset identifier should not be changed from what AppBuilder or the command-line gem generate for you. Your identifier that is currently
ruleset avogadro {
should look something more like
ruleset a60x304 {
3) You don't need the
setting ()
in the select statement unless you have a capture group in your regular expression
Turns out that pick("$..Title.$t") does work. It looks funny but it works. Less funny than a clown hat I guess.
name = pizza.pick("$..Title.$t");
city = pizza.pick("$..City.$t");
phone = pizza.pick("$..Phone.$t");
list_item = "<li>#{name}/#{city} #{phone}</li>"
Wish I had some pizza right now!