Class: HasStates::Configuration
- Inherits:
-
Object
- Object
- HasStates::Configuration
- Includes:
- Singleton
- Defined in:
- lib/has_states/configuration.rb,
lib/has_states/configuration/model_configuration.rb,
lib/has_states/configuration/state_type_configuration.rb
Defined Under Namespace
Classes: ModelConfiguration, StateTypeConfiguration
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#model_configurations ⇒ Object
readonly
Returns the value of attribute model_configurations.
Instance Method Summary collapse
-
#clear_callbacks! ⇒ void
Clear all callbacks.
-
#config_for(model_class, state_type) ⇒ Configuration::StateTypeConfiguration
Get the configuration for a given state type.
-
#configure_model(model_class) {|model_config| ... } ⇒ Configuration::ModelConfiguration
Configure a model to use HasStates.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#limit_for(model_class, state_type) ⇒ Integer
Get the limit for a given state type.
-
#matching_callbacks(state) ⇒ Array
Get the callbacks that match a given state.
-
#metadata_schema_for(model_class, state_type) ⇒ Hash
Get the metadata schema for a given state type.
-
#off(callback_or_id) ⇒ Callback
Remove a callback by id or callback object.
-
#on(state_type, id: nil, **conditions, &block) ⇒ Callback
Register a callback for a given state type.
-
#state_types_for(model_class) ⇒ Hash
Get the state types for a given model.
-
#statuses_for(model_class, state_type) ⇒ Array
Get the statuses for a given state type.
-
#valid_state_type?(model_class, state_type) ⇒ Boolean
Check if a state type is valid for a given model.
-
#valid_status?(model_class, state_type, status) ⇒ Boolean
Check if a status is valid for a given state type.
Constructor Details
#initialize ⇒ Configuration
9 10 11 12 13 |
# File 'lib/has_states/configuration.rb', line 9 def initialize @callbacks = {} @next_callback_id = 0 @model_configurations = {} end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
7 8 9 |
# File 'lib/has_states/configuration.rb', line 7 def callbacks @callbacks end |
#model_configurations ⇒ Object (readonly)
Returns the value of attribute model_configurations.
7 8 9 |
# File 'lib/has_states/configuration.rb', line 7 def model_configurations @model_configurations end |
Instance Method Details
#clear_callbacks! ⇒ void
This method returns an undefined value.
Clear all callbacks
129 130 131 |
# File 'lib/has_states/configuration.rb', line 129 def clear_callbacks! @callbacks = {} end |
#config_for(model_class, state_type) ⇒ Configuration::StateTypeConfiguration
Get the configuration for a given state type
55 56 57 |
# File 'lib/has_states/configuration.rb', line 55 def config_for(model_class, state_type) @model_configurations[model_class]&.state_types&.dig(state_type.to_s) end |
#configure_model(model_class) {|model_config| ... } ⇒ Configuration::ModelConfiguration
Configure a model to use HasStates
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/has_states/configuration.rb', line 19 def configure_model(model_class) unless model_class.is_a?(Class) && model_class < ActiveRecord::Base raise ArgumentError, "#{model_class} must be an ActiveRecord model" end model_config = Configuration::ModelConfiguration.new(model_class) yield(model_config) @model_configurations[model_class] = model_config model_class.include(Stateable) unless model_class.included_modules.include?(Stateable) end |
#limit_for(model_class, state_type) ⇒ Integer
Get the limit for a given state type
80 81 82 83 84 |
# File 'lib/has_states/configuration.rb', line 80 def limit_for(model_class, state_type) return nil unless (config = config_for(model_class, state_type)) config.limit end |
#matching_callbacks(state) ⇒ Array
Get the callbacks that match a given state
123 124 125 |
# File 'lib/has_states/configuration.rb', line 123 def matching_callbacks(state) @callbacks.values.select { |callback| callback.matches?(state) } end |
#metadata_schema_for(model_class, state_type) ⇒ Hash
Get the metadata schema for a given state type
90 91 92 93 94 |
# File 'lib/has_states/configuration.rb', line 90 def (model_class, state_type) return nil unless (config = config_for(model_class, state_type)) config. end |
#off(callback_or_id) ⇒ Callback
Remove a callback by id or callback object
112 113 114 115 116 117 118 |
# File 'lib/has_states/configuration.rb', line 112 def off(callback_or_id) if callback_or_id.is_a?(Callback) @callbacks.delete_if { |_, cb| cb == callback_or_id } else @callbacks.delete(callback_or_id) end end |
#on(state_type, id: nil, **conditions, &block) ⇒ Callback
Register a callback for a given state type
102 103 104 105 106 107 |
# File 'lib/has_states/configuration.rb', line 102 def on(state_type, id: nil, **conditions, &block) callback = Callback.new(state_type, conditions, block) callback_id = id&.to_sym || generate_callback_id @callbacks[callback_id] = callback callback end |
#state_types_for(model_class) ⇒ Hash
Get the state types for a given model
62 63 64 |
# File 'lib/has_states/configuration.rb', line 62 def state_types_for(model_class) @model_configurations[model_class]&.state_types end |
#statuses_for(model_class, state_type) ⇒ Array
Get the statuses for a given state type
70 71 72 73 74 |
# File 'lib/has_states/configuration.rb', line 70 def statuses_for(model_class, state_type) return nil unless (config = config_for(model_class, state_type)) config.statuses end |
#valid_state_type?(model_class, state_type) ⇒ Boolean
Check if a state type is valid for a given model
47 48 49 |
# File 'lib/has_states/configuration.rb', line 47 def valid_state_type?(model_class, state_type) @model_configurations[model_class]&.state_types&.key?(state_type.to_s) end |
#valid_status?(model_class, state_type, status) ⇒ Boolean
Check if a status is valid for a given state type
37 38 39 40 41 |
# File 'lib/has_states/configuration.rb', line 37 def valid_status?(model_class, state_type, status) return false unless @model_configurations[model_class]&.state_types&.dig(state_type.to_s) @model_configurations[model_class].state_types[state_type.to_s].statuses.include?(status) end |