Module: MethodSerializer::Cache
- Included in:
- Mock::Ec2
- Defined in:
- lib/deltacloud/method_serializer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #args_hash(args) ⇒ Object
- #cache_dir ⇒ Object
- #cache_file_name(method_name, args) ⇒ Object
- #deserialize_data(method_name, args) ⇒ Object
- #serialize_data(method_name, args, data) ⇒ Object
Class Method Details
.wrap_methods(c, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/deltacloud/method_serializer.rb', line 62 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 |
Instance Method Details
#args_hash(args) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/deltacloud/method_serializer.rb', line 49 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 ⇒ Object
26 27 28 29 30 31 |
# File 'lib/deltacloud/method_serializer.rb', line 26 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) ⇒ Object
56 57 58 59 60 |
# File 'lib/deltacloud/method_serializer.rb', line 56 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) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/deltacloud/method_serializer.rb', line 40 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) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/deltacloud/method_serializer.rb', line 33 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 |