Module: Redis::Relations::BelongsTo
- Defined in:
- lib/redis/relations/belongs_to.rb
Class Method Summary collapse
Instance Method Summary collapse
- #belongs_to_references ⇒ Object
- #belongs_to_relation_id(name) ⇒ Object
- #get_belongs_to_reference(name) ⇒ Object
- #save_belongs_to_references ⇒ Object
- #set_belongs_to_reference(name, a) ⇒ Object
Class Method Details
.included(base) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/redis/relations/belongs_to.rb', line 32 def self.included(base) base.class_eval do add_relation :belongs_to class << self def belongs_to(relation_name, = {}) belongs_to_relations[relation_name] = .reverse_merge({ :relation => relation_name }) define_method relation_name do get_belongs_to_reference(relation_name) end define_method "#{relation_name}=" do |a| set_belongs_to_reference(relation_name, a) end end end end end |
Instance Method Details
#belongs_to_references ⇒ Object
2 3 4 |
# File 'lib/redis/relations/belongs_to.rb', line 2 def belongs_to_references @belongs_to_references ||= {} end |
#belongs_to_relation_id(name) ⇒ Object
19 20 21 |
# File 'lib/redis/relations/belongs_to.rb', line 19 def belongs_to_relation_id(name) File.join("references", belongs_to_relations[name][:relation].to_s) end |
#get_belongs_to_reference(name) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/redis/relations/belongs_to.rb', line 10 def get_belongs_to_reference(name) if belongs_to_references.key?(name) belongs_to_references[name] else result = self.class.find(connection.hget(belongs_to_relation_id(name), id)) belongs_to_references[name] = result end end |
#save_belongs_to_references ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/redis/relations/belongs_to.rb', line 23 def save_belongs_to_references belongs_to_references.each do |relation_name, reference| if reference reference = reference.id end connection.hset(belongs_to_relation_id(relation_name), id, reference) end end |
#set_belongs_to_reference(name, a) ⇒ Object
6 7 8 |
# File 'lib/redis/relations/belongs_to.rb', line 6 def set_belongs_to_reference(name, a) belongs_to_references[name] = a end |