Module: StateMachines::Machine::ClassMethods
- Included in:
- StateMachines::Machine
- Defined in:
- lib/state_machines/machine/class_methods.rb
Instance Attribute Summary collapse
-
#ignore_method_conflicts ⇒ Object
Default messages to use for validation errors in ORM integrations Thread-safe access via atomic operations on simple values.
- #renderer ⇒ Object
Instance Method Summary collapse
- #default_messages ⇒ Object
- #default_messages=(messages) ⇒ Object
- #draw ⇒ Object
-
#find_or_create(owner_class, *args) ⇒ Object
Attempts to find or create a state machine for the given class.
- #replace_messages(message_hash) ⇒ Object
Instance Attribute Details
#ignore_method_conflicts ⇒ Object
Default messages to use for validation errors in ORM integrations Thread-safe access via atomic operations on simple values
53 54 55 |
# File 'lib/state_machines/machine/class_methods.rb', line 53 def ignore_method_conflicts @ignore_method_conflicts end |
#renderer ⇒ Object
77 78 79 80 81 |
# File 'lib/state_machines/machine/class_methods.rb', line 77 def renderer return @renderer if @renderer STDIORenderer end |
Instance Method Details
#default_messages ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/state_machines/machine/class_methods.rb', line 55 def @default_messages ||= { invalid: 'is invalid', invalid_event: 'cannot transition when %s', invalid_transition: 'cannot transition via "%1$s"' }.freeze end |
#default_messages=(messages) ⇒ Object
63 64 65 66 |
# File 'lib/state_machines/machine/class_methods.rb', line 63 def () # Atomic replacement with frozen object @default_messages = deep_freeze_hash() end |
#draw ⇒ Object
47 48 49 |
# File 'lib/state_machines/machine/class_methods.rb', line 47 def draw(*) raise NotImplementedError end |
#find_or_create(owner_class, *args) ⇒ Object
Attempts to find or create a state machine for the given class. For example,
StateMachines::Machine.find_or_create(Vehicle)
StateMachines::Machine.find_or_create(Vehicle, :initial => :parked)
StateMachines::Machine.find_or_create(Vehicle, :status)
StateMachines::Machine.find_or_create(Vehicle, :status, :initial => :parked)
If a machine of the given name already exists in one of the class’s superclasses, then a copy of that machine will be created and stored in the new owner class (the original will remain unchanged).
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 |
# File 'lib/state_machines/machine/class_methods.rb', line 17 def find_or_create(owner_class, *args, &) = args.last.is_a?(Hash) ? args.pop : {} name = args.first || :state # Find an existing machine machine = (owner_class.respond_to?(:state_machines) && ((args.first && owner_class.state_machines[name]) || (!args.first && owner_class.state_machines.values.first))) || nil if machine # Only create a new copy if changes are being made to the machine in # a subclass if machine.owner_class != owner_class && (.any? || block_given?) machine = machine.clone machine.initial_state = [:initial] if .include?(:initial) machine.owner_class = owner_class # Configure async mode if requested in options machine.configure_async_mode!([:async]) if .include?(:async) end # Evaluate DSL machine.instance_eval(&) if block_given? else # No existing machine: create a new one machine = new(owner_class, name, , &) end machine end |
#replace_messages(message_hash) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/state_machines/machine/class_methods.rb', line 68 def () # Atomic replacement: read current messages, merge with new ones, replace atomically = @default_messages || {} = .merge() @default_messages = deep_freeze_hash() end |