Class: Lutaml::Store::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_type: :memory, adapter_options: {}, cache: {}, monitoring: {}, events: {}, compression: {}, serialization: {}) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lutaml/store/config.rb', line 13

def initialize(adapter_type: :memory, adapter_options: {},
               cache: {}, monitoring: {}, events: {},
               compression: {}, serialization: {}, **)
  @adapter_type = normalize_adapter_type(adapter_type)
  @adapter_options = symbolize_keys(adapter_options)

  cache_config = symbolize_keys(cache)
  @cache_enabled = cache_config.fetch(:enabled, true)
  @cache_max_size = cache_config.fetch(:max_size, 1000)
  @cache_ttl = cache_config.fetch(:ttl, nil)

  monitoring_config = symbolize_keys(monitoring)
  @monitoring_enabled = monitoring_config.fetch(:enabled, false)

  events_config = symbolize_keys(events)
  @async_events = events_config.fetch(:async, false)

  compression_config = symbolize_keys(compression)
  @compression_enabled = compression_config.fetch(:enabled, false)
  @compression_algorithm = compression_config.fetch(:algorithm, "gzip")
  @compression_level = compression_config.fetch(:level, 6)

  serialization_config = symbolize_keys(serialization)
  @serialization_formats = serialization_config.fetch(:formats, %w[marshal hash json yaml xml toml])
  @validate_on_write = serialization_config.fetch(:validate_on_write, false)
end

Instance Attribute Details

#adapter_optionsObject (readonly)

Returns the value of attribute adapter_options.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def adapter_options
  @adapter_options
end

#adapter_typeObject (readonly)

Returns the value of attribute adapter_type.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def adapter_type
  @adapter_type
end

#async_eventsObject (readonly)

Returns the value of attribute async_events.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def async_events
  @async_events
end

#cache_enabledObject (readonly)

Returns the value of attribute cache_enabled.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def cache_enabled
  @cache_enabled
end

#cache_max_sizeObject (readonly)

Returns the value of attribute cache_max_size.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def cache_max_size
  @cache_max_size
end

#cache_ttlObject (readonly)

Returns the value of attribute cache_ttl.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def cache_ttl
  @cache_ttl
end

#compression_algorithmObject (readonly)

Returns the value of attribute compression_algorithm.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def compression_algorithm
  @compression_algorithm
end

#compression_enabledObject (readonly)

Returns the value of attribute compression_enabled.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def compression_enabled
  @compression_enabled
end

#compression_levelObject (readonly)

Returns the value of attribute compression_level.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def compression_level
  @compression_level
end

#monitoring_enabledObject (readonly)

Returns the value of attribute monitoring_enabled.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def monitoring_enabled
  @monitoring_enabled
end

#serialization_formatsObject (readonly)

Returns the value of attribute serialization_formats.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def serialization_formats
  @serialization_formats
end

#validate_on_writeObject (readonly)

Returns the value of attribute validate_on_write.



8
9
10
# File 'lib/lutaml/store/config.rb', line 8

def validate_on_write
  @validate_on_write
end

Class Method Details

.from_file(file_path) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/lutaml/store/config.rb', line 40

def self.from_file(file_path)
  config_data = YAML.load_file(file_path)
  lutaml_config = config_data["lutaml_store"] || config_data
  from_hash(lutaml_config)
rescue Errno::ENOENT
  raise ConfigurationError, "Configuration file not found: #{file_path}"
rescue Psych::SyntaxError => e
  raise ConfigurationError, "Invalid YAML in configuration file: #{e.message}"
end

.from_hash(hash) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lutaml/store/config.rb', line 97

def from_hash(hash)
  symbolized = symbolize_keys(hash)
  adapter_config = symbolized[:adapter] || {}
  new(
    adapter_type: adapter_config[:type],
    adapter_options: adapter_config[:options] || {},
    cache: symbolized[:cache] || {},
    monitoring: symbolized[:monitoring] || {},
    events: symbolized[:events] || {},
    compression: symbolized[:compression] || {},
    serialization: symbolized[:serialization] || {}
  )
end

.from_yaml(yaml_string) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/lutaml/store/config.rb', line 50

def self.from_yaml(yaml_string)
  config_data = YAML.safe_load(yaml_string)
  lutaml_config = config_data["lutaml_store"] || config_data
  from_hash(lutaml_config)
rescue Psych::SyntaxError => e
  raise ConfigurationError, "Invalid YAML configuration: #{e.message}"
end

.symbolize_keys(hash) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/lutaml/store/config.rb', line 113

def symbolize_keys(hash)
  return hash unless hash.is_a?(Hash)

  hash.each_with_object({}) do |(key, value), result|
    new_key = key.to_sym
    new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
    result[new_key] = new_value
  end
end

Instance Method Details

#async_events?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/lutaml/store/config.rb', line 66

def async_events?
  @async_events
end

#cache_enabled?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/lutaml/store/config.rb', line 58

def cache_enabled?
  @cache_enabled
end

#compression_enabled?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/lutaml/store/config.rb', line 70

def compression_enabled?
  @compression_enabled
end

#monitoring_enabled?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/lutaml/store/config.rb', line 62

def monitoring_enabled?
  @monitoring_enabled
end

#to_hObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/lutaml/store/config.rb', line 85

def to_h
  {
    adapter: { type: @adapter_type, options: @adapter_options },
    cache: { enabled: @cache_enabled, max_size: @cache_max_size, ttl: @cache_ttl },
    monitoring: { enabled: @monitoring_enabled },
    events: { async: @async_events },
    compression: { enabled: @compression_enabled, algorithm: @compression_algorithm, level: @compression_level },
    serialization: { formats: @serialization_formats, validate_on_write: @validate_on_write }
  }
end

#validate!Object



78
79
80
81
82
83
# File 'lib/lutaml/store/config.rb', line 78

def validate!
  validate_adapter_config
  validate_cache_config
  validate_monitoring_config
  validate_events_config
end

#validate_on_write?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/lutaml/store/config.rb', line 74

def validate_on_write?
  @validate_on_write
end