-
Notifications
You must be signed in to change notification settings - Fork 67
Adding possibility to return context keys for dynamically populate context. #13
Conversation
Adding possibility to return context keys for dynamically populate context.
Adding possibility to return context keys for dynamically populate context.
fixed
Hello :-), I don't understand your usecase. Can you explain? |
Hello again, Sure I can explain.
foreach($contextKeys as $item ) {
$context[$item] = $myArray[$item];
or
$method = 'get'.$item;
$context[$item] = $this->$method();
}
Sounds legit? :D |
Why not writing: foreach($myArray as $item => $value)
$context[$item] = $value; which does the same thing :-/ ? I don't know why do it is needed to collect variables in a rule. |
Because myArray will have much more info that is actually needed and because option 2 suites better my needs because I have to take info from many places |
foreach($myArray as $item => $value)
$context[$item] = $value; does the same thing as foreach($contextKeys as $item ) {
$context[$item] = $myArray[$item];
} but is different from foreach($contextKeys as $item ) {
$method = 'get'.$item;
$context[$item] = $this->$method();
} In some cases, calling In some other cases, we might be unable to know exhaustively all the keys that can be used. |
Ok so the goal here is to get a list of variables in a rule, right? |
Exactly, you got it :) |
Hi, why do not use Your context could be <?php
$context = array(
'group' => function() use ($self) {
return $self->getGroup();
},
'points' => function() use ($self) {
return $self->getPoints();
}
); |
Hi @stephpy, I don't think you understand exactly what my needs are.
... and all this without knowing how my rule will look. All this is because if my rule changes i can provide additional context without adding new code as i will have getters for all my context variables. |
@stephpy: Yes, @ncosmin2001 would like to list all the variables declared in a rule. It would be useful to you for a graphical interface for example. |
I understood, it could be useful to have this information. But it doesn't change what i said about population of context. Even if you have But you can create a dynamic context too, sure. :) |
Yes you are right about that, it was just an example. But i thought you didn't understand my problem :) |
@stephpy: +1 for closures but this does not solve the current issue (but thanks for the reminder!). @ncosmin2001: Your patches collect the list of variables in the interpreter, but a rule can serialized and saved in, let's say, a database. Actually, the serialized rule is representing by a model. If we collect variables during the interpretation, we loose this information when we manipulate the model, or, to fix this, we should save the model along with the list of declared variables, which seems complicated. Here is my proposal: what about implementing this feature in the model directly? There is two solutions:
Thoughts? |
Hi, I am now revisiting my fix and a new thought came to me: It would be also nice if we had a list of new added operators if is the case For example for the rule : also I don't have a solution yet for any of the problems mentioned above but i'm working on it :) |
@stephpy Could you take a look at the bug mentionned by @ncosmin2001 with |
what about having protected $_contextVariables = array();
public static function addContextVariable($variable) {
array_push($this->_contextVariables, $variable )
} in Ruler class and public function variable ( $id ) {
\Ruler\Ruler::addContextVariable($id);
return new Bag\Context($id);
} in Model class |
@ncosmin2001 Nop because a created variable is not necessarily added on the model. See my comment #13 (comment). |
Then I think the 1st option from your comment is more appropriate. |
@ncosmin2001 You meant, the 2nd? :-p |
No No :) i don't wanna visit the model :) 1st one seems ok :) ca we have it? :p |
The first proposal does not work. This is what you did in #13 (comment). |
Visiting the model twice is a little bit redundant in my opinion ... |
@ncosmin2001: It is a low CPU-cost operation and this is the cleaner way to do :-). |
ping? |
ping^2 ? |
last ping (after that, we will do this by our own) |
last last ping (we will do this by our own else :-)). |
In my case, I want to be able to populate my context dynamically so i've added this possibility.
The code will look something like this: