Class: Wrapture::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapture/scope.rb

Overview

Describes a scope of one or more class specifications.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec = nil) ⇒ Scope

Creates an empty scope with no classes in it.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wrapture/scope.rb', line 34

def initialize(spec = nil)
  @classes = []
  @enums = []
  @templates = []

  return if spec.nil?

  @version = Wrapture.spec_version(spec)

  @templates = spec.fetch('templates', []).collect do |template_hash|
    TemplateSpec.new(template_hash)
  end

  @classes = spec.fetch('classes', []).collect do |class_hash|
    ClassSpec.new(class_hash, scope: self)
  end

  @enums = spec.fetch('enums', []).collect do |enum_hash|
    EnumSpec.new(enum_hash)
  end
end

Instance Attribute Details

#classesObject (readonly)

A list of classes currently in the scope.



25
26
27
# File 'lib/wrapture/scope.rb', line 25

def classes
  @classes
end

#enumsObject (readonly)

A list of enumerations currently in the scope.



28
29
30
# File 'lib/wrapture/scope.rb', line 28

def enums
  @enums
end

#templatesObject (readonly)

A list of the templates defined in the scope.



31
32
33
# File 'lib/wrapture/scope.rb', line 31

def templates
  @templates
end

Instance Method Details

#<<(spec) ⇒ Object

Adds a class or template specification to the scope.

This does not set the scope as the owner of the class for a ClassSpec. This must be done during the construction of the class spec.



60
61
62
63
64
# File 'lib/wrapture/scope.rb', line 60

def <<(spec)
  @templates << spec if spec.is_a?(TemplateSpec)
  @classes << spec if spec.is_a?(ClassSpec)
  @enums << spec if spec.is_a?(EnumSpec)
end

#add_class_spec_hash(spec) ⇒ Object

Adds a class to the scope created from the given specification hash.



67
68
69
# File 'lib/wrapture/scope.rb', line 67

def add_class_spec_hash(spec)
  ClassSpec.new(spec, scope: self)
end

#add_enum_spec_hash(spec) ⇒ Object

Adds an enumeration to the scope created from the given specification hash.



73
74
75
# File 'lib/wrapture/scope.rb', line 73

def add_enum_spec_hash(spec)
  @enums << EnumSpec.new(spec)
end

#generate_wrappersObject

Generates the wrapper class files for all classes in the scope.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/wrapture/scope.rb', line 78

def generate_wrappers
  files = []

  @classes.each do |class_spec|
    files.concat(class_spec.generate_wrappers)
  end

  @enums.each do |enum_spec|
    files.concat(enum_spec.generate_wrapper)
  end

  files
end

#overloads(parent) ⇒ Object

A list of ClassSpecs in this scope that are overloads of the given class.



93
94
95
# File 'lib/wrapture/scope.rb', line 93

def overloads(parent)
  @classes.select { |class_spec| class_spec.overloads?(parent) }
end

#overloads?(parent) ⇒ Boolean

True if there is an overload of the given class in this scope.

Returns:

  • (Boolean)


98
99
100
# File 'lib/wrapture/scope.rb', line 98

def overloads?(parent)
  @classes.any? { |class_spec| class_spec.overloads?(parent) }
end

#type(type) ⇒ Object

Returns the ClassSpec for the given type in the scope, if one exists.



103
104
105
# File 'lib/wrapture/scope.rb', line 103

def type(type)
  @classes.find { |class_spec| class_spec.name == type.base }
end

#type?(type) ⇒ Boolean

Returns true if there is a class matching the given type in this scope.

Returns:

  • (Boolean)


108
109
110
# File 'lib/wrapture/scope.rb', line 108

def type?(type)
  @classes.any? { |class_spec| class_spec.name == type.base }
end