Class: Red::DefinitionNode::ClassNode

Inherits:
Object
  • Object
show all
Defined in:
lib/red/definition_nodes.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(class_name, superclass, scope) ⇒ ClassNode

Returns a new instance of ClassNode.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/red/definition_nodes.rb', line 4

def initialize(class_name, superclass, scope)
  raise(BuildError::NoClassInheritance, "Class inheritance is currently not supported#{" for the #{@@red_library} JavaScript library";''}") if superclass
  old_class = @@red_class
  @@red_class = @class = class_name
  @class_name, @superclass = [class_name, superclass].build_nodes
  block_node = scope.assoc(:block) || scope
  case @@red_library
  when :Prototype
    @initializer = block_node.rassoc(:initialize)
    @properties = block_node.select {|node| (node.first == :cvdecl) rescue false }.build_nodes
    @functions = block_node.select {|node| ![:block, :scope].include?(node) && ((node.first != :cvdecl) rescue false) }.build_nodes
  else
    if initializer_node = block_node.rassoc(:initialize)
      args_node = initializer_node.assoc(:scope).assoc(:block).assoc(:args)
      @arguments = (args_node[1..-1] || []).build_nodes
      @initializer = initializer_node.assoc(:scope).assoc(:block).reject {|node| node == args_node}.build_node
    end
    @properties = block_node.select {|node| (node.first == :cvdecl) rescue false }.build_nodes
    @functions = block_node.select {|node| (node != initializer_node) && ![:block, :scope].include?(node) && ((node.first != :cvdecl) rescue false) }.build_nodes
  end
  @@red_class = old_class
end

Instance Method Details

#compile_as_prototype_classObject



44
45
46
47
48
# File 'lib/red/definition_nodes.rb', line 44

def compile_as_prototype_class
  class_name = @class_name.compile_node
  slots = (@properties | @functions).compile_nodes(:as_prototype => true).compact.join(', ')
  return "%s%s = Class.create({ %s })" % [self.var?, class_name, slots]
end

#compile_as_standard_class(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/red/definition_nodes.rb', line 50

def compile_as_standard_class(options = {})
  class_name = @class_name.compile_node
  arguments = @arguments.compile_nodes.join(', ')
  initializer = @initializer.compile_node
  functions = @functions.compile_nodes(:as_attribute => true).join('; ')
  properties = @properties.compile_nodes.join('; ')
  return "%s%s = function(%s) { %s;%s }; %s" % [self.var?, class_name, arguments, initializer, functions, properties]
end

#compile_as_virtual_class(options = {}) ⇒ Object



59
60
61
62
63
# File 'lib/red/definition_nodes.rb', line 59

def compile_as_virtual_class(options = {})
  class_name = @class_name.compile_node
  slots = (@properties | @functions).compile_nodes(:as_prototype => true).compact.join(', ')
  return "%s%s = { %s }" % [self.var?, class_name, slots]
end

#compile_node(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/red/definition_nodes.rb', line 27

def compile_node(options = {})
  old_class = @@red_class
  @@red_class = @class
  if @initializer
    case @@red_library
    when :Prototype
      output = self.compile_as_prototype_class
    else
      output = self.compile_as_standard_class
    end
  else
    output = self.compile_as_virtual_class
  end
  @@red_class = old_class
  return output
end

#var?Boolean



65
66
67
# File 'lib/red/definition_nodes.rb', line 65

def var?
  return "var " unless @class_name.is_a?(LiteralNode::NamespaceNode)
end