Class: Ronin::Script::Path

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/ronin/script/path.rb

Overview

The Path model stores information in the Database about cached Ronin::Script objects.

Since:

  • 1.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Instance Attribute Details

#cache_errorsObject (readonly)

Any cache errors encountered when caching the object

Since:

  • 1.1.0


58
59
60
# File 'lib/ronin/script/path.rb', line 58

def cache_errors
  @cache_errors
end

#cache_exceptionObject (readonly)

Any exceptions raise when loading a fresh object

Since:

  • 1.1.0


55
56
57
# File 'lib/ronin/script/path.rb', line 55

def cache_exception
  @cache_exception
end

Instance Method Details

#cacheBoolean

Caches the freshly loaded object from the cache file into the Database.

Returns:

  • (Boolean)

    Specifies whether the object was successfully cached.

Since:

  • 1.1.0


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.timestamp = 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_scriptModel

The object from the Database that was cached from the file.

Returns:

  • (Model)

    The previously cached object connected to the cache file.

Since:

  • 1.1.0


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_pathString

The path to require to access the Class of the cached object.

Returns:

  • (String)

    The possible path inferred from the class name.

Since:

  • 1.1.0


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

#destroyObject

Before destroying the cached file object, also destroy the associated cached object.

Since:

  • 1.1.0


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_scriptScript?

A freshly loaded Ronin::Script object from the cache file.

Returns:

  • (Script, nil)

    The first Ronin::Script object loaded from the cache file. If nil is returned, the file did not contain any cacheable objects or the cache file contained a syntax error.

Since:

  • 1.1.0


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.

Returns:

  • (Boolean)

    Specifies whether the cache file was deleted.

Since:

  • 1.1.0


185
186
187
# File 'lib/ronin/script/path.rb', line 185

def missing?
  !(self.path.file?)
end

#script_classClass?

The Model of the cached object.

Returns:

  • (Class, nil)

    Returns the Model of the cached object, or nil if the class could not be loaded or found.

Since:

  • 1.1.0


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

#syncBoolean

Syncs the cached object in the Database with the object loaded from the cache file.

Returns:

  • (Boolean)

    Specifies whether the cached object was successfully synced.

Since:

  • 1.1.0


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_sString

Converts the script path to a String.

Returns:

  • (String)

    The path of the script path.

Since:

  • 1.1.0


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.

Returns:

  • (Boolean)

    Specifies whether the cache file was updated.

Since:

  • 1.1.0


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.timestamp

  if File.file?(self.path)
    return self.path.mtime > self.timestamp
  end

  # do not assume updates, if there is no path
  return false
end