Module: StateManager::Resource

Defined in:
lib/state_manager/resource.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/state_manager/resource.rb', line 4

def self.extended(base)
  base.instance_eval do
    class_attribute :state_managers
    self.state_managers = {}

    attr_accessor :state_managers
  end

  base.send :include, InstanceMethods
end

Instance Method Details

#state_manager(property = :state, klass = nil, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/state_manager/resource.rb', line 15

def state_manager(property=:state, klass=nil, options={}, &block)
  default_options = {:helpers => true}
  options = default_options.merge(options)

  klass ||= begin
    "#{self.name}States".constantize
  rescue NameError
    nil
  end
  klass ||= StateManager::Base

  # Create a subclass of the specified state manager and mixin an adapter
  # if a matching one is found
  this = self
  adapter = Adapters.match(self)
  resource_name = self.name.demodulize.underscore
  
  klass = Class.new(klass) do
    state_property property
    resource_class this
    resource_name resource_name
    include adapter.const_get('ManagerMethods') if adapter
    class_eval &block if block_given?
  end
  include adapter.const_get('ResourceMethods') if adapter

  # Callbacks
  state_manager_added(property, klass, options) if respond_to? :state_manager_added
  klass.added_to_resource(self, property, options)

  # Define the subclass as a constant. We do this for multiple reasons, one
  # of which is to allow it to be serialized to YAML for delayed_job
  const_name = "#{property.to_s.camelize}States"
  remove_const const_name if const_defined?(const_name, false)
  const_set(const_name, klass)

  # Create an accessor for the state manager on this resource
  state_managers[property] = klass
  property_name = "#{property.to_s}_manager"
  define_method property_name do
    state_manager = state_managers[property]
    unless state_manager
      state_manager = klass.new(self)
      state_managers[property] = state_manager
    end
    state_manager
  end

  # Define the helper methods on the resource
  Helpers::Methods.define_methods(klass.specification, self, property) if options[:helpers]
end