I understand that this may be a duplicate of Any equivalent of Ruby's public_send method?. I'd like to explain what I am doing, and perhaps someone could advice.
I've been porting a ruby app over the last few days to learn Crystal. I've had to cut out a lot of functionality due to lack of send but today I've hit a main artery in my program.
I have a Hash which contains keystroke as key, and method as value. Based on what key is struck, the appropriate method is called. This obviously uses the send to implement the same.
From the linked question, I understand that Crystal is compiled so dynamic method calls are not permitted. However, if you look at the Vim editor, a user can map keys to methods, too. And vi(m) is written in C.
I am wondering if I missed something.
I know I could probably hardcode a switch statement with each key and call the appropriate method, but that still does not allow the user to assign a key to a method. Is there any alternative to this very large switch-case method ?
(I am guessing that rather than check the key in the when part, I would check the binding and call the method.
binding = #bindings[key]
case binding
when :up
up
when :down
down
when .....
else
end
Any better solution ?
I'm not sure that this way most simple and convenient (perhaps more experienced developers will correct me below) but i would use the Proc:
def method1
puts "i'm method1"
end
def method2
puts "i'm method2"
end
def method3
puts "i'm method3"
end
hash = {
"ctrl": -> { method1 },
"shift": -> { method2 },
"alt": -> { method3 }
}
binding = ["ctrl", "shift", "alt"].sample
hash[binding].call #=> i'm method2
See working example
Related
im quite familar with Mocktio. But never needed something like this before:
Within my Test i like to verify that a method gets called with several values that represent a kind of "loading started". lets say "loading started" and "loading finished with result T". The order of those state change matters!
So, in pseudo code it would do something like this:
resultObject ...
clientcode.requestDataload()
// Order of below state changes is imporant
verifyThat(resultObject.data).wasCalledWith("loading started")
verifyThat(resultObject.data).wasCalledWith("loading finished with result T")
Thanks for pointing me into some direction.
You can use the verifiy method from Mockito:
verify(mockInstance, times(1)).method(ArgumentMatchers.eq("what you want"))
where method is the method that the call need to be checked, and the ArgumentMatchers are the arguments passed to the method for the call (you can use any() too).
You can use InOrder:
val inOrder = inOrder(yourMock)
inOrder.verify(yourMock).yourMethod("loading started")
inOrder.verify(yourMock).yourMethod("loading finished with result T")
As the title say, I have some problem understanding what does this call return.
This is how I am using it:
fetchEngines()
{
let object = Ember.getOwner(this).lookup('application:main').engines;
console.log(object);
}
And it return me something like that:
At this point, this is what I want, the list of all my ember-engines.
But I don't know how to use it. By that I mean, how do I fetch the name of each engine, what is object at this point, I can't find anything about it.
I have tried the forEach() method, but it returns me : object.forEach is not a function. I have also tried the Object.keys method, but it returned me undefined, maybe somebody can indicate me a doc or something, I don't understand at all what is it.
Good day to you and thank you for reading.
I will answer this. This is very simple, and I made a mistake. The Object.keys method work, I didn't know how to write it well.
This is the corrected version:
fetchEngines()
{
let object = Ember.getOwner(this).lookup('application:main').engines;
// This will properly show every key in your object
console.log(Object.keys(object));
// And if you want to enumerate it
let filledArray = [];
for (let key in object) {
if (object.hasOwnProperty(key))
filledArray.push(key);
}
// The object filledArray is now a perfectly manipulable object
}
We have a business need to collect specific bindings in every JSF page. and we do that inside overridden ViewHandlerWrapper class
I use the below code inside renderView method to get the whole expression value property for every RichInputText and it's work fine for me
ValueExpression valExp = Inputcomponent.getValueExpression("value");
String ExpressionString = valExp.getExpressionString();
output was: #{binding.EmployeeId.inputValue}
When I do the same against RichButtin I got null value as following:
ValueExpression valExp = Btncomponent.getValueExpression("actionlistener");
String ExpressionString = valExp.getExpressionString();
What is the wrong in my last peace of code?
Obtaining a ValueExpression form a RichInputText works because, as the name suggests, it evaluates to a value, which may or may not be an EL expression, let alone a method.
On the other hand, a RichButton does not really have to evaluate to something; rather, it aims to invoke behavior (i.e. a method), from which you would want a MethodExpression - though in this case, the closest we get to it is a MethodBinding.
Luckily, UIXCommand, a superclass of RichButton, provides two methods from which you can obtain your action listeners:
public final MethodBinding getActionListener()
From the MethodBinding returned, you can invoke getExpressionString() so you can get what you wanted - such as some actionListener EL string like #{binding.bean.actionListenerMethod}.
public final ActionListener[] getActionListeners()
Might be worth mentioning, though there is not much merit for this in your use case. It simply returns the listeners on which you can manually process the events.
I've written my own access layer to a game engine. There is a GameLoop which gets called every frame which lets me process my own code. I'm able to do specific things and to check if these things happened. In a very basic way it could look like this:
void cycle()
{
//set a specific value
Engine::setText("Hello World");
//read the value
std::string text = Engine::getText();
}
I want to test if my Engine-layer is working by writing automated tests. I have some experience in using the Boost Unittest Framework for simple comparison tests like this.
The problem is, that some things I want the engine to do are just processed after the call to cycle(). So calling Engine::getText() directly after Engine::setText(...) would return an empty string. If I would wait until the next call of cycle() the right value would be returned.
I now am wondering how I should write my tests if it is not possible to process them in the same cycle. Are there any best practices? Is it possible to use the "traditional testing" approach given by Boost Unittest Framework in such an environment? Are there perhaps other frameworks aimed at such a specialised case?
I'm using C++ for everything here, but I could imagine that there are answers unrelated to the programming language.
UPDATE:
It is not possible to access the Engine outside of cycle()
In your example above, std::string text = Engine::getText(); is the code you want to remember from one cycle but execute in the next. You can save it for later execution. For example - using C++11 you could use a lambda to wrap the test into a simple function specified inline.
There are two options with you:
If the library that you have can be used synchronously or using c++11 futures like facility (which can indicate the readyness of the result) then in your test case you can do something as below
void testcycle()
{
//set a specific value
Engine::setText("Hello World");
while (!Engine::isResultReady());
//read the value
assert(Engine::getText() == "WHATEVERVALUEYOUEXPECT");
}
If you dont have the above the best you can do have a timeout (this is not a good option though because you may have spurious failures):
void testcycle()
{
//set a specific value
Engine::setText("Hello World");
while (Engine::getText() != "WHATEVERVALUEYOUEXPECT") {
wait(1 millisec);
if (total_wait_time > 1 sec) // you can put whatever max time
assert(0);
}
}
I would like to know whether it's possible to have something like this next line but with the exclusion of some actions. Because I would like to have manage, add, delete,... to go to the respective action and not to the display action. I know it's possible by specifying these rules explicitely upfront, but if you have quite some of these it will not look to good in the router file.
Router::connect('/paginas/manage', array('controller' => 'paginas', 'action' => 'manage'));
...
Router::connect('/paginas/*', array('controller' => 'paginas', 'action' => 'display'));
So the aim is to remove the first line...
Thanks.
Do something like this in your PaginasController which is basicly the PagesController I think.:
....
public function display(){
// Assuming default behavior of cakephp here
...
if (!empty($path[0])) {
$page = $path[0];
if(
method_exists($this, $page) &&
!in_array(
$page,
array(
// Methods that never should be executed in this controller are going in here
)
)
){
$this->{$page}();
}
}
}
This would execute a specific method in the controller. You could exit it there to make it stop working afterwards.
This might be a bit dangerous though because it can access parent methods of AppController, too.
The other way in which you also need exactly two of those Router::connect() rules one described here: http://api.cakephp.org/class/router#method-Routerconnect
Router::connect(
'paginas/:action/*',
array(),
array('paginas' => '(manage|add|delete)')
);
This one goes first followed by the other one. Never tested!
Though I don't see why there should be public methods for editing pages. Use these to seperate them from the rest: http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
I recommend, if you are trying to do this what I am thinking of, that you won't start to write a management for pages using the PagesController. "Pages" is a more or less reserved word in cake and you can get in big trouble with using those words (wrote a "File" plugin once including a "File" model. Waste of time as if since cake1.2 or 1.3 there is actually a "File" class to handle file operations). Create something new like "ContentPage" or whatever for it. You are on a saver side then. This paragraph is maybe useless, because your controller is not even named "Pages", but I hate to delete long ones, so it stays, just in case you need this information once.
Also i recommend not to change programming language, which means, you either use english or spanish or whatever but not both or worst more. You could name your route whatever you want to, but the class names should maybe stay in english because cake is also.
Greetings
func0der