Search in sources :

Example 26 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlAttr method value_set.

@JRubyMethod(name = { "value=", "content=" })
public IRubyObject value_set(ThreadContext context, IRubyObject content) {
    Attr attr = (Attr) node;
    attr.setValue(rubyStringToString(XmlNode.encode_special_chars(context, content)));
    setContent(content);
    return content;
}
Also used : Attr(org.w3c.dom.Attr) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 27 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method key_p.

/**
 * Test if this node has an attribute named <code>rbkey</code>.
 * Overridden in XmlElement.
 */
@JRubyMethod(name = { "key?", "has_attribute?" })
public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {
    if (node instanceof Element) {
        String key = rubyStringToString(rbkey);
        Element element = (Element) node;
        return context.getRuntime().newBoolean(element.hasAttribute(key));
    } else {
        return context.getRuntime().getNil();
    }
}
Also used : Element(org.w3c.dom.Element) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 28 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method namespace_definitions.

/**
 * Return an array of XmlNamespace nodes based on the attributes
 * of this node.
 */
@JRubyMethod
public IRubyObject namespace_definitions(ThreadContext context) {
    // don't use namespace_definitions cache anymore since
    // namespaces might be deleted. Reflecting the result of
    // namesapce removals is complicated, so the cache might not be
    // updated.
    Ruby ruby = context.getRuntime();
    RubyArray namespace_definitions = ruby.newArray();
    if (doc == null)
        return namespace_definitions;
    if (doc instanceof HtmlDocument)
        return namespace_definitions;
    List<XmlNamespace> namespaces = ((XmlDocument) doc).getNamespaceCache().get(node);
    for (XmlNamespace namespace : namespaces) {
        namespace_definitions.append(namespace);
    }
    return namespace_definitions;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 29 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project propane by ruby-processing.

the class MathToolModule method norm_strict.

/**
 * Identical to p5map(value, low, high, 0, 1) but 'clamped'. Numbers outside
 * of the range are clamped to 0 and 1,
 *
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of args must be be numeric
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "norm_strict", rest = true, module = true)
public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    Ruby ruby = context.runtime;
    double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
    double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
    double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
    double max = Math.max(start, stop);
    double min = Math.min(start, stop);
    if (value < min) {
        return mapMt(context, min, start, stop, 0, 1.0);
    }
    if (value > max) {
        return mapMt(context, max, start, stop, 0, 1.0);
    }
    return mapMt(context, value, start, stop, 0, 1.0);
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 30 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project propane by ruby-processing.

the class Vec2 method from_angle.

/**
 * Example of a regular ruby class method Use Math rather than RadLut
 * here!!!
 *
 * @param context ThreadContext
 * @param klazz IRubyObject
 * @param scalar input angle in radians
 * @return new Vec2 object (ruby)
 */
@JRubyMethod(name = "from_angle", meta = true)
public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) {
    Ruby runtime = context.runtime;
    double angle = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
    return Vec2.rbNew(context, klazz, new IRubyObject[] { runtime.newFloat(Math.cos(angle)), runtime.newFloat(Math.sin(angle)) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)304 Ruby (org.jruby.Ruby)157 RubyString (org.jruby.RubyString)83 IRubyObject (org.jruby.runtime.builtin.IRubyObject)77 IOException (java.io.IOException)49 RubyArray (org.jruby.RubyArray)34 Node (org.w3c.dom.Node)30 RaiseException (org.jruby.exceptions.RaiseException)27 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)24 Document (org.w3c.dom.Document)20 ByteList (org.jruby.util.ByteList)19 RubyClass (org.jruby.RubyClass)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 RubyFloat (org.jruby.RubyFloat)13 JRubyClass (org.jruby.anno.JRubyClass)13 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)12 RubyFixnum (org.jruby.RubyFixnum)12 Element (org.w3c.dom.Element)12