Velocity - get the current class name? - velocity

If the name of the class is ClassName1000, how do I get the name of the class and how do I get only the number in the name of the class.
//if code name is ClassName1000, I want to get numbers after ClassName.
#set($className = ????)
#set($number = $className.split("[Name]")[1]);
// if I call $number, I want to get 1000
What should I do? I need some help!

I have not get any better way So the simple way it should be replaceAll
#set($number = $str.replaceAll("[^0-9]", ""));
The value of class: $number

Related

Get unique css selector from WebElement

is it possible to get a css selector of an WebElement?
eg
var optionSelectors = mutableListOf<String>()
val options = selectWebElement?.findElements(By.cssSelector("option")).orEmpty()
for(option in options){
var optionSelector = option.getSelector()
optionSelectors.add(optionSelector)
}
return toJson(optionSelectors)
Thank you in advance
You can always use Reflection to get foundBy property value like:
Field field = element.getClass().getDeclaredField("foundBy");
field.setAccessible(true);
String foundBy = field.get(element).toString();
However the nature of your question is a little bit weird, given you found the element already you should know its selector, shouldn't you? If you want to interact with the Select option values you can go for the relevant Select class which has getOptions() function.
Also consider going for Page Object Model design pattern, it is one of best practices to keep your test logic separate from UI layer

Is it possible to add selector text to a By object

I am wondering if it is possible to add selector text to an existing By object.
That is, suppose I have a popup with a class name ".popup". But then I want to be a little more specific as the webapp is utilized. Now, our popup has an added class name ".cart". My new selector wants to be ".popup.cart". Here is what I have. Is there a better or more efficient way to do this? (I'm coding in C#, but it can be relevant to any language.)
By selector= By.CssSelector("div.popup");
selector = By.CssSelector(
selector.ToString().Remove(0, selector.ToString().LastIndexOf(":") + 1)
+ ".cart");
What is going on above, selector becomes "div.popup".
selector.ToString() returns "By.CssSelector: div.popup"
So I remove all text from the beginning to the last ":" and add the remaining text to ".cart".
selector then becomes "div.popup.cart", which is what I want.
The problem comes when I try to combine any selectors that have been "ByChained." The ToString() method will return By.Chained([By.CssSelector: .popup, By.CssSelector: .title])
If it helps, I am also Sizzle. So, if there's a JQuery way to do this, I'm open.
Put the "div.popup" in a string say "str" like this:-
String str = div.popup
Then you can append ".cart" to the string, make it "div.popup.cart", and use the cssSelector as:
By selector= By.cssSelector(str+".cart");
Note:- Above is a java code.
As mentioned above, I'm basically solving this by unrolling the By.CssSelector().ToString() output and/or the ByChained().ToString() output. There are some caveats. Mainly that the last selector in a ByChained() must be of type By.CssSelector (since I'm adding a JQuery pseudo class):
private static By AddPseudoFunction(By selector, string pseudoFunc)
{
string type = selector.GetType().ToString();
string selectorValue = selector.ToString();
if (type.Contains("Chained"))
{
selectorValue = selectorValue.Remove(0, selector.ToString().IndexOf("[") + 1);
int start = selectorValue.LastIndexOf("]");
int delta = selectorValue.Length - start;
selectorValue = selectorValue.Remove(start, delta);
}
StringBuilder builder = new StringBuilder();
foreach (string s in selectorValue.Split(','))
{
builder.Append(" ");
builder.Append(s.ToString().Remove(0, s.ToString().LastIndexOf(":") + 2));
}
selectorValue = builder.ToString().TrimStart(' ') + pseudoFunc;
return new ByJQuery.ByJQuerySelector(selectorValue, true);
}
Of course, if you want to make this so that it will return a By.CssSelector, just replace the return with:
return By.CssSelector(selctorValue + addSelector);
Where addSelector would be a valid CSS Selector value (like what I describe in the question).

How to iterate multiple times over data during PIG store function

I wonder if it possible to write a user-defined store function for PIG that iterates twice over the data / input tuples.
I read here http://pig.apache.org/docs/r0.7.0/udf.html#Store+Functions how to write your own store function, e.g. by implementing your own "getNext()" method.
For my use case, however, it is necessary to see every tuple twice in the "getNext()" method, so I wonder whether there is a way to that, for example by reseting the reader somehow or by overwriting some other method...
Additional information: I am looking for a way to iterate from tuple 1 to tuple n and then again from 1 to n.
Does anyone has an idea how to do something like that?
Thanks!
Sebastian
This is from top of my head, but you could try something like this:
imports here ...;
class MyStorage extends PigStorage {
private int counter = 0;
private Tuple cachedTuple = null;
public Tuple getNext(){
if (this.counter++ % 2 == 0) {
this.cachedTuple = super.getNext();
}
return this.cachedTuple;
}
}

Laravel 4: Eager loading

I have a table called UserWords that has a word_id column in it, and I want to use that to get the row from the Words table, and kind of concatenate them together like a join. This way each one will have the info from the row in UserWords and Words. I defined the relation like so in UserWord:
class UserWord extends Eloquent{
public function word(){
$this->belongsTo('Word');
}
}
and then after that, I try to get all the UserWords and Words like so:
$words = UserWord::with("word")->
whereRaw("user_id = ".Auth::user()->id.
" AND lang1 = '".$lang1.
"' AND lang2 = '".$lang2."'")
->get();
This works if I don't have the with() in there. So, what am I doing wrong? Or am I going to have to create a raw JOIN to get what I want? I've never done relations before, so maybe I'm thinking of this fundamentally wrong? Either way, please teach me more than just giving the answer if you can! I read the docs and I think it should work, but it doesn't so... I'm asking here.
EDIT:
The error I get is:
Call to a member function addEagerConstraints() on a non-object
The definition of your relation is not correct, you have to return the result of the belongsTo method call. Try the following:
class UserWord extends Eloquent {
public function word(){
return $this->belongsTo('Word');
}
}
$words = UserWord::with('word')
->where('user_id', Auth::user()->id)
->where('lang1', $lang1)
->where('lang2', $lang2)
->get();

New ArrayList filtering from another ArrayList using a String as Filter

In my program, a Die (dice for embroidery) is a class with different fields. One of them is of the type String and it is called haveIt. So, if the user of the program enters the word "Yes" on the haveIt field, he should be able to track a list of all the Dies he has, on the myInventory list.
How do I do this? Should I create the myInventory ArrayList<Die> on the fields and constructor of my Contoller class or should I built it inside a special method in that class?
I have tryed everything and nothing works. But I am really new on this.
Here is my last attempt, creating a loop to create the new ArrayList<Die> (that has "Yes" on the haveIt field) from a special getMyInventory method in my Controller class:
public ArrayList<Die> getMyInventory(Die anyDie) {
for (int counting = 0; counting <
diesBigList.Count ; counting++);
{
if
(((Die)diesBigList[counting]).doIHaveIt.contains("Yes"))
myInventory.add(diesBigList[counting]);
return myInventory;
}
}
It does not compile. It tells me that the result should be an Array type but it is resolved as ArrayList... (and I do not comprendo that).
Thanks in advance.
Your missing a return statement. What if this is never true?
if (((Die)diesBigList[counting]).doIHaveIt.contains("Yes"))
you never reach your return statement
Here is the answer
public ArrayList<Die> getMyInventory(Die anyDie) {
ArrayList<Die> myInventory = new ArrayList<Die>();
for (int counting = 0; counting < diesBigList.Count; counting++) {
if (((Die)diesBigList[counting]).doIHaveIt.contains("Yes")) {
myInventory.add(diesBigList[counting]);
}
}
return myInventory;
}
Also there could be a problem with this: diesBigList.Count I have no idea where you got that object or what it's methods looks like but I presume in my code your making that call correctly.