Module: StateMachines::Integrations::ActiveRecord::MachineMethods
- Included in:
- StateMachines::Integrations::ActiveRecord
- Defined in:
- lib/state_machines/integrations/active_record.rb
Overview
Machine-specific methods for enum integration
Instance Attribute Summary collapse
-
#enum_integration ⇒ Object
Enum integration metadata storage.
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Hook called after machine initialization.
-
#detect_enum_integration ⇒ Object
Check if enum integration should be enabled for this machine.
-
#enum_integrated? ⇒ Boolean
Check if this machine has enum integration enabled.
-
#enum_mapping ⇒ Object
Get the enum mapping for this attribute.
-
#initialize_enum_integration ⇒ Object
Initialize enum integration if enum is detected.
- #integer_type_registered? ⇒ Boolean
-
#original_enum_methods ⇒ Object
Get list of original enum methods that were preserved.
-
#read(object, attr_sym, ivar = false) ⇒ Object
Machine internals (state matching, validations) call read() to get the current state value and compare it against state.value.
-
#state ⇒ Object
Override state method to trigger method generation after states are defined.
-
#state_machine_methods ⇒ Object
Get list of state machine methods that were generated.
Instance Attribute Details
#enum_integration ⇒ Object
Enum integration metadata storage
379 380 381 |
# File 'lib/state_machines/integrations/active_record.rb', line 379 def enum_integration @enum_integration end |
Instance Method Details
#after_initialize ⇒ Object
Hook called after machine initialization
382 383 384 385 386 |
# File 'lib/state_machines/integrations/active_record.rb', line 382 def after_initialize super initialize_enum_integration register_integer_type if integer_column? && !enum_integrated? end |
#detect_enum_integration ⇒ Object
Check if enum integration should be enabled for this machine
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/state_machines/integrations/active_record.rb', line 389 def detect_enum_integration return nil unless owner_class.defined_enums.key?(attribute.to_s) # For now, auto-detect enum and enable basic integration # Later we can add explicit configuration options { enabled: true, prefix: true, suffix: false, scopes: true, enum_values: owner_class.defined_enums[attribute.to_s] || {}, original_enum_methods: detect_existing_enum_methods, state_machine_methods: [] } end |
#enum_integrated? ⇒ Boolean
Check if this machine has enum integration enabled
425 426 427 |
# File 'lib/state_machines/integrations/active_record.rb', line 425 def enum_integrated? enum_integration && enum_integration[:enabled] end |
#enum_mapping ⇒ Object
Get the enum mapping for this attribute
430 431 432 433 434 |
# File 'lib/state_machines/integrations/active_record.rb', line 430 def enum_mapping return {} unless enum_integrated? enum_integration[:enum_values] || {} end |
#initialize_enum_integration ⇒ Object
Initialize enum integration if enum is detected
406 407 408 409 410 411 412 |
# File 'lib/state_machines/integrations/active_record.rb', line 406 def initialize_enum_integration detected_config = detect_enum_integration return unless detected_config # Store enum integration metadata self.enum_integration = detected_config end |
#integer_type_registered? ⇒ Boolean
450 451 452 |
# File 'lib/state_machines/integrations/active_record.rb', line 450 def integer_type_registered? !!@integer_type_registered end |
#original_enum_methods ⇒ Object
Get list of original enum methods that were preserved
437 438 439 440 441 |
# File 'lib/state_machines/integrations/active_record.rb', line 437 def original_enum_methods return [] unless enum_integrated? enum_integration[:original_enum_methods] || [] end |
#read(object, attr_sym, ivar = false) ⇒ Object
Machine internals (state matching, validations) call read() to get the current state value and compare it against state.value. Only override when states have explicit integer values (e.g. state :pending, value: 0). In that case the custom type returns a state name string but machine internals need the raw integer for value-based lookup.
For auto-indexed states (no explicit value), state.value is the string name so super() returns the right thing already.
462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/state_machines/integrations/active_record.rb', line 462 def read(object, attr_sym, ivar = false) return super unless integer_type_registered? && attr_sym == :state return super unless states_with_explicit_integer_values? raw = object.read_attribute_before_type_cast(attribute.to_s) if raw.is_a?(::String) || raw.is_a?(::Symbol) matched = states.detect { |s| s.name && s.name.to_s == raw.to_s } return matched ? matched.value : raw end raw end |
#state ⇒ Object
Override state method to trigger method generation after states are defined
415 416 417 418 419 420 421 422 |
# File 'lib/state_machines/integrations/active_record.rb', line 415 def state(*, &) result = super # Generate methods after each state addition if enum integration is enabled generate_state_machine_methods if enum_integrated? result end |
#state_machine_methods ⇒ Object
Get list of state machine methods that were generated
444 445 446 447 448 |
# File 'lib/state_machines/integrations/active_record.rb', line 444 def state_machine_methods return [] unless enum_integrated? enum_integration[:state_machine_methods] || [] end |