lib/ably/modules/state_emitter.rb in ably-0.1.6 vs lib/ably/modules/state_emitter.rb in ably-0.2.0

- old
+ new

@@ -1,10 +1,13 @@ module Ably::Modules # StateEmitter module adds a set of generic state related methods to a class on the assumption that # the instance variable @state is used exclusively, the {Enum} STATE is defined prior to inclusion of this # module, and the class is an {EventEmitter}. It then emits state changes. # + # It also ensures the EventEmitter is configured to retrict permitted events to the + # the available STATEs and :error. + # # @example # class Connection # include Ably::Modules::EventEmitter # extend Ably::Modules::Enum # STATE = ruby_enum('STATE', @@ -52,10 +55,16 @@ end alias_method :change_state, :state= private def self.included(klass) - klass.configure_event_emitter coerce_into: Proc.new { |event| klass::STATE(event) } + klass.configure_event_emitter coerce_into: Proc.new { |event| + if event == :error + :error + else + klass::STATE(event) + end + } klass::STATE.each do |state_predicate| klass.instance_eval do define_method("#{state_predicate.to_sym}?") do state?(state_predicate)