Module: RedisObj::Relations::Initializers
- Defined in:
- lib/redis_obj/relations.rb
Instance Method Summary collapse
- #inherited(subklass) ⇒ Object
- #redis_hash(name, opts = {}) ⇒ Object
- #redis_list(name, opts = {}) ⇒ Object
- #redis_obj(type, name, opts = {}) ⇒ Object
- #redis_prefix(ctx) ⇒ Object
- #redis_set(name, opts = {}) ⇒ Object
- #redis_sorted_set(name, opts = {}) ⇒ Object
- #store_redis_in(&blk) ⇒ Object
Instance Method Details
#inherited(subklass) ⇒ Object
68 69 70 71 |
# File 'lib/redis_obj/relations.rb', line 68 def inherited subklass super subklass.instance_variable_set(:@store_redis_in,@store_redis_in) end |
#redis_hash(name, opts = {}) ⇒ Object
52 53 54 |
# File 'lib/redis_obj/relations.rb', line 52 def redis_hash name, opts={} redis_obj(RedisObj::Hash,name,opts) end |
#redis_list(name, opts = {}) ⇒ Object
64 65 66 |
# File 'lib/redis_obj/relations.rb', line 64 def redis_list name, opts={} redis_obj(RedisObj::List,name,opts) end |
#redis_obj(type, name, opts = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/redis_obj/relations.rb', line 21 def redis_obj type, name, opts={} opts[:key] = name unless opts.has_key?(:key) opts[:redis] ||= -> { RedisObj.redis } klass_method = "redis_#{name}" instance_variable = "@#{name}" define_method name do unless obj = instance_variable_get(instance_variable) obj = self.class.send(klass_method,self) instance_variable_set(instance_variable,obj) end obj end define_singleton_method klass_method do |context| redis = opts[:redis] if redis.respond_to?(:call) redis = redis.call end if opts[:key] == false type.new(redis, "#{redis_prefix(context)}") else type.new(redis, "#{redis_prefix(context)}:#{opts[:key]}") end end end |
#redis_prefix(ctx) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/redis_obj/relations.rb', line 14 def redis_prefix ctx if ctx.is_a? ::Hash ctx = OpenStruct.new(ctx) end ctx.instance_exec(&store_redis_in) end |
#redis_set(name, opts = {}) ⇒ Object
56 57 58 |
# File 'lib/redis_obj/relations.rb', line 56 def redis_set name, opts={} redis_obj(RedisObj::Set,name,opts) end |
#redis_sorted_set(name, opts = {}) ⇒ Object
60 61 62 |
# File 'lib/redis_obj/relations.rb', line 60 def redis_sorted_set name, opts={} redis_obj(RedisObj::SortedSet,name,opts) end |
#store_redis_in(&blk) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/redis_obj/relations.rb', line 3 def store_redis_in &blk if blk @store_redis_in = blk else @store_redis_in ||= begin base_class = self.class.to_s.downcase Proc.new { "#{base_class}:#{id}" } end end end |