Sha256: 1f2250aa564b28526c22e19b77ace0f55521ff980d9ca0cc946c2a7856f34a26

Contents?: true

Size: 1.55 KB

Versions: 11

Compression:

Stored size: 1.55 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom'))
class Fathom::Simulation
  
  class << self
    
    # Expects a symbol for the callback to define on the simulation.
    def define_callback_accessor(callback, obj)
      call_method = callback.to_s.scan(/^on_(\w+)/).flatten.compact.first
      raise ArgumentError, "Callback doesn't appear to be a callback: #{callback}" unless call_method
      return true if self.respond_to?(call_method)
      define_method(call_method) do
        callbacks[callback].each do |agent|
          agent.send(callback, obj)
        end
      end
      
      agent_lookup_method = "agents_using_#{call_method}".to_sym
      return true if self.respond_to?(agent_lookup_method)
      define_method(agent_lookup_method) do
        callbacks[callback]
      end
      
    end
  end
  
  attr_reader :agents
  
  def initialize(*agents)
    @agents = agents
    assert_callbacks
  end
  
  def callbacks
    @callbacks ||= {}
  end
  
  protected
    def assert_callbacks
      self.agents.each do |agent|
        assert_agent(agent)
      end
    end
    
    def assert_agent(agent)
      return false unless agent.respond_to?(:callbacks)
      agent.callbacks.each do |callback|
        callback_sym = callback.to_sym
        self.callbacks[callback_sym] ||= []
        self.callbacks[callback_sym] << agent
        self.class.define_callback_accessor(callback_sym, self)
      end
    end
end

if __FILE__ == $0
  include Fathom
  # TODO: Is there anything you want to do to run this file on its own?
  # Simulation.new
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fathom-0.3.7 lib/fathom/simulation.rb
fathom-0.3.6 lib/fathom/simulation.rb
fathom-0.3.4 lib/fathom/simulation.rb
fathom-0.3.3 lib/fathom/simulation.rb
fathom-0.3.2 lib/fathom/simulation.rb
fathom-0.3.1 lib/fathom/simulation.rb
fathom-0.3.0 lib/fathom/simulation.rb
fathom-0.2.3 lib/fathom/simulation.rb
fathom-0.2.2 lib/fathom/simulation.rb
fathom-0.2.1 lib/fathom/simulation.rb
fathom-0.2.0 lib/fathom/simulation.rb