lib/ronin/object_context.rb in ronin-0.1.1 vs lib/ronin/object_context.rb in ronin-0.1.2
- old
+ new
@@ -23,33 +23,35 @@
require 'ronin/exceptions/unknown_object_context'
require 'ronin/exceptions/object_context_not_found'
require 'ronin/extensions/meta'
require 'ronin/context'
+require 'ronin/model'
-require 'dm-core'
+require 'parameters'
module Ronin
module ObjectContext
include DataMapper::Types
def self.included(base)
base.class_eval do
- include Model
include Context
+ include Parameters
+ include Model
- metaclass_def(:object_contextify) do |name|
+ # The Path to the object context
+ property :object_path, String, :key => true
+
+ # The modification timestamp of the object context
+ property :object_timestamp, EpochTime
+
+ metaclass_def(:objectify) do |name|
ObjectContext.object_contexts[name] = self
contextify name
- # The Path to the object context
- property :object_path, String, :key => true
-
- # The modification timestamp of the object context
- property :object_timestamp, EpochTime
-
meta_def(:load_object) do |path,*args|
ObjectContext.load_object(context_name,path,*args)
end
meta_def(:cache) do |path,*args|
@@ -69,54 +71,10 @@
meta_def(:search) do |*attribs|
all(*attribs).map { |obj| obj.object }
end
- class_def(:object) do
- self.class.load_object(self.object_path)
- end
-
- class_def(:missing?) do
- if self.object_path
- return !(File.file?(self.object_path))
- end
-
- return false
- end
-
- class_def(:stale?) do
- if self.object_timestamp
- return File.mtime(self.object_path) > self.object_timestamp
- end
-
- return false
- end
-
- class_def(:cache) do
- if self.object_path
- self.object_timestamp = File.mtime(self.object_path)
- return save
- end
-
- return false
- end
-
- class_def(:mirror) do
- if self.object_path
- unless File.file?(self.object_path)
- return destroy
- else
- if (!(dirty?) && stale?)
- destroy
- return object.cache
- end
- end
- end
-
- return false
- end
-
# define Repo-level object loader method
Ronin.module_eval %{
def ronin_load_#{name}(path,*args,&block)
new_obj = #{self}.load_object(path,*args)
@@ -194,46 +152,48 @@
#
# Cache all objects loaded from the specified _path_.
#
def ObjectContext.cache_objects(path)
- ObjectContext.load_objects(path).each { |obj| obj.cache }
+ ObjectContext.load_objects(path).each do |obj|
+ obj.cache
+ end
+
+ return nil
end
#
# Cache all objects loaded from the paths within the specified
# _directory_.
#
def ObjectContext.cache_objects_in(directory)
directory = File.expand_path(directory)
paths = Dir[File.join(directory,'**','*.rb')]
- paths.each do |path|
- ObjectContext.load_objects(path).each { |obj| obj.cache }
- end
-
+ paths.each { |path| ObjectContext.cache_objects(path) }
return nil
end
#
# Mirror all objects that were previously cached from paths within
# the specified _directory_. Also cache objects which have yet to
# be cached.
#
def ObjectContext.mirror_objects_in(directory)
directory = File.expand_path(directory)
- paths = Dir[File.join(directory,'**','*.rb')]
+ new_paths = Dir[File.join(directory,'**','*.rb')]
ObjectContext.object_contexts.each_value do |base|
objects = base.all(:object_path.like => "#{directory}%")
- paths -= objects.map { |obj| obj.object_path }
+ new_paths -= objects.map { |obj| obj.object_path }
+ # mirror existing objects
objects.each { |obj| obj.mirror }
end
- paths.each { |path| ObjectContext.cache_objects(path) }
-
+ # cache the remaining new paths
+ new_paths.each { |path| ObjectContext.cache_objects(path) }
return nil
end
#
# Deletes all cached objects that existed in the specified _directory_.
@@ -244,8 +204,75 @@
ObjectContext.object_contexts.each_value do |base|
base.all(:object_path.like => "#{directory}%").destroy!
end
return nil
+ end
+
+ #
+ # Returns a new object loaded from the file pointed to by the
+ # +object_path+ property.
+ #
+ def object
+ self.class.load_object(self.object_path)
+ end
+
+ #
+ # Returns +true+ if the file pointed to by the +object_path+
+ # property is missing, returns +false+ otherwise.
+ #
+ def missing?
+ if self.object_path
+ return !(File.file?(self.object_path))
+ end
+
+ return false
+ end
+
+ #
+ # Returns +true+ if the +object_timestamp+ property is older than
+ # the modification time on the file pointed to by the +object_path+
+ # property, returns +false+ otherwise.
+ #
+ def stale?
+ if self.object_timestamp
+ return File.mtime(self.object_path) > self.object_timestamp
+ end
+
+ return false
+ end
+
+ #
+ # Sets the +object_timestamp+ property to the modification time of the
+ # file pointed to by the +object_path+ property and saves the object.
+ #
+ def cache
+ if self.object_path
+ self.object_timestamp = File.mtime(self.object_path)
+ return save
+ end
+
+ return false
+ end
+
+ #
+ # If the file pointed to by the +object_path+ property is missing, the
+ # object will be destroyed. If the object has not been changed and
+ # is stale?, a newly loaded object from the file pointed to by the
+ # +object_path+ property will be cached.
+ #
+ def mirror
+ if self.object_path
+ unless File.file?(self.object_path)
+ return destroy
+ else
+ if (!(dirty?) && stale?)
+ destroy
+ return object.cache
+ end
+ end
+ end
+
+ return false
end
end
end