Module: MotionFlux::Store::ClassMethods

Included in:
MotionFlux::Store
Defined in:
lib/motion_flux/store.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



32
33
34
# File 'lib/motion_flux/store.rb', line 32

def attributes
  @attributes ||= []
end

#emit(event) ⇒ Object



48
49
50
# File 'lib/motion_flux/store.rb', line 48

def emit event
  handlers[event].each(&:call)
end

#handlersObject



40
41
42
# File 'lib/motion_flux/store.rb', line 40

def handlers
  @handlers ||= Hash.new { |hash, key| hash[key] = [] }
end

#instanceObject



36
37
38
# File 'lib/motion_flux/store.rb', line 36

def instance
  @instance ||= new
end

#on(event, &proc) ⇒ Object



44
45
46
# File 'lib/motion_flux/store.rb', line 44

def on event, &proc
  handlers[event] << proc
end

#store_attribute(*attrs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/motion_flux/store.rb', line 16

def store_attribute *attrs
  options = attrs.extract_options!
  attrs.each do |attr|
    attributes << attr

    attr_reader attr
    define_singleton_method attr do
      instance.send attr
    end

    if options[:default]
      instance.instance_variable_set "@#{attr}", options[:default]
    end
  end
end

#subscribe(action) ⇒ Object



8
9
10
# File 'lib/motion_flux/store.rb', line 8

def subscribe action
  MotionFlux::Dispatcher.register instance, action
end

#wait_for(*stores) ⇒ Object



12
13
14
# File 'lib/motion_flux/store.rb', line 12

def wait_for *stores
  MotionFlux::Dispatcher.add_dependency instance, stores.map(&:instance)
end