Class: Integrations::Field

Inherits:
Object
  • Object
show all
Defined in:
app/models/integrations/field.rb

Constant Summary collapse

BOOLEAN_ATTRIBUTES =
i[required api_only is_secret exposes_secrets].freeze
ATTRIBUTES =
i[
  section type placeholder choices value checkbox_label
  title help if description
  label_description
  non_empty_password_help
  non_empty_password_title
].concat(BOOLEAN_ATTRIBUTES).freeze
TYPES =
i[text textarea password checkbox number string_array select].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, integration_class:, type: :text, is_secret: false, api_only: false, **attributes) ⇒ Field

Returns a new instance of Field.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/integrations/field.rb', line 21

def initialize(name:, integration_class:, type: :text, is_secret: false, api_only: false, **attributes)
  @name = name.to_s.freeze
  @integration_class = integration_class

  attributes[:type] = is_secret ? :password : type
  attributes[:api_only] = api_only
  attributes[:if] = attributes.fetch(:if, true)
  attributes[:is_secret] = is_secret
  attributes[:description] ||= attributes[:help]
  @attributes = attributes.freeze

  invalid_attributes = attributes.keys - ATTRIBUTES
  if invalid_attributes.present?
    raise ArgumentError, "Invalid attributes #{invalid_attributes.inspect}"
  elsif TYPES.exclude?(self[:type])
    raise ArgumentError, "Invalid type #{self[:type].inspect}"
  end
end

Instance Attribute Details

#integration_classObject (readonly)

Returns the value of attribute integration_class.



17
18
19
# File 'app/models/integrations/field.rb', line 17

def integration_class
  @integration_class
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'app/models/integrations/field.rb', line 17

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
43
44
45
46
47
# File 'app/models/integrations/field.rb', line 40

def [](key)
  return name if key == :name

  value = attributes[key]
  return integration_class.class_exec(&value) if value.respond_to?(:call)

  value
end

#api_typeObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/integrations/field.rb', line 65

def api_type
  case type
  when :checkbox
    ::API::Integrations::Boolean
  when :number
    Integer
  when :string_array
    Array[String]
  else
    String
  end
end

#secret?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/integrations/field.rb', line 49

def secret?
  self[:type] == :password
end