Module: Stator::Model

Defined in:
lib/stator/model.rb

Defined Under Namespace

Modules: InstanceMethods, TrackerMethods

Instance Method Summary collapse

Instance Method Details

#_stator(namespace) ⇒ Object



30
31
32
# File 'lib/stator/model.rb', line 30

def _stator(namespace)
  self._stators[namespace.to_s]
end

#stator(options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stator/model.rb', line 4

def stator(options = {}, &block)

  class_attribute :_stators unless respond_to?(:_stators)

  include InstanceMethods   unless self.included_modules.include?(InstanceMethods)
  include TrackerMethods    if options[:track] == true

  self._stators ||= {}

  unless self.abstract_class?
    f = options[:field] || :state
    # rescue nil since the table may not exist yet.
    initial = self.columns_hash[f.to_s].default rescue nil
    options = options.merge(initial: initial) if initial
  end

  machine = (self._stators[options[:namespace].to_s] ||= ::Stator::Machine.new(self, options))

  if block_given?
    machine.instance_eval(&block)
    machine.evaluate
  end

  machine
end