Class: MetaStates::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/meta_states/configuration.rb,
lib/meta_states/configuration/model_configuration.rb,
lib/meta_states/configuration/state_type_configuration.rb

Defined Under Namespace

Classes: ModelConfiguration, StateTypeConfiguration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



9
10
11
12
13
# File 'lib/meta_states/configuration.rb', line 9

def initialize
  @callbacks = {}
  @next_callback_id = 0
  @model_configurations = {}
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



7
8
9
# File 'lib/meta_states/configuration.rb', line 7

def callbacks
  @callbacks
end

#model_configurationsObject (readonly)

Returns the value of attribute model_configurations.



7
8
9
# File 'lib/meta_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/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

Returns:



55
56
57
# File 'lib/meta_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 MetaStates

Parameters:

  • model_class (Class)

    The model class to configure

Yields:

  • (model_config)

Returns:

Raises:

  • (ArgumentError)

    If the model class is not an ActiveRecord model



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

Returns:

  • (Integer)

    The limit for the state type



80
81
82
83
84
# File 'lib/meta_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

Parameters:

  • state (State)

    The state

Returns:

  • (Array)

    The matching callbacks



123
124
125
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

Returns:

  • (Hash)

    The metadata schema for the state type



90
91
92
93
94
# File 'lib/meta_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

Parameters:

  • callback_or_id (Symbol, Callback)

    The callback id or callback object

Returns:



112
113
114
115
116
117
118
# File 'lib/meta_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

Parameters:

  • state_type (String)

    The state type

  • id (Symbol) (defaults to: nil)

    The callback id

  • conditions (Hash)

    The conditions for the callback

  • block (Proc)

    The callback block

Returns:



102
103
104
105
106
107
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

Returns:

  • (Hash)

    The state types for the model



62
63
64
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

Returns:

  • (Array)

    The statuses for the state type



70
71
72
73
74
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

Returns:

  • (Boolean)

    True if the state type is valid, false otherwise



47
48
49
# File 'lib/meta_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

Parameters:

  • model_class (Class)

    The model class

  • state_type (String)

    The state type

  • status (String)

    The status

Returns:

  • (Boolean)

    True if the status is valid, false otherwise



37
38
39
40
41
# File 'lib/meta_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