Module: Ronin::Core::ClassRegistry::ClassMethods
- Defined in:
- lib/ronin/core/class_registry.rb
Overview
Class-methods.
Instance Method Summary collapse
-
#class_dir(new_dir = nil) ⇒ String
Gets or sets the class directory path.
-
#list_files ⇒ Array<String>
Lists all class files within #class_dir.
-
#load_class(id) ⇒ Class
Loads a class from the #class_dir.
-
#load_class_from_file(file) ⇒ Class
Loads a class from a file.
-
#path_for(id) ⇒ String?
Finds the path for the class
id
. -
#register(id, mod) ⇒ Object
Registers a class with the registry.
-
#registry ⇒ Hash{String => Class}
The class registry.
Instance Method Details
#class_dir(new_dir = nil) ⇒ String
Gets or sets the class directory path.
101 102 103 104 105 106 107 |
# File 'lib/ronin/core/class_registry.rb', line 101 def class_dir(new_dir=nil) if new_dir @class_dir = new_dir else @class_dir || raise(NotImplementedError,"#{self} did not define a class_dir") end end |
#list_files ⇒ Array<String>
Lists all class files within #class_dir.
115 116 117 118 119 |
# File 'lib/ronin/core/class_registry.rb', line 115 def list_files paths = Dir.glob('{**/}*.rb', base: class_dir) paths.each { |path| path.chomp!('.rb') } return paths end |
#load_class(id) ⇒ Class
Loads a class from the #class_dir.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/ronin/core/class_registry.rb', line 221 def load_class(id) # short-circuit if the module is already loaded if (klass = registry[id]) return klass end unless (path = path_for(id)) raise(ClassNotFound,"could not find file for #{id.inspect}") end previous_entries = registry.keys require(path) unless (klass = registry[id]) new_entries = registry.keys - previous_entries if new_entries.empty? raise(ClassNotFound,"file did not register a class: #{path.inspect}") else raise(ClassNotFound,"file registered a class with a different id (#{new_entries.map(&:inspect).join(', ')}): #{path.inspect}") end end return klass end |
#load_class_from_file(file) ⇒ Class
Loads a class from a file.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ronin/core/class_registry.rb', line 186 def load_class_from_file(file) file = File.(file) unless File.file?(file) raise(ClassNotFound,"no such file or directory: #{file.inspect}") end require(file) registry.each_value do |worker_class| class_file, = worker_class.const_source_location(worker_class.name) return worker_class if class_file == file end raise(ClassNotFound,"file did not register a class: #{file.inspect}") end |
#path_for(id) ⇒ String?
Finds the path for the class id
.
161 162 163 164 165 166 167 |
# File 'lib/ronin/core/class_registry.rb', line 161 def path_for(id) path = File.join(class_dir,"#{id}.rb") if File.file?(path) return path end end |
#register(id, mod) ⇒ Object
Registers a class with the registry.
143 144 145 |
# File 'lib/ronin/core/class_registry.rb', line 143 def register(id,mod) registry[id] = mod end |
#registry ⇒ Hash{String => Class}
The class registry.
127 128 129 |
# File 'lib/ronin/core/class_registry.rb', line 127 def registry @registry ||= {} end |