Module: ActiveRedis::Relation::InstanceMethods
- Included in:
- Base
- Defined in:
- lib/active_redis/relation.rb
Class Method Summary collapse
-
.create_relation_methods ⇒ void
Generate methods for relations.
-
.handle_relation(relation_key: nil, value: nil, action: :add) ⇒ void
Create a set for each relation.
Class Method Details
.create_relation_methods ⇒ void
This method returns an undefined value.
Generate methods for relations.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_redis/relation.rb', line 13 def create_relation_methods self.class.instance_eval do (@has_many || []).each do |x| define_method(x.to_s.pluralize) do klazz = Object.const_get(x.to_s.capitalize.singularize) klazz.where( basename: "#{basename}:#{x.to_s.downcase.pluralize}") end end (@belongs_to || []).each do |x| keys.merge!({"#{x.to_s.downcase.singularize}_id".to_sym => Integer}) define_method(x.to_s.singularize) do klazz = Object.const_get(x.to_s.capitalize.singularize) klazz.find send("#{x.to_s.downcase.singularize}_id") end end (@has_one || []).each do |x| define_method(x.to_s) do klazz = Object.const_get(x.to_s.capitalize.singularize) klazz.find_by(basename: "#{basename}:"+ "#{x.to_s.downcase.singularize}") end end end end |
.handle_relation(relation_key: nil, value: nil, action: :add) ⇒ void
This method returns an undefined value.
Create a set for each relation. This is needed to fetch directly the objects by id from redis store. Nobody wan’t to do it only at application level.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_redis/relation.rb', line 50 def handle_relation relation_key: nil, value: nil, action: :add blto = self.class.instance_variable_get(:@belongs_to) relation = relation_key.to_s[/^([a-z_]+)_id$/,1].to_s if blto.is_a?(Array) and blto.include?(relation.to_sym) if action == :add connection.sadd( "#{relation.pluralize}:#{value}:#{self.class.cname}", id) elsif action == :del connection.srem( "#{relation.pluralize}:"+ "#{value || send(relation_key)}:"+ "#{self.class.cname}", id) end end end |