Class: ClassRoom::ClassServerContainer

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/classroom.rb

Overview

The class that proxies between user classes on the ClassRoom server and the requests made by client apps. It stores references to the user classes, evals the code, and allows instances to be created of the user classes.

Constant Summary collapse

@@classes =
[]

Class Method Summary collapse

Class Method Details

.add_class(code) ⇒ Object

Add class(es) to the ClassRoom server



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/classroom.rb', line 24

def self.add_class(code)
  base_class = code.scan(/(class|module)\s+([\w\:]+)/).collect{|c| c[1]}.flatten.first
  base_class.untaint
  @@classes << base_class

  # Actually add the class to the ClassRoom server
  begin
    # Force the code to run by untainting it
    code.untaint
    # Force the code into the ClassContainer module and force a
    # SAFE mode where all data is considered tainted. This stops
    # users running system commands, etc.
    eval("module ClassRoom; module ClassContainer\n$SAFE = 3\n" + code + "\nend; end")
  rescue
    # If the user's code was bad, duck out.
    return nil
  end

  # Now the code is loaded.. loop through all of the classes and add their
  # full (nested) names to the class list so automatic loading
  # can work for clients
  classes_to_check = [base_class]
  classes_to_check.each do |class_name|
    class_internal = eval("ClassRoom::ClassContainer::#{class_name}")
    class_internal.constants.each do |subclass_name| 
      if class_internal.const_get(subclass_name).is_a?(Class) || class_internal.const_get(subclass_name).is_a?(Module)
        sub_class = class_name + "::" + subclass_name
        @@classes << sub_class
        classes_to_check << sub_class
      end
    end          
  end
  
  # Hack to make sure the classes array is clean
  @@classes.collect! { |c| c.to_sym }
  @@classes.uniq!
  true
end

.class_method(class_name, method_name, *args) ⇒ Object

Call a class method on a class in the ClassContainer (this might need work to check what comes back and whether to mix in DRbUndumped if it’s an object)



102
103
104
105
106
107
108
109
110
# File 'lib/classroom.rb', line 102

def self.class_method(class_name, method_name, *args)
  begin
    obj = eval('ClassRoom::ClassContainer::' + class_name.to_s).send(method_name.to_s, *args)
    obj.extend(DRbUndumped) if self.classes.detect { |c| obj.class.to_s.index(c.to_s) }
    obj
  rescue
    nil
  end
end

.classesObject

Return a list of all classes in the ClassContainer (must be a nicer way to do this without logging it ourselves..?)



84
85
86
# File 'lib/classroom.rb', line 84

def self.classes
  return @@classes
end

.new_instance_of(class_name, *args) ⇒ Object

Return a new instance of a class in the ClassContainer



89
90
91
92
93
94
95
96
97
# File 'lib/classroom.rb', line 89

def self.new_instance_of(class_name, *args)
  begin
    obj = eval('ClassRoom::ClassContainer::' + class_name.to_s).new(*args)
    obj.extend(DRbUndumped)
    obj
  rescue
    nil
  end
end