Module: Promiscuous::Subscriber::Model::Mongoid::ClassMethods

Defined in:
lib/promiscuous/subscriber/model/mongoid.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribe_on(method, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/promiscuous/subscriber/model/mongoid.rb', line 28

def self.subscribe_on(method, options={})
  define_method(method) do |name, *args, &block|
    super(name, *args, &block)
    if @in_subscribe_block
      name = args.last[:as] if args.last.is_a?(Hash) && args.last[:as]
      subscribe(name)
    end
  end
end

Instance Method Details

#__promiscuous_duplicate_key_exception?(e) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/promiscuous/subscriber/model/mongoid.rb', line 46

def __promiscuous_duplicate_key_exception?(e)
  e.message =~ /E11000/
end

#__promiscuous_fetch_existing(id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/promiscuous/subscriber/model/mongoid.rb', line 50

def __promiscuous_fetch_existing(id)
  key = subscribe_foreign_key
  if respond_to?("find_by")
    promiscuous_root_class.find_by(key => id)
  else
    instance = promiscuous_root_class.where(key => id).first
    raise __promiscuous_missing_record_exception.new(promiscuous_root_class, id) if instance.nil?
    instance
  end
end

#__promiscuous_missing_record_exceptionObject



42
43
44
# File 'lib/promiscuous/subscriber/model/mongoid.rb', line 42

def __promiscuous_missing_record_exception
  Mongoid::Errors::DocumentNotFound
end

#subscribe(*args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/promiscuous/subscriber/model/mongoid.rb', line 16

def subscribe(*args, &block)
  super
  return unless block

  begin
    @in_subscribe_block = true
    block.call
  ensure
    @in_subscribe_block = false
  end
end