Sha256: 30bbf24051c58cf7d82fd4004f4cda7ec583f137c045feec93b447f1ef8dd2ea

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

module Metacosm
  class Model
    include PassiveRecord
    after_create :register_observer, :emit_creation_event
    after_update :emit_updation_event

    private
    def register_observer
      Simulation.current.watch(self)
    end

    def emit_creation_event
      emit(creation_event) if created_event_class
    end

    def emit_updation_event
      emit(updation_event) if updated_event_class
    end

    def attributes_with_external_id
      attrs = to_h
      if attrs.key?(:id)
        new_id_key = self.class.name.split('::').last.underscore + "_id"
        attrs[new_id_key.to_sym] = attrs.delete(:id)
      end
      attrs
    end

    # trim down extenralized attrs for evt
    def attributes_for_event(klass)
      # assume evts attrs are attr_accessible?
      keys_to_keep = klass.instance_methods.find_all do |method|
        method != :== &&
          method != :! &&
          klass.instance_methods.include?(:"#{method}=")
      end

      attributes_with_external_id.
        delete_if {|k,v| !keys_to_keep.include?(k) }
    end

    def assemble_event(klass, addl_attrs={})
      klass.create(attributes_for_event(klass).merge(addl_attrs))
    end

    def creation_event
      assemble_event created_event_class
    end

    def updation_event
      assemble_event updated_event_class
    end

    def created_event_class
      created_event_name = self.class.name + "CreatedEvent"
      Object.const_get(created_event_name) rescue nil
    end

    def updated_event_class
      updated_event_name = self.class.name + "UpdatedEvent"
      Object.const_get(updated_event_name) rescue nil
    end

    def blacklisted_attribute_names
      [ :@observer_peers ]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
metacosm-0.2.1 lib/metacosm/model.rb
metacosm-0.2.0 lib/metacosm/model.rb
metacosm-0.1.8 lib/metacosm/model.rb
metacosm-0.1.7 lib/metacosm/model.rb
metacosm-0.1.6 lib/metacosm/model.rb
metacosm-0.1.5 lib/metacosm/model.rb