Sha256: 0aa41896fb1ee7a9c1a618ebf3a6f82d1a8e90f2c473a759232a129809f74ad8

Contents?: true

Size: 1.03 KB

Versions: 21

Compression:

Stored size: 1.03 KB

Contents

class Reactor::Subscriber < ActiveRecord::Base
  attr_accessible :event
  attr_accessor :message

  def event=(event)
    write_attribute :event, event.to_s
  end

  def fire(data)
    self.message = Reactor::Event.new(data)
    instance_exec &self.class.on_fire
    self
  end

  class << self
    def on_fire(&block)
      if block
        @fire_block = block
      end
      @fire_block
    end

    def fire(subscriber_id, data)
      Reactor::Subscriber.find(subscriber_id).fire data
    end

    def subscribes_to(name = nil, data = {})
      #subscribers << name
      #TODO: REMEMBER SUBSCRIBERS so we can define them in code as well as with a row in the DB
      # until then, here's a helper to make it easy to create with random data in postgres
      # total crap I know but whatever
      define_singleton_method :exists! do
        chain = where(event: name)
        data.each do |key, value|
          chain = chain.where("subscribers.data @> ?", "#{key}=>#{value}")
        end
        chain.first_or_create!(data)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
reactor-0.6.0 lib/reactor/models/subscriber.rb
reactor-0.5.3 lib/reactor/models/subscriber.rb
reactor-0.5.2 lib/reactor/models/subscriber.rb
reactor-0.5.1 lib/reactor/models/subscriber.rb
reactor-0.5.0 lib/reactor/models/subscriber.rb
reactor-0.4.7 lib/reactor/models/subscriber.rb
reactor-0.4.6 lib/reactor/models/subscriber.rb
reactor-0.4.5 lib/reactor/models/subscriber.rb
reactor-0.4.4 lib/reactor/models/subscriber.rb
reactor-0.4.3 lib/reactor/models/subscriber.rb
reactor-0.4.2 lib/reactor/models/subscriber.rb
reactor-0.4.0 lib/reactor/models/subscriber.rb
reactor-0.3.2 lib/reactor/models/subscriber.rb
reactor-0.3.1 lib/reactor/models/subscriber.rb
reactor-0.3.0 lib/reactor/models/subscriber.rb
reactor-0.2.10 lib/reactor/models/subscriber.rb
reactor-0.2.9 lib/reactor/models/subscriber.rb
reactor-0.2.8 lib/reactor/models/subscriber.rb
reactor-0.2.7 lib/reactor/models/subscriber.rb
reactor-0.2.6 lib/reactor/models/subscriber.rb