Module: Omega::Client::TrackState::ClassMethods

Defined in:
lib/omega/client/mixins/track_state.rb

Overview

Methods that are defined on the class including the TrackState module

Instance Method Summary collapse

Instance Method Details

#server_state(state, args = {}) ⇒ Object

Define a state for this entity type which clients can register on/off handlers for specific entity instances.

This method registers a callback to be invoked on entity initialization, integrating the local entity w/ the TrackState module after which an entity updated event handler is registered to detect changes in state

All callbacks registered here are invoked in the context of the entity w/ self as the only param

Options Hash (args):

  • :check (Callable)

    callback to invoke to check if the entity is in the specified state, should return true/false

  • :on (Callable)

    callback to invoke when entity enters the specified state

  • :off (Callable)

    callback to invoke when entity leaves the specified state


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/omega/client/mixins/track_state.rb', line 113

def server_state(state, args = {})
  entity_init { |e|
    @states              ||= []
    @on_state_callbacks  ||= {}
    @off_state_callbacks ||= {}

    if args.has_key?(:on)
      on_state(state, &args[:on])
    end

    if args.has_key?(:off)
      off_state(state, &args[:off])
    end

    @condition_checks ||= {}
    if args.has_key?(:check)
      @condition_checks[state] = args[:check]
    end

    e.handle(:all){
      #return if \@updating_state
      #\@updating_state = true
      @condition_checks.each { |st,check|
        if instance_exec(e, &check)
          e.set_state st
        else
          e.unset_state st
        end
      }
      #\@updating_state = false

    # only need to register state check
    # event handler once as it will check all states
    } unless @checking_state
    @checking_state = true
  }
end