Class: HatiConfig::Schema::SchemaDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/hati_config/schema.rb

Overview

SchemaDefinition class handles schema validation and migration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ SchemaDefinition

Returns a new instance of SchemaDefinition.



53
54
55
56
57
58
59
# File 'lib/hati_config/schema.rb', line 53

def initialize(version)
  @version = version
  @required_fields = {}
  @optional_fields = {}
  @deprecated_fields = {}
  @migrations = {}
end

Instance Attribute Details

#deprecated_fields ⇒ Object (readonly)

Returns the value of attribute deprecated_fields.



51
52
53
# File 'lib/hati_config/schema.rb', line 51

def deprecated_fields
  @deprecated_fields
end

#migrations ⇒ Object (readonly)

Returns the value of attribute migrations.



51
52
53
# File 'lib/hati_config/schema.rb', line 51

def migrations
  @migrations
end

#optional_fields ⇒ Object (readonly)

Returns the value of attribute optional_fields.



51
52
53
# File 'lib/hati_config/schema.rb', line 51

def optional_fields
  @optional_fields
end

#required_fields ⇒ Object (readonly)

Returns the value of attribute required_fields.



51
52
53
# File 'lib/hati_config/schema.rb', line 51

def required_fields
  @required_fields
end

#version ⇒ Object (readonly)

Returns the value of attribute version.



51
52
53
# File 'lib/hati_config/schema.rb', line 51

def version
  @version
end

Instance Method Details

#add_migration(from_version, to_version = nil, block = nil, &implicit_block) ⇒ Object

Adds a migration between versions.

Parameters:

  • from_version (String) —

    The source version

  • to_version (String) (defaults to: nil) —

    The target version

  • block (Proc) (defaults to: nil) —

    The migration block

Raises:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hati_config/schema.rb', line 109

def add_migration(from_version, to_version = nil, block = nil, &implicit_block)
  if block.nil? && to_version.respond_to?(:call)
    # Handle the case where to_version is the block
    block = to_version
    if from_version.is_a?(Hash)
      from_version, to_version = from_version.first
    else
      from_version, to_version = from_version.to_s.gsub(/['"{}]/, '').split('=>').map(&:strip)
    end
  end

  migration_block = block || implicit_block
  raise MigrationError, 'Invalid migration format' unless from_version && to_version && migration_block

  key = migration_key(from_version, to_version)
  @migrations[key] = migration_block
end

#deprecated(name, since:, remove_in:) ⇒ Object

Marks a field as deprecated.

Parameters:

  • name (Symbol) —

    The field name

  • since (String) —

    The version since this field is deprecated

  • remove_in (String) —

    The version when this field will be removed



100
101
102
# File 'lib/hati_config/schema.rb', line 100

def deprecated(name, since:, remove_in:)
  @deprecated_fields[name] = { since: since, remove_in: remove_in }
end

#field(name, type:, required: true, default: nil, since: version) ⇒ Object

Defines a field in the schema.

Parameters:

  • name (Symbol) —

    The field name

  • type (Symbol, Class) —

    The field type

  • required (Boolean) (defaults to: true) —

    Whether the field is required

  • default (Object) (defaults to: nil) —

    The default value for optional fields

  • since (String) (defaults to: version) —

    The version since this field is available



68
69
70
71
72
73
74
# File 'lib/hati_config/schema.rb', line 68

def field(name, type:, required: true, default: nil, since: version)
  if required
    required(name, type: type, since: since)
  else
    optional(name, type: type, default: default, since: since)
  end
end

#migrate(data, from_version, to_version) ⇒ Hash

Migrates configuration data from one version to another.

Parameters:

  • data (Hash) —

    The configuration data to migrate

  • from_version (String) —

    The source version

  • to_version (String) —

    The target version

Returns:

  • (Hash) —

    The migrated configuration data

Raises:



155
156
157
158
159
160
161
162
163
# File 'lib/hati_config/schema.rb', line 155

def migrate(data, from_version, to_version)
  key = migration_key(from_version, to_version)
  migration = migrations[key]
  raise MigrationError, "No migration path from #{from_version} to #{to_version}" unless migration

  data = data.dup
  migration.call(data)
  data
end

#migration(versions, &block) ⇒ Object

Raises:



127
128
129
130
131
132
133
134
135
136
# File 'lib/hati_config/schema.rb', line 127

def migration(versions, &block)
  if versions.is_a?(Hash)
    from_version, to_version = versions.first
  else
    from_version, to_version = versions.to_s.gsub(/['"{}]/, '').split('=>').map(&:strip)
  end
  raise MigrationError, 'Invalid migration format' unless from_version && to_version

  add_migration(from_version, to_version, nil, &block)
end

#optional(name, type:, default: nil, since: version) ⇒ Object

Defines an optional field in the schema.

Parameters:

  • name (Symbol) —

    The field name

  • type (Symbol, Class) —

    The field type

  • default (Object) (defaults to: nil) —

    The default value

  • since (String) (defaults to: version) —

    The version since this field is available



91
92
93
# File 'lib/hati_config/schema.rb', line 91

def optional(name, type:, default: nil, since: version)
  @optional_fields[name] = { type: type, default: default, since: since }
end

#required(name, type:, since: version) ⇒ Object

Defines a required field in the schema.

Parameters:

  • name (Symbol) —

    The field name

  • type (Symbol, Class) —

    The field type

  • since (String) (defaults to: version) —

    The version since this field is required



81
82
83
# File 'lib/hati_config/schema.rb', line 81

def required(name, type:, since: version)
  @required_fields[name] = { type: type, since: since }
end

#validate(data, current_version = version) ⇒ Object

Validates configuration data against the schema.

Parameters:

  • data (Hash) —

    The configuration data to validate

  • current_version (String) (defaults to: version) —

    The current schema version

Raises:



143
144
145
146
147
# File 'lib/hati_config/schema.rb', line 143

def validate(data, current_version = version)
  validate_required_fields(data, current_version)
  validate_deprecated_fields(data, current_version)
  validate_types(data)
end