Class: EleetScript::RubyToEleetWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/ruby_to_eleet_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(ruby_obj, engine, options = {}) ⇒ RubyToEleetWrapper

Returns a new instance of RubyToEleetWrapper.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 3

def initialize(ruby_obj, engine, options = {})
  @ruby_obj = ruby_obj
  @engine = engine
  @options = options
  if @options[:lock]
    @options[:lock] = [@options[:lock]] unless @options[:lock].kind_of?(Array)
  end
  if @options[:allow]
    @options[:allow] = [@options[:allow]] unless @options[:allow].kind_of?(Array)
  end
end

Instance Method Details

#call(method_name, args = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 15

def call(method_name, args = [])
  unless can_call_method?(method_name)
    @engine["Errors"] < "Attempt to call locked method \"#{method_name}\" failed."
    return Values.to_eleet_value(@engine["nil"], @engine)
  end
  if method_name.to_sym == :to_string
    method_name = :to_s
  elsif method_name.to_sym == :class_ref
    method_name = :class
  elsif method_name.to_sym == :class_name
    cls = if @ruby_obj.class == Class
      @ruby_obj
    else
      @ruby_obj.class
    end
    return Values.to_eleet_value(cls.name, @engine)
  end
  ruby_args = args.map { |arg| Values.to_ruby_value(arg, @engine) }
  block = if ruby_args.length > 0 && ruby_args.last.is_a?(RubyLambda)
    ruby_args.pop
  else
    nil
  end
  begin
    Values.to_eleet_value(@ruby_obj.send(method_name, *ruby_args, &block), @engine)
  rescue NoMethodError => e
    Values.to_eleet_value(@engine["nil"], @engine)
  end
end

#can_call_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 45

def can_call_method?(method_name)
  method_name = method_name.to_sym
  # Check our options first, they can override class defined locks/allows.
  if @options[:allow]
    if @options[:allow].include?(method_name)
      return true
    else
      return false
    end
  end
  if @options[:lock]
    if @options[:lock].include?(method_name)
      return false
    else
      return true
    end
  end
  # Check the Whitelist
  if @ruby_obj.respond_to?(:eleetscript_allow_methods)
    val = @ruby_obj.eleetscript_allow_methods
    return false if val == :none
    return true if val == :all
    if val.kind_of?(Array) && val.include?(method_name)
      return true
    else
      return false
    end
  end
  # Check the Blacklist
  if @ruby_obj.respond_to?(:eleetscript_lock_methods)
    val = @ruby_obj.eleetscript_lock_methods
    return true if val == :none
    return false if val == :all
    if val.kind_of?(Array) && val.include?(method_name)
      return false
    else
      return true
    end
  end
  # If not specifically allowed/denied, then it can be accessed.
  true
end

#class?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 108

def class?
  Values.to_eleet_value(@ruby_obj.class == Class, @engine)
end

#instance?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 112

def instance?
  !class?
end

#is_a?(name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 92

def is_a?(name)
  cls = if @ruby_obj.class == Class
    @ruby_obj
  else
    @ruby_obj.class
  end
  cls_names = cls.ancestors.map do |a|
    if a.name.nil?
      nil
    else
      a.name.split("::").last
    end
  end
  cls_names.compact.include?(name)
end

#nameObject



116
117
118
119
120
121
122
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 116

def name
  if @ruby_obj.class == Class
    @ruby_obj.name
  else
    @ruby_obj.class.name
  end
end

#rawObject



88
89
90
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 88

def raw
  @ruby_obj
end

#runtime_classObject



124
125
126
127
128
129
130
131
# File 'lib/engine/ruby_to_eleet_wrapper.rb', line 124

def runtime_class
  cls = if @ruby_obj.class == Class
    @ruby_obj
  else
    @ruby_obj.class
  end
  Values.to_eleet_value(cls)
end