Class/Module Index [+]

Quicksearch

MethodSerializer::Cache

Public Class Methods

wrap_methods(c, opts={}) click to toggle source
# File lib/deltacloud/method_serializer.rb, line 60
def self.wrap_methods(c, opts={})
  $methods_cache_dir = opts[:cache_dir]
  $scenario_prefix = nil
  c.class_eval do
    cached_methods.each do |m|
      next if c.instance_methods(false).include?("original_#{m}")
      alias_method "original_#{m}".to_sym, m.to_sym
      define_method m.to_sym do |*args|
        args = args.first if args.size.eql?(1) and not args.first.class.eql?(Array)
        output = deserialize_data(m, args)
        unless output
          output = method("original_#{m}".to_sym).to_proc[args]
          return serialize_data(m, args, output)
        else
          return output
        end
      end
    end
  end
end

Public Instance Methods

args_hash(args) click to toggle source
# File lib/deltacloud/method_serializer.rb, line 47
def args_hash(args)
  if args.class == Hash
    args = args.to_a.collect {|i| [i[0].to_s, i[1]]}.sort
  end
  Digest::SHA1.hexdigest(args.to_s)
end
cache_dir() click to toggle source
# File lib/deltacloud/method_serializer.rb, line 24
def cache_dir
  storage_dir = $methods_cache_dir || File.join(File.dirname(__FILE__), 'cache')
  class_dir = self.class.name.split('::').last
  class_dir ||= self.class.name
  File.join(storage_dir, class_dir.downcase)
end
cache_file_name(method_name, args) click to toggle source
# File lib/deltacloud/method_serializer.rb, line 54
def cache_file_name(method_name, args)
  FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
  method_name = $scenario_prefix ? "#{$scenario_prefix}_#{method_name}" : method_name
  File.join(cache_dir, "#{method_name}.#{args_hash(args)}")
end
deserialize_data(method_name, args) click to toggle source
# File lib/deltacloud/method_serializer.rb, line 38
def deserialize_data(method_name, args)
  begin
    data = File.readlines(cache_file_name(method_name, args)).join
    Marshal.load(Base64.decode64(data))
  rescue Errno::ENOENT
    return false
  end
end
serialize_data(method_name, args, data) click to toggle source
# File lib/deltacloud/method_serializer.rb, line 31
def serialize_data(method_name, args, data)
  File.open(cache_file_name(method_name, args), 'w') do |f|
    f.puts(Base64.encode64(Marshal.dump(data)))
  end
  return data
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.