| BeanGlobalVariable.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 */
14
15 package com.liferay.portal.scripting.ruby;
16
17 import org.jruby.Ruby;
18 import org.jruby.javasupport.Java;
19 import org.jruby.javasupport.JavaObject;
20 import org.jruby.javasupport.JavaUtil;
21 import org.jruby.runtime.Block;
22 import org.jruby.runtime.IAccessor;
23 import org.jruby.runtime.builtin.IRubyObject;
24
25 /**
26 * <a href="BeanGlobalVariable.java.html"><b><i>View Source</i></b></a>
27 *
28 * @author Alberto Montero
29 */
30 class BeanGlobalVariable implements IAccessor {
31
32 public BeanGlobalVariable(Ruby ruby, Object bean, Class<?> type) {
33 _ruby = ruby;
34 _bean = bean;
35 _type = type;
36 }
37
38 public IRubyObject getValue() {
39 IRubyObject value = JavaUtil.convertJavaToRuby(
40 _ruby, _bean, _type);
41
42 if (value instanceof JavaObject) {
43 return Java.wrap(_ruby, value);
44 }
45 else {
46 return value;
47 }
48 }
49
50 public IRubyObject setValue(IRubyObject value) {
51 Object bean = Java.ruby_to_java(
52 _ruby.getObject(), value, Block.NULL_BLOCK);
53
54 _bean = JavaUtil.convertArgument(_ruby, bean, _type);
55
56 return value;
57 }
58
59 private Object _bean;
60 private Ruby _ruby;
61 private Class<?> _type;
62
63 }