Sha256: 4e0d985b1a256c0a699723c31e495736651f5d488b3d55042c971c712672f557

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

require 'psych'
require 'active_support/core_ext/time/calculations'

module Seabright
	class RedisObject
		
		def self.dump_everything(file=nil)
			out = {}
			ObjectSpace.enum_for(:each_object, class << RedisObject; self; end).each do |cls|
				unless cls == RedisObject
					out[cls.name] = {}
					out[cls.name][:objects] = cls.all.map do |obj|
						obj.full_hash_dump
					end
				end
			end
			Psych.dump(out)
		end
		
		def self.load_dump(str)
			Psych.load(str).each do |(k,v)|
				if klass = RedisObject.deep_const_get(k)
					if v[:objects]
						v[:objects].each do |o|
							load_object klass, o
						end
					end
				end
			end
		end
		
		def self.load_object(klass,pkt)
			puts "Loading a #{klass.name}: #{pkt.inspect}" if DEBUG
			cols = nil
			pkt.delete(:collections).each do |col_name|
				if objs = pkt.delete(col_name.to_sym)
					cols ||= {}
					cols[col_name.to_sym] = objs
				end
			end
			obj = klass.create(pkt)
			if cols
				cols.each do |name,objs|
					puts "  Loading in collected #{name}: #{objs.inspect}" if DEBUG
					obj.collect_type_by_key name, *objs
				end
			end
		end
		
	end
	
	module Dumping
		
		# def dump(file=nil)
		# 	if file && File.exists?(file)
		# 		# 
		# 	else
		# 		self.to_yaml
		# 	end
		# end
		
		def full_hash_dump
			store.hgetall(hkey).inject({}) {|acc,(k,v)| acc[k.to_sym] = enforce_format(k,v); acc }.merge(dump_collections)
		end
		
		def dump_collections
			cols = []
			collections.inject({}) do |acc,(k,v)|
				acc[k.to_sym] = v.map {|o| o.hkey }
				cols << k
				acc
			end.merge(collections: cols)
		end
		
		def to_json
			Yajl::Encoder.encode(full_hash_dump)
		end
		
		def to_yaml
			Psych.dump(full_hash_dump)
		end
		alias_method :to_yml, :to_yaml
		
		module ClassMethods
			
			
			
		end
		
		def self.included(base)
			base.extend(ClassMethods)
		end
		
	end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
redis_object-1.3.2 lib/redis_object/experimental/dumping.rb
redis_object-1.3.1b lib/redis_object/experimental/dumping.rb
redis_object-1.3.1 lib/redis_object/experimental/dumping.rb
redis_object-1.3.0 lib/redis_object/experimental/dumping.rb
redis_object-1.2.9b lib/redis_object/experimental/dumping.rb
redis_object-1.2.9 lib/redis_object/experimental/dumping.rb
redis_object-1.2.8 lib/redis_object/experimental/dumping.rb
redis_object-1.2.7 lib/redis_object/experimental/dumping.rb
redis_object-1.2.6 lib/redis_object/experimental/dumping.rb
redis_object-1.2.5 lib/redis_object/experimental/dumping.rb
redis_object-1.2.4 lib/redis_object/experimental/dumping.rb