Sha256: 9faa6475c0d52fd90b82b956e5c24b5ac1972ef76d575d16edbc11d133108aa7

Contents?: true

Size: 974 Bytes

Versions: 7

Compression:

Stored size: 974 Bytes

Contents

module PowerTypes
  module Observable
    @@observable_disabled = false

    def self.observable_disabled=(_value)
      @@observable_disabled = _value
    end

    def self.observable_disabled?
      @@observable_disabled
    end

    def self.included(_klass)
      _klass.extend ClassMethods
    end

    PowerTypes::Util::OBSERVABLE_EVENTS.each do |event|
      define_method("_run_#{event}_callbacks") do |&_block|
        self.class.observers.each { |o| o.trigger(:before, event, self) }
        result = super &_block
        self.class.observers.each { |o| o.trigger(:after, event, self) }
        result
      end
    end

    module ClassMethods
      def observers
        return [] if PowerTypes::Observable.observable_disabled?
        @observers ||= [].tap do |array|
          begin
            array << Kernel.const_get("#{self}Observer")
          rescue NameError
            # could not find observer
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
power-types-0.7.0 lib/power_types/patterns/observer/observable.rb
power-types-0.6.0 lib/power_types/patterns/observer/observable.rb
power-types-0.5.0 lib/power_types/patterns/observer/observable.rb
power-types-0.4.0 lib/power_types/patterns/observer/observable.rb
power-types-0.3.1 lib/power_types/patterns/observer/observable.rb
power-types-0.3.0 lib/power_types/patterns/observer/observable.rb
power-types-0.2.0 lib/power_types/patterns/observer/observable.rb