Module: Rediline::Object::ClassMethods

Defined in:
lib/rediline/object.rb

Instance Method Summary collapse

Instance Method Details

#rediline(field_name, attrs) ⇒ Object



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
51
52
53
# File 'lib/rediline/object.rb', line 23

def rediline(field_name, attrs)
  attrs.symbolize_keys!
  callback = attrs.delete :when
  
  define_method "rediline_#{callback}" do
    if attrs.frozen?
      entry = Rediline::Entry.new(attrs.dup, attrs[:queries])
    else
      attrs[:object] = self
      case attrs[:user]
        when Symbol, String
          attrs[:user] = send(attrs[:user])
        when Proc
          attrs[:user] = attrs[:user].call(self)
        when nil
          attrs[:user] = send(:user)
      end
      attrs.freeze
      entry = Rediline::Entry.new(attrs.dup, attrs[:queries])
    end
    
    entry.user.send(field_name).lists.each_pair do |k, v|
      v.each do |user|
        rediline_insert! entry, rediline_key(field_name, entry, k, user)
      end
    end
    
    true
  end
  send(callback, "rediline_#{callback}")
end