Class: Arrow::AppletRegistry::AppletFile

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/appletregistry.rb

Overview

Registry applet filemap data structure.

Constant Summary collapse

DEFAULT_SOURCE_WINDOW_SIZE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(path) ⇒ AppletFile

Create a new Arrow::AppletRegistry::AppletFile for the applet at the given path.



41
42
43
44
45
46
47
# File 'lib/arrow/appletregistry.rb', line 41

def initialize( path )
	@path = path
	@uris = []
	@appletclasses = nil
	@timestamp = File.mtime( path )
	@exception = nil
end

Instance Attribute Details

#exceptionObject

The Exception object that was thrown when trying to load this file, if any



64
65
66
# File 'lib/arrow/appletregistry.rb', line 64

def exception
  @exception
end

#pathObject (readonly)

The fully-qualified path to the applet file



55
56
57
# File 'lib/arrow/appletregistry.rb', line 55

def path
  @path
end

#timestampObject (readonly)

A Time object representing the modification time of the file when it was loaded



61
62
63
# File 'lib/arrow/appletregistry.rb', line 61

def timestamp
  @timestamp
end

#urisObject (readonly)

An Array of URIs that applets contained in this file are mapped to



58
59
60
# File 'lib/arrow/appletregistry.rb', line 58

def uris
  @uris
end

Instance Method Details

#appletclassesObject

Returns an Array of Arrow::Applet classes loaded from this file, loading them if they haven’t already been loaded.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/arrow/appletregistry.rb', line 81

def appletclasses
	unless @appletclasses
		self.log.debug "Loading applet classes from #{@path}"
		@appletclasses = Arrow::Applet.load( @path )
	end

rescue ::Exception => err
	@exception = err
	frames = self.filtered_backtrace
	self.log.error "%s failed to load: %s" % [ path, err.message ]
	self.log.debug "  " + frames.collect {|frame| "[%s]" % frame}.join("  ")
	@appletclasses = []
ensure
	return @appletclasses
end

#exception_lineObject

Return the line of the exception that occurred while loading the applet, if any. If there was no exception, this method returns nil.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/arrow/appletregistry.rb', line 155

def exception_line
	return nil unless @exception
	targetline = nil
	line = nil

	# ScriptErrors have the target line in the message; everything else
	# is assumed to have it in the first line of the backtrace
	if @exception.is_a?( ScriptError )
		targetline = @exception.message
	else
		targetline = @exception.backtrace.first
	end

	# 
	if targetline =~ /.*:(\d+)(?::.*)?$/
		line = Integer( $1 )
	else
		raise "Couldn't parse exception backtrace '%s' for error line." %
			[ targetline ]
	end

	return line
end

#exception_source_window(window_size = DEFAULT_SOURCE_WINDOW_SIZE) ⇒ Object

Return window_size lines surrounding the line of the applet’s loading exception. If there was no loading exception, returns an empty Array.



183
184
185
186
# File 'lib/arrow/appletregistry.rb', line 183

def exception_source_window( window_size=DEFAULT_SOURCE_WINDOW_SIZE )
	return [] unless @exception
	return self.source_window( self.exception_line, window_size )
end

#filtered_backtraceObject

Return the lines of the applet exception’s backtrace up to the first frame of the framework. Returns an empty Array if there is no current exception.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/arrow/appletregistry.rb', line 101

def filtered_backtrace
	return [] unless @exception

	filtered = []
	@exception.backtrace.each do |frame|
		break if frame.include?('lib/arrow/')
		filtered.push( frame )
	end

	return filtered
end

#has_changed?Boolean

Returns true if the corresponding file has changed since it was loaded

Returns:

  • (Boolean)


74
75
76
# File 'lib/arrow/appletregistry.rb', line 74

def has_changed?
	@timestamp != File.mtime( path )
end

#loaded_okay?Boolean

Returns true if this file loaded without error

Returns:

  • (Boolean)


68
69
70
# File 'lib/arrow/appletregistry.rb', line 68

def loaded_okay?
	@exception.nil?
end

#source_linesObject

Return the lines from the applet’s source as an Array.



115
116
117
# File 'lib/arrow/appletregistry.rb', line 115

def source_lines
	return File.readlines( @path )
end

#source_window(linenum, window_size = DEFAULT_SOURCE_WINDOW_SIZE) ⇒ Object

Return window_size lines of the source from the applet surrounding the specified linenum as an Array of Hashes of the form:

{
  :source  => <line of source code>,
  :linenum => <line number>,
  :target  => <true if this is the target line>
}


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/arrow/appletregistry.rb', line 128

def source_window( linenum, window_size=DEFAULT_SOURCE_WINDOW_SIZE )
	linenum -= 1
	before_line = linenum - (window_size / 2)
	after_line = linenum + (window_size / 2.0).ceil

	before_line = 0 if before_line < 0

	self.log.debug "Reading lines %d-%d from %s for source window on line %d" %
		[ before_line, after_line, @path, linenum + 1 ]

	rval = []
	lines = self.source_lines[ before_line .. after_line ]
	lines.each_with_index do |line, i|
		rval << {
			:source  => line.chomp,
			:linenum => before_line + i + 1,
			:target  => (before_line + i) == linenum,
		}
	end

	return rval
end