Class: ContextualConfig::ModuleConfig
- Inherits:
-
Object
- Object
- ContextualConfig::ModuleConfig
- Defined in:
- lib/contextual_config/module_registry.rb
Overview
Configuration for individual modules
Instance Attribute Summary collapse
-
#cache_enabled ⇒ Object
Returns the value of attribute cache_enabled.
-
#default_priority ⇒ Object
Returns the value of attribute default_priority.
-
#description ⇒ Object
Returns the value of attribute description.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#key_prefix ⇒ Object
Returns the value of attribute key_prefix.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#schema_file ⇒ Object
Returns the value of attribute schema_file.
-
#validation_enabled ⇒ Object
Returns the value of attribute validation_enabled.
Instance Method Summary collapse
-
#cache_enabled? ⇒ Boolean
Check if caching is enabled for this module.
-
#default_key(suffix = nil) ⇒ Object
Generate default key for this module.
-
#enabled? ⇒ Boolean
Check if module is enabled.
-
#initialize(name) ⇒ ModuleConfig
constructor
A new instance of ModuleConfig.
-
#load_schema ⇒ Object
Load schema from file if specified.
-
#to_h ⇒ Object
Convert to hash for serialization.
-
#validate! ⇒ Object
Validate module configuration.
-
#validation_enabled? ⇒ Boolean
Check if validation is enabled for this module.
Constructor Details
#initialize(name) ⇒ ModuleConfig
Returns a new instance of ModuleConfig.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/contextual_config/module_registry.rb', line 69 def initialize(name) @name = name.to_sym @model_class = nil @default_priority = ContextualConfig.configuration.default_priority @schema_file = nil @key_prefix = name.to_s @description = nil @enabled = true @cache_enabled = false @validation_enabled = true end |
Instance Attribute Details
#cache_enabled ⇒ Object
Returns the value of attribute cache_enabled.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def cache_enabled @cache_enabled end |
#default_priority ⇒ Object
Returns the value of attribute default_priority.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def default_priority @default_priority end |
#description ⇒ Object
Returns the value of attribute description.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def description @description end |
#enabled ⇒ Object
Returns the value of attribute enabled.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def enabled @enabled end |
#key_prefix ⇒ Object
Returns the value of attribute key_prefix.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def key_prefix @key_prefix end |
#model_class ⇒ Object
Returns the value of attribute model_class.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
67 68 69 |
# File 'lib/contextual_config/module_registry.rb', line 67 def name @name end |
#schema_file ⇒ Object
Returns the value of attribute schema_file.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def schema_file @schema_file end |
#validation_enabled ⇒ Object
Returns the value of attribute validation_enabled.
64 65 66 |
# File 'lib/contextual_config/module_registry.rb', line 64 def validation_enabled @validation_enabled end |
Instance Method Details
#cache_enabled? ⇒ Boolean
Check if caching is enabled for this module
92 93 94 |
# File 'lib/contextual_config/module_registry.rb', line 92 def cache_enabled? @cache_enabled && ContextualConfig.configuration.cache_enabled? end |
#default_key(suffix = nil) ⇒ Object
Generate default key for this module
106 107 108 109 110 |
# File 'lib/contextual_config/module_registry.rb', line 106 def default_key(suffix = nil) parts = [key_prefix] parts << suffix if suffix parts.join('.') end |
#enabled? ⇒ Boolean
Check if module is enabled
82 83 84 |
# File 'lib/contextual_config/module_registry.rb', line 82 def enabled? @enabled end |
#load_schema ⇒ Object
Load schema from file if specified
97 98 99 100 101 102 103 |
# File 'lib/contextual_config/module_registry.rb', line 97 def load_schema return nil unless schema_file && File.exist?(schema_file) JSON.parse(File.read(schema_file)) rescue JSON::ParserError => e raise(ConfigurationError, "Invalid JSON schema file for module #{name}: #{e.message}") end |
#to_h ⇒ Object
Convert to hash for serialization
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/contextual_config/module_registry.rb', line 130 def to_h { name: name, model_class: model_class&.name, default_priority: default_priority, schema_file: schema_file, key_prefix: key_prefix, description: description, enabled: enabled?, cache_enabled: cache_enabled?, validation_enabled: validation_enabled? } end |
#validate! ⇒ Object
Validate module configuration
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/contextual_config/module_registry.rb', line 113 def validate! if model_class && !model_class.is_a?(Class) raise(ConfigurationError, "model_class must be a Class for module #{name}") end if schema_file && !File.exist?(schema_file) raise(ConfigurationError, "Schema file does not exist for module #{name}: #{schema_file}") end if default_priority.negative? raise(ConfigurationError, "default_priority must be non-negative for module #{name}") end true end |
#validation_enabled? ⇒ Boolean
Check if validation is enabled for this module
87 88 89 |
# File 'lib/contextual_config/module_registry.rb', line 87 def validation_enabled? @validation_enabled end |