Sha256: bdbde09f7386470d928959adbee7086dcc2dcba176dc344fb89bd6a88ff438db

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module Magent
  module Actor
    def self.included(klass)
      klass.class_eval do
        extend Actor::ClassMethods
        include Actor::InstanceMethods
      end
    end

    module ClassMethods
      def expose(*methods)
        methods.each do |m|
          actions << m.to_s
        end
      end

      def channel_name(name = nil)
        @channel_name ||= (name || Magent::Utils.underscore(self.name))
      end

      def actions
        @actions ||= Set.new
      end

      def can_handle?(action)
        actions.include?(action.to_s)
      end

      def channel
        @channel ||= begin
          channel_name = "/"+self.channel_name
          Channel.new(channel_name)
        end
      end

      def tasks
        @tasks ||= []
      end

      def at_least_every(seconds, &block)
        tasks << {:every => seconds, :last_time => Time.now, :block => block}
      end
    end

    module InstanceMethods
      def _run_tasks
        tasks = self.class.tasks

        return false if tasks.empty?
        performed = false

        tasks.each do |task|
          delta = Time.now - task[:last_time]

          if delta >= task[:every]
            task[:last_time] = Time.now
            begin
              instance_eval(&task[:block])
            rescue Exception => e
              $stderr.puts "Failed periodical task: #{e.message}"
              $stderr.puts e.backtrace.join("\n\t")
            end
            performed = true
          end
        end

        performed
      end
    end
  end # Actor

  def self.register(actor)
    @current_actor = actor
  end

  def self.current_actor
    @current_actor
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magent-0.2 lib/magent/actor.rb