Class: Ronin::Script::Path
- Inherits:
-
Object
- Object
- Ronin::Script::Path
- Includes:
- Model
- Defined in:
- lib/ronin/script/path.rb
Overview
The Path model stores information in the Database about cached Ronin::Script objects.
Instance Attribute Summary collapse
-
#cache_errors ⇒ Object
readonly
Any cache errors encountered when caching the object.
-
#cache_exception ⇒ Object
readonly
Any exceptions raise when loading a fresh object.
Instance Method Summary collapse
-
#cache ⇒ Boolean
Caches the freshly loaded object from the cache file into the Database.
-
#cached_script ⇒ Model
The object from the Database that was cached from the file.
-
#class_path ⇒ String
The path to require to access the Class of the cached object.
-
#destroy ⇒ Object
Before destroying the cached file object, also destroy the associated cached object.
-
#load_script ⇒ Script?
A freshly loaded Ronin::Script object from the cache file.
-
#missing? ⇒ Boolean
Determines if the cache file was deleted.
-
#script_class ⇒ Class?
The Model of the cached object.
-
#sync ⇒ Boolean
Syncs the cached object in the Database with the object loaded from the cache file.
-
#to_s ⇒ String
Converts the script path to a String.
-
#updated? ⇒ Boolean
Determines if the cache file was updated.
Methods included from Model
Instance Attribute Details
#cache_errors ⇒ Object (readonly)
Any cache errors encountered when caching the object
58 59 60 |
# File 'lib/ronin/script/path.rb', line 58 def cache_errors @cache_errors end |
#cache_exception ⇒ Object (readonly)
Any exceptions raise when loading a fresh object
55 56 57 |
# File 'lib/ronin/script/path.rb', line 55 def cache_exception @cache_exception end |
Instance Method Details
#cache ⇒ Boolean
Caches the freshly loaded object from the cache file into the Database.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/ronin/script/path.rb', line 198 def cache if (new_script = load_script) # reset the model-class self.class_name = new_script.class.to_s # update the timestamp self. = self.path.mtime # re-cache the newly loaded script new_script.script_path = self if new_script.save @cache_errors = nil return true else @cache_errors = new_script.errors end end return false end |
#cached_script ⇒ Model
The object from the Database that was cached from the file.
113 114 115 116 117 |
# File 'lib/ronin/script/path.rb', line 113 def cached_script if (cached_class = script_class) return cached_class.first(:script_path => self) end end |
#class_path ⇒ String
The path to require to access the Class of the cached object.
68 69 70 71 72 |
# File 'lib/ronin/script/path.rb', line 68 def class_path if self.class_name Support::Inflector.underscore(self.class_name) end end |
#destroy ⇒ Object
Before destroying the cached file object, also destroy the associated cached object.
253 254 255 256 257 258 259 260 261 |
# File 'lib/ronin/script/path.rb', line 253 def destroy unless destroyed? if (script = cached_script) script.destroy! end end super end |
#load_script ⇒ Script?
A freshly loaded Ronin::Script object from the cache file.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ronin/script/path.rb', line 129 def load_script begin # load the first found object blocks = ObjectLoader.load_blocks(self.path) rescue ::Exception => e @cache_exception = e return nil end blocks.each do |object_class,object_block| if object_class < Script # create the fresh object object = object_class.new() begin object.instance_eval(&object_block) @cache_exception = nil return object rescue ::Exception => e @cache_exception = e end end end return nil end |
#missing? ⇒ Boolean
Determines if the cache file was deleted.
185 186 187 |
# File 'lib/ronin/script/path.rb', line 185 def missing? !(self.path.file?) end |
#script_class ⇒ Class?
The Model of the cached object.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ronin/script/path.rb', line 83 def script_class return unless self.class_name # filter out unloadable script classes begin require class_path rescue Gem::LoadError => e raise(e) rescue ::LoadError end # filter out missing class names loaded_class = begin DataMapper::Ext::Object.full_const_get(self.class_name) rescue NameError return nil end # filter out non-script classes return loaded_class if loaded_class < Script end |
#sync ⇒ Boolean
Syncs the cached object in the Database with the object loaded from the cache file.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ronin/script/path.rb', line 229 def sync if missing? # destroy the cached file, if the actual file is missing return destroy elsif updated? if (script = cached_script) # destroy the previously cached object script.destroy! end # if we couldn't cache anything, self-destruct destroy unless cache return true end return false end |
#to_s ⇒ String
Converts the script path to a String.
271 272 273 |
# File 'lib/ronin/script/path.rb', line 271 def to_s self.path.to_s end |
#updated? ⇒ Boolean
Determines if the cache file was updated.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ronin/script/path.rb', line 165 def updated? # assume updates if there is no timestamp return true unless self. if File.file?(self.path) return self.path.mtime > self. end # do not assume updates, if there is no path return false end |