Skip to content

Commit

Permalink
Impl KeyError.initialize with receiver and key kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 5, 2020
1 parent 6fe36b9 commit 0c1fa51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
46 changes: 46 additions & 0 deletions core/src/main/java/org/jruby/RubyKeyError.java
Expand Up @@ -33,15 +33,18 @@

import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.exceptions.KeyError;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
* @author Miguel Landaeta
*/
@JRubyClass(name="KeyError", parent="IndexError")
public class RubyKeyError extends RubyIndexError {
private static final String[] VALID_KEYS = {"receiver", "key"};
private IRubyObject receiver;
private IRubyObject key;

Expand All @@ -67,6 +70,49 @@ protected RaiseException constructThrowable(String message) {
return new KeyError(message, this);
}

@JRubyMethod
public IRubyObject initialize(ThreadContext context, IRubyObject messageOrKwargs) {
IRubyObject[] receiverKey = ArgsUtil.extractKeywordArgs(context, messageOrKwargs, VALID_KEYS);

if (receiverKey == null) return initialize(context, messageOrKwargs, null);

return initializeCommon(context, context.nil, receiverKey);
}

@JRubyMethod
public IRubyObject initialize(ThreadContext context, IRubyObject message, IRubyObject kwargs) {
IRubyObject[] receiverKey = ArgsUtil.extractKeywordArgs(context, kwargs, VALID_KEYS);

return initializeCommon(context, message, receiverKey);
}

private IRubyObject initializeCommon(ThreadContext context, IRubyObject message, IRubyObject[] receiverKey) {
IRubyObject receiver;
IRubyObject key;
if (receiverKey == null) {
receiver = context.nil;
key = context.nil;
} else {
receiver = receiverKey[0];
key = receiverKey[1];
}

setMessage(message);
this.receiver = receiver;
this.key = key;

return context.nil;
}

@JRubyMethod
public IRubyObject initialize(ThreadContext context, IRubyObject message, IRubyObject receiver, IRubyObject key) {
setMessage(message);
this.receiver = receiver;
this.key = key;

return context.nil;
}

@JRubyMethod
public IRubyObject receiver() {
return receiver;
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/org/jruby/ast/util/ArgsUtil.java
Expand Up @@ -145,10 +145,16 @@ public void visit(IRubyObject key, IRubyObject value) {

// not used
public static IRubyObject[] extractKeywordArgs(ThreadContext context, IRubyObject[] args, String... validKeys) {
IRubyObject options = ArgsUtil.getOptionsArg(context.runtime, args);
return extractKeywordArgs(context, ArgsUtil.getOptionsArg(context.runtime, args), validKeys);
}

public static IRubyObject[] extractKeywordArgs(ThreadContext context, IRubyObject maybeKwargs, String... validKeys) {
IRubyObject options = ArgsUtil.getOptionsArg(context.runtime, maybeKwargs);

if (options instanceof RubyHash) {
return extractKeywordArgs(context, (RubyHash) options, validKeys);
}

return null;
}

Expand Down

0 comments on commit 0c1fa51

Please sign in to comment.