debian-mirror-gitlab/app/models/integrations/field.rb

61 lines
1.6 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# frozen_string_literal: true
module Integrations
class Field
2023-03-17 16:20:25 +05:30
BOOLEAN_ATTRIBUTES = %i[required api_only is_secret exposes_secrets].freeze
2022-08-13 15:12:31 +05:30
2022-05-07 20:08:51 +05:30
ATTRIBUTES = %i[
2022-08-13 15:12:31 +05:30
section type placeholder choices value checkbox_label
2022-05-07 20:08:51 +05:30
title help
non_empty_password_help
non_empty_password_title
2022-08-13 15:12:31 +05:30
].concat(BOOLEAN_ATTRIBUTES).freeze
TYPES = %w[text textarea password checkbox select].freeze
2022-05-07 20:08:51 +05:30
2022-07-23 23:45:48 +05:30
attr_reader :name, :integration_class
2022-05-07 20:08:51 +05:30
2023-06-20 00:43:36 +05:30
def initialize(name:, integration_class:, type: 'text', is_secret: false, api_only: false, **attributes)
2022-05-07 20:08:51 +05:30
@name = name.to_s.freeze
2022-07-23 23:45:48 +05:30
@integration_class = integration_class
2022-05-07 20:08:51 +05:30
2023-06-20 00:43:36 +05:30
attributes[:type] = is_secret ? 'password' : type
2022-05-07 20:08:51 +05:30
attributes[:api_only] = api_only
2023-03-17 16:20:25 +05:30
attributes[:is_secret] = is_secret
2022-05-07 20:08:51 +05:30
@attributes = attributes.freeze
2022-08-13 15:12:31 +05:30
invalid_attributes = attributes.keys - ATTRIBUTES
if invalid_attributes.present?
raise ArgumentError, "Invalid attributes #{invalid_attributes.inspect}"
elsif !TYPES.include?(self[:type])
raise ArgumentError, "Invalid type #{self[:type].inspect}"
end
2022-05-07 20:08:51 +05:30
end
def [](key)
return name if key == :name
value = @attributes[key]
2022-07-23 23:45:48 +05:30
return integration_class.class_exec(&value) if value.respond_to?(:call)
2022-05-07 20:08:51 +05:30
value
end
2022-06-21 17:19:12 +05:30
def secret?
2022-08-13 15:12:31 +05:30
self[:type] == 'password'
2022-05-07 20:08:51 +05:30
end
ATTRIBUTES.each do |name|
define_method(name) { self[name] }
end
2022-08-13 15:12:31 +05:30
BOOLEAN_ATTRIBUTES.each do |name|
define_method("#{name}?") { !!self[name] }
end
TYPES.each do |type|
define_method("#{type}?") { self[:type] == type }
end
2022-05-07 20:08:51 +05:30
end
end