Class: RedisHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/redis-struct.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



33
34
35
# File 'lib/redis-struct.rb', line 33

def [](key)
	@database.get prefix(key)
end

#[]=(key, value) ⇒ Object



29
30
31
# File 'lib/redis-struct.rb', line 29

def []=(key,value)
	@database.set prefix(key), value
end

#add_hashObject



79
80
81
82
83
# File 'lib/redis-struct.rb', line 79

def add_hash
	@hash.each_pair do |key,value|
		self[key] = value
	end
end

#config_database(database) ⇒ Object



9
10
11
# File 'lib/redis-struct.rb', line 9

def config_database(database)
	@database = database
end

#config_hash(hash) ⇒ Object

def config_redis_struct(redis_struct) @redis_struct = redis_struct end



25
26
27
# File 'lib/redis-struct.rb', line 25

def config_hash(hash)
	@hash = hash
end

#config_prefix(prefix) ⇒ Object



13
14
15
# File 'lib/redis-struct.rb', line 13

def config_prefix(prefix)
	@prefix = prefix
end

#config_suffix(suffix) ⇒ Object



17
18
19
# File 'lib/redis-struct.rb', line 17

def config_suffix(suffix)
	@suffix = suffix
end

#delete(key) ⇒ Object



85
86
87
# File 'lib/redis-struct.rb', line 85

def delete(key)
	@database.del prefix(key)
end

#dupObject



51
52
53
# File 'lib/redis-struct.rb', line 51

def dup 
	self
end

#each_key(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/redis-struct.rb', line 37

def each_key(&block)
	keys = @database.keys prefix("*")
	keys.each do |key|
		yield unprefix(key)
	end
end

#each_pairObject

(&block) ## I don’t think this works.



44
45
46
47
48
49
# File 'lib/redis-struct.rb', line 44

def each_pair # (&block) ## I don't think this works.
	each_key do |key|
		value = self[key]
		yield unprefix(key), value
	end
end

#get_suffixObject



67
68
69
# File 'lib/redis-struct.rb', line 67

def get_suffix
	@suffix || self.object_id
end

#prefix(key) ⇒ Object



63
64
65
# File 'lib/redis-struct.rb', line 63

def prefix(key)
	"#{@prefix}:#{get_suffix}:#{key}"
end

#prefix_with_id(key) ⇒ Object



75
76
77
# File 'lib/redis-struct.rb', line 75

def prefix_with_id(key)
	"#{@prefix}:#{self.object_id}:#{key}"
end

#to_hObject



55
56
57
58
59
60
61
# File 'lib/redis-struct.rb', line 55

def to_h
	hash = {}
	each_key do |key|
		hash[key] = self[key]
	end
	hash
end

#unprefix(key) ⇒ Object



71
72
73
# File 'lib/redis-struct.rb', line 71

def unprefix(key)
	key.sub /^#{@prefix}:#{get_suffix}:/, ''
end