gitlab-cloud-connector 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17fe80dc098578ea54c50cf02e06e83bb516a4b18d6fbb649d9fd7d593e853c8
4
- data.tar.gz: 89cbb45796d3eaa078a36e084b872f7b820f6fbf85fb745490cb164c52a66447
3
+ metadata.gz: 4d3a9c6cb24eae104dda40308b879b159d1e1583da6c39292dc864b3f9306746
4
+ data.tar.gz: 474c1fa8efc2fb0480818d74ac587152aa3ab907a5c340e3d5de7524c95fb3e2
5
5
  SHA512:
6
- metadata.gz: 52f2b3ac0efb7d0aff0a360bf2272cdaf3151a8b912ce3d60503ac0cb2297f72809e879254751afeec188d7976c80b94f5f1fcd318792559f3460a7f32de8331
7
- data.tar.gz: a4f104be9c1c99486d2b44a5183aba2c71d2f398cb4454557e27684e2669ac7c95394c6abe4321fb3f2e928ed59617a917886431fdf87c1e136255270bef8fa5
6
+ metadata.gz: f502784866d6d67fdbccbf262591ccc14d0a96fd44c6e38ab0de5273e224ce85258e0e504ded2d90f8d00b6f7a48662afae650a3dc8afe6de39ad8adcba37b26
7
+ data.tar.gz: ce85d65412bf2722bc68c5dcc2c8f16fa5da8c80147abef96c606dce03367aaaf053396671cf88281530aba9cce406440ef6b4ce11ce35a0379e3966c989edd9
data/README.md CHANGED
@@ -10,8 +10,6 @@ We expect Bundler is used to manage dependencies. To add the dependency, add it
10
10
  gem "gitlab-cloud-connector", "~> 1.0.0", require: 'gitlab/cloud_connector'
11
11
  ```
12
12
 
13
- ## Usage
14
-
15
13
  ### Data Storage
16
14
 
17
15
  Cloud Connector uses YAML files stored in the project's `/config` directory. The data is organized by model type:
@@ -1,21 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'data_model/yaml_data_loader'
4
+ require_relative 'data_model/abstract_data_loader'
5
+
3
6
  module Gitlab
4
7
  module CloudConnector
5
8
  module Configuration
9
+ DEFAULT_DATA_LOADER_CLASS = Gitlab::CloudConnector::DataModel::YamlDataLoader
10
+ INVALID_LOADER_MESSAGE = "Data loader must inherit from Gitlab::CloudConnector::DataModel::AbstractDataLoader"
11
+
12
+ InvalidDataLoaderError = Class.new(StandardError)
13
+
6
14
  class << self
7
- attr_writer :config_dir
15
+ attr_writer :config_dir, :memoize_data
8
16
 
9
17
  def config_dir
10
18
  @config_dir ||= File.expand_path("../../../config", __dir__).freeze
11
19
  end
12
20
 
21
+ def data_loader_class
22
+ @data_loader_class ||= DEFAULT_DATA_LOADER_CLASS
23
+ end
24
+
25
+ def data_loader_class=(klass)
26
+ validate_data_loader!(klass)
27
+ @data_loader_class = klass
28
+ end
29
+
30
+ def memoize_data?
31
+ return @memoize_data unless @memoize_data.nil?
32
+
33
+ (data_loader_class == DEFAULT_DATA_LOADER_CLASS)
34
+ end
35
+
13
36
  def configure
14
37
  yield self if block_given?
15
38
  end
16
39
 
17
- def reset!
18
- @config_dir = nil
40
+ private
41
+
42
+ def validate_data_loader!(klass)
43
+ return if klass && klass < Gitlab::CloudConnector::DataModel::AbstractDataLoader
44
+
45
+ raise InvalidDataLoaderError, INVALID_LOADER_MESSAGE
19
46
  end
20
47
  end
21
48
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ module CloudConnector
5
+ module DataModel
6
+ class AbstractDataLoader
7
+ def initialize(model_class)
8
+ @model_class = model_class
9
+ end
10
+
11
+ def load!
12
+ raise NotImplementedError, "#{self.class} must implement #load!"
13
+ end
14
+
15
+ protected
16
+
17
+ attr_reader :model_class
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'associations'
4
- require_relative 'data_loader'
5
4
 
6
5
  module Gitlab
7
6
  module CloudConnector
@@ -24,13 +23,17 @@ module Gitlab
24
23
  end
25
24
 
26
25
  def all
27
- @all ||= data_loader.load!
26
+ if Configuration.memoize_data?
27
+ @all ||= data_loader.load!
28
+ else
29
+ data_loader.load!
30
+ end
28
31
  end
29
32
 
30
33
  private
31
34
 
32
35
  def data_loader
33
- @data_loader ||= DataLoader.new(self)
36
+ Configuration.data_loader_class.new(self)
34
37
  end
35
38
  end
36
39
 
@@ -43,6 +46,13 @@ module Gitlab
43
46
  def [](name)
44
47
  instance_variable_get(:"@#{name}")
45
48
  end
49
+
50
+ def to_hash
51
+ instance_variables.each_with_object({}) do |var, hash|
52
+ key = var.to_s.delete('@').to_sym
53
+ hash[key] = instance_variable_get(var)
54
+ end
55
+ end
46
56
  end
47
57
  end
48
58
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require_relative 'abstract_data_loader'
5
+
6
+ module Gitlab
7
+ module CloudConnector
8
+ module DataModel
9
+ class YamlDataLoader < AbstractDataLoader
10
+ def load!
11
+ Dir.glob(data_files_path).map { |file| load_model_from_file(file) }
12
+ end
13
+
14
+ private
15
+
16
+ def data_files_path
17
+ File.join(CloudConnector::Configuration.config_dir, model_class.model_name.tableize, '*.yml')
18
+ end
19
+
20
+ def load_model_from_file(file_path)
21
+ data = YAML.safe_load(
22
+ File.read(file_path),
23
+ permitted_classes: [Time],
24
+ symbolize_names: true
25
+ ).deep_symbolize_keys!
26
+
27
+ model_class.new(**data)
28
+ rescue StandardError => e
29
+ raise "Error loading file #{file_path}: #{e.message}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  module CloudConnector
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-cloud-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikola Milojevic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-20 00:00:00.000000000 Z
11
+ date: 2025-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -163,9 +163,11 @@ files:
163
163
  - lib/gitlab/cloud_connector.rb
164
164
  - lib/gitlab/cloud_connector/available_services_generator.rb
165
165
  - lib/gitlab/cloud_connector/configuration.rb
166
+ - lib/gitlab/cloud_connector/data_model/abstract_data_loader.rb
166
167
  - lib/gitlab/cloud_connector/data_model/associations.rb
167
168
  - lib/gitlab/cloud_connector/data_model/base.rb
168
169
  - lib/gitlab/cloud_connector/data_model/data_loader.rb
170
+ - lib/gitlab/cloud_connector/data_model/yaml_data_loader.rb
169
171
  - lib/gitlab/cloud_connector/data_models.rb
170
172
  - lib/gitlab/cloud_connector/json_web_token.rb
171
173
  - lib/gitlab/cloud_connector/version.rb