Module: Seabright::Dumping

Included in:
RedisObject
Defined in:
lib/redis_object/experimental/dumping.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_dump(dump, format = :hash) ⇒ Object

def dump(file=nil) if file && File.exists?(file) # else self.to_yaml end end



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/redis_object/experimental/dumping.rb', line 86

def self.format_dump(dump,format=:hash)
	case format
	when :hash
		return dump
	when :yaml, :yml
		return Psych.dump(dump)
	# JSON format is not ready yet!
	# when :json
	# 	return Yajl::Encoder.encode(dump)
	else
		return dump
	end
end

.included(base) ⇒ Object



135
136
137
# File 'lib/redis_object/experimental/dumping.rb', line 135

def self.included(base)
	base.extend(ClassMethods)
end

Instance Method Details

#dump_collectionsObject



104
105
106
107
108
109
110
111
# File 'lib/redis_object/experimental/dumping.rb', line 104

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

#full_hash_dumpObject



100
101
102
# File 'lib/redis_object/experimental/dumping.rb', line 100

def full_hash_dump
	store.hgetall(hkey).inject({}) {|acc,(k,v)| acc[k.to_sym] = enforce_format(k,v); acc }.merge(dump_collections)
end

#to_json(*discard) ⇒ Object



113
114
115
# File 'lib/redis_object/experimental/dumping.rb', line 113

def to_json(*discard)
	Yajl::Encoder.encode(full_hash_dump)
end

#to_yaml(*discard) ⇒ Object Also known as: to_yml



117
118
119
# File 'lib/redis_object/experimental/dumping.rb', line 117

def to_yaml(*discard)
	Psych.dump(full_hash_dump)
end