This question already has answers here:
Calling a Method From a String With the Method's Name in Ruby
(4 answers)
Closed 3 years ago.
I have a table of scores that need processing at certain points. I am trying to avoid having walls of repetitive code so I am trying to get the values dynamically just by sending in the metric value but I cannot seem to get the syntax right and wasn't even sure how to properly search for this.
A typical value I need is something like this
s = score.total_weighted_strategic_values_score
All of these have the same structure to the name but one part changes based on the metric, so I have been trying something like this
s = score.total_weighted_"#{metric}"_score
Where metric is equal to a string strategic_values or whatever the metric is named.
This throws a syntax error, however, and I cannot find anything else to try. any help would be greatly appreciated, thanks!
You need to use public_send:
s = score.public_send("total_weighted_#{metric}_score")
Some more reading on it:
http://vaidehijoshi.github.io/blog/2015/05/05/metaprogramming-dynamic-methods-using-public-send/
Related
This question already has answers here:
Design RESTful query API with a long list of query parameters [closed]
(4 answers)
How to send a huge parameter list to a GET request
(3 answers)
Rest POST VS GET if payload is huge
(5 answers)
How to desing RESTful advanced search/filter
(1 answer)
Closed 6 months ago.
I'm trying to design a (simple?) REST API using openapi3.0 and the Swagger tools (and sort of new to this, I should add).
One of the endpoints will retrieve a set of items (from a database of several thousand items) based on one or more 'filters', or set of conditions.
path:
/item/filter:
get:
Here is my first doubt: I have to pass a (potentially large) set of conditions to my query.
For example:
[
{"propertyName":"height","queryType":"EqualOrGreaterThan","targetvalue":"1200"},
{"propertyName":"color","queryType":"Equal","targetvalue":"green"},
{"propertyName":"weigth","queryType":"LessThan","targetvalue":"1.2"}
]
Does using the GET method imply that everything will be encoded into the URL itself? How will OAS/Swagger know that my URLs won't get too long? Or is this some parameter that I can tweak in the codegen part?
Alternatively, I could use the POST method, but wouldn't this be a betrayal of good API design, as this query would not be modifying the status of the system/database?
Also, GET or POST method aside, I will still need to model the complex query. Should I go with defining a 'filter' schema and have my request define an array of items with the 'filter' schema? Or am I missing a better API design strategy for a case like this?
Also, what about the backend? Will the format of the data the server receives be the same regardless of POST/GET and, above all, regardless of the programming language used for the backend? (.NET, Python Flask, Java, PHP,...)?
During tutorials it looks easy, but I'm sort of confused right now.
I'm trying, to wrap my head around Touchosc and script based on LUA 5.1.
I have a number of labels, called song0, song1, song2, and so on. I'm trying to set different values in these, using
local text = 'Smoke On The Water'
for i = 1, 2 do
self.children.pager1.children.main.children.song[i].values.text = text
end
but that gives me an error.
:-) I do need help.
Finn
Since you haven't provided the actual error, it's difficult to say what the problem is, but if I have to venture a guess, then try replacing ...children.song[i].values... with ...children["song"..i].values.... Since there is no table song, you just need to generate dynamic field names.
I'm trying to retrieve rows from a class called Consultations using a column called userPointer. It should be straight forward but I've been struggling with this for couple of days:
Let us for example say I would like to get the first object
where userPointer = ttnRYrdu0J
Note: I will replace ttnRYrdu0J with PFUser.current() later to be dynamic if this works.
I wrote the code like this:
Unfortunately it doesn't work, however when I use any other columns such as objectId or type it works fine.
Any help please!!
Thanks
I've got the answer I used PFUser.current() instead of hard coding it which is what I want at the end because I want to show the consultations of the current user and it worked! I'm still confused why it is not working when I hard code the value but I got what I want.
I found this similar question How to fill in a datetime-local field with capybara?, and the only answer in this thread is not working. So I decided to open this question. It seems like there's no documentation or tutorial about this. Have any solution? It will be a great help!
The keys different browsers accept for setting a datetime input field are diffrent, however if you're using selenium with chrome and you are actually attempting to fill in a visible <input type="datetime-local"> element, as your question states, then the answer in the question your linked to should work - Here is a gist that shows it working - https://gist.github.com/twalpole/a541746b354afde8e82fa89a35a9b2da
The important part in that answer is the format of the string you send since it needs to match the keys the browser is expecting for setting that input (to_json doesn't match that format)
Therefore, in your case of wanting to set DateTime.current it should be something along the lines of
fill_in 'id/name/label of input', with: DateTime.current.strftime("%m%d%Y\t%I%M%P")
If that doesn't work for you then most likely you're not actually attempting to fill a visible <input type="datetime-local"> field (maybe you're using some kind of JS widget that replaces/hides the input???) and you'll need to specify the exact HTML you are trying to fill in your question.
from what I understand, you need to fill in a string representation of the datetime format
examples of such dates are
1990-12-31T23:59:60Z
or
1996-12-19T16:39:57-08:00
so something like
fill_in datetimeinput, with: "1990-12-31T23:59:60Z"
should work (hope so!)
For an API, I want to return the actual keyified string.
So:
User.errors.messages[:name]
#=> activerecord.errors.models.user.attrributes.blank
Instead of
Can't be blank
I know I can override this by creating an actual translation, or by setting custom errors in the validates methods in my Models, but I was wondering if there is a lower level, simpler way to make rails return the "keyified" string instead of parsing it through the translation-layers.
I answered some similar questions on SO, but could not find them right now...
I think that this is not possible right now because the ActiveModel::Errors::add method does not store the Key to the message, but just the derived message.
It's also not trivial to reverse get the key from translation files or the like.
I think it would be a valuable addition to rails to actually store the key of the error-message instead of just the message itself.