Class: Sol::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/jx/callback.rb

Overview

Class Callback is used for a ruby object to be called from inside a javascript script. When a ruby object is injected into a javascript only public java methods can be called. For this to work, the Callback class implements the RubyCallbackInterface that defines the run public method. In order to execute a ruby method, the javascript code needs to call ‘run’ passing as arguments the method name to be executed in ruby and all its arguments.

Direct Known Subclasses

ArrayCallback, HashCallback

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_obj) ⇒ Callback


Array and Hash quite often


Parameters:

  • ruby_obj (Object)

    a Ruby object, could be any object, in particular we use



67
68
69
# File 'lib/jx/callback.rb', line 67

def initialize(ruby_obj)      
  @ruby_obj = ruby_obj
end

Instance Attribute Details

#ruby_objObject (readonly)

java_import ‘com.teamdev.jxbrowser.chromium.JSAccessible’



42
43
44
# File 'lib/jx/callback.rb', line 42

def ruby_obj
  @ruby_obj
end

#thisObject (readonly)

Returns the value of attribute this.



43
44
45
# File 'lib/jx/callback.rb', line 43

def this
  @this
end

Class Method Details

.build(ruby_obj) ⇒ Object





49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jx/callback.rb', line 49

def self.build(ruby_obj)

  case ruby_obj
  when Hash
    HashCallback.new(ruby_obj)
  when Array
    ArrayCallback.new(ruby_obj)
  else
    Callback.new(ruby_obj)
  end
    
end

.process_args(args) ⇒ Object


Converts given arguments into Ruby arguments arguments to be given to @ruby_obj.


Parameters:

  • args (Array)

    array of javascript arguments to be converted into ruby



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/jx/callback.rb', line 165

def self.process_args(args)

  collect = []

  args.each do |arg|
    if (arg.is_a? Java::ComTeamdevJxbrowserChromium::JSValue)
      collect << Callback.process_arg(arg)
    else
      collect << arg
    end
  end
  
  collect
  
end

Instance Method Details

#build(class_name, *args) ⇒ Object





153
154
155
156
157
# File 'lib/jx/callback.rb', line 153

def build(class_name, *args)
  raise "#{class_name} #{args}"
  klass = get_class(class_name)
  klass.send("new", *args)
end

#default(*args) ⇒ Object


NOT REALLY SURE WHY THIS METHOD IS CALLED… COULD GENERATE AN ERROR SOMEWHERE. NEEDS TO BE FIXED OR AT LEAST UNDERSTOOD!!!




76
77
78
# File 'lib/jx/callback.rb', line 76

def default(*args)
  false
end

#get_class(class_name) ⇒ Object





145
146
147
# File 'lib/jx/callback.rb', line 145

def get_class(class_name)
  Callback.pack(send("const_get", class_name))
end

#is_instance_of(class_name) ⇒ Object


Checks if the ruby_obj is an instance of the given class


Parameters:

  • class_name (String)

    the name of a ruby class



136
137
138
139
# File 'lib/jx/callback.rb', line 136

def is_instance_of(class_name)
  klass = Object.const_get(class_name)
  @ruby_obj.instance_of? klass
end

#run(*args) ⇒ Object


method call. The last argument could be a block. If it is a string, then we try to convert this string into a block. This method is called from javascript, so all args are javascript args and need to be converted to Ruby args for the method to run


Parameters:

  • args (Array)

    the first element of the array is a method to be called on the

Returns:

  • a packed js o



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jx/callback.rb', line 101

def run(*args)
  
  # first argument is the 'method' name 
  method = args.shift
  
  # try to convert last argument to block if it is a String.  If fails to convert
  # the String to a block, then leave the string untouched as the last argument.
  if ((last = args[-1]).is_a? String)
    args.pop
    begin
      blok = (eval "lambda " + last)
    rescue
      blok = nil
      args << last
    end
  end

  # convert all remaining arguments to Ruby 
  params = Callback.process_args(args)

  case @ruby_obj
  when Proc
    B.pack(instance_exec(*params, &(@ruby_obj)))
  else
    B.pack(@ruby_obj.send(method, *params, &blok))
  end
  
end

#set_this(this) ⇒ Object


at the time of calling. called


Parameters:

  • this (javascript JSObject)

    the javascript ‘this’ at the time the block is



87
88
89
# File 'lib/jx/callback.rb', line 87

def set_this(this)
  @this = this
end