Class: UltraSettings::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/ultra_settings/field.rb

Overview

Definition for a field on a configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: :string, description: nil, default: nil, default_if: nil, env_var: nil, runtime_setting: nil, yaml_key: nil, static: false, secret: false) ⇒ Field

Returns a new instance of Field.

Parameters:

  • name (String, Symbol)

    The name of the field.

  • type (Symbol) (defaults to: :string)

    The type of the field.

  • description (String) (defaults to: nil)

    The description of the field.

  • default (Object) (defaults to: nil)

    The default value of the field.

  • default_if (Proc, Symbol) (defaults to: nil)

    A proc that returns true if the default value should be used.

  • env_var (String, Symbol) (defaults to: nil)

    The name of the environment variable to use for the field.

  • runtime_setting (String, Symbol) (defaults to: nil)

    The name of the setting to use for the field.

  • yaml_key (String, Symbol) (defaults to: nil)

    The name of the YAML key to use for the field.

  • static (Boolean) (defaults to: false)

    Whether or not the field is static and cannot be changed at runtime.

  • secret (Boolean, Proc) (defaults to: false)

    Whether or not the field contains a value that should be kept secret.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ultra_settings/field.rb', line 25

def initialize(
  name:,
  type: :string,
  description: nil,
  default: nil,
  default_if: nil,
  env_var: nil,
  runtime_setting: nil,
  yaml_key: nil,
  static: false,
  secret: false
)
  @name = name.to_s.freeze
  @type = type.to_sym
  @description = description&.to_s&.freeze
  @default = Coerce.coerce_value(default, @type).freeze
  @default_if = default_if
  @env_var = env_var&.to_s&.freeze
  @runtime_setting = runtime_setting&.to_s&.freeze
  @yaml_key = yaml_key&.to_s&.freeze
  @static = !!static
  @secret = (secret.respond_to?(:call) ? secret : !!secret)
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/ultra_settings/field.rb', line 9

def default
  @default
end

#default_ifObject (readonly)

Returns the value of attribute default_if.



10
11
12
# File 'lib/ultra_settings/field.rb', line 10

def default_if
  @default_if
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/ultra_settings/field.rb', line 8

def description
  @description
end

#env_varObject (readonly)

Returns the value of attribute env_var.



11
12
13
# File 'lib/ultra_settings/field.rb', line 11

def env_var
  @env_var
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ultra_settings/field.rb', line 6

def name
  @name
end

#runtime_settingObject (readonly)

Returns the value of attribute runtime_setting.



12
13
14
# File 'lib/ultra_settings/field.rb', line 12

def runtime_setting
  @runtime_setting
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/ultra_settings/field.rb', line 7

def type
  @type
end

#yaml_keyObject (readonly)

Returns the value of attribute yaml_key.



13
14
15
# File 'lib/ultra_settings/field.rb', line 13

def yaml_key
  @yaml_key
end

Instance Method Details

#coerce(value) ⇒ Object

Coerce the passed in value to the type of the field.

Parameters:

  • value (Object)

    The value to coerce.

Returns:

  • (Object)

    The coerced value.



72
73
74
# File 'lib/ultra_settings/field.rb', line 72

def coerce(value)
  Coerce.coerce_value(value, @type)
end

#secret?Boolean

Returns true if the field is marked as having a secret value.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/ultra_settings/field.rb', line 86

def secret?
  if @secret.respond_to?(:call)
    !!@secret.call
  else
    @secret
  end
end

#source(env: nil, settings: nil, yaml_config: nil) ⇒ Symbol?

Get the source for the field from the passed in state.

Parameters:

  • env (Hash) (defaults to: nil)

    The environment variables.

  • settings (Hash) (defaults to: nil)

    The runtime settings.

  • yaml_config (Hash) (defaults to: nil)

    The YAML configuration.

Returns:

  • (Symbol, nil)

    The source of the value (:env, :settings, or :yaml).



64
65
66
# File 'lib/ultra_settings/field.rb', line 64

def source(env: nil, settings: nil, yaml_config: nil)
  fetch_value_and_source(env: env, settings: settings, yaml_config: yaml_config).last
end

#static?Boolean

Returns true if the field is static.

Returns:

  • (Boolean)


79
80
81
# File 'lib/ultra_settings/field.rb', line 79

def static?
  @static
end

#value(env: nil, settings: nil, yaml_config: nil) ⇒ Object

Get the value for the field from the passed in state.

Parameters:

  • env (Hash, nil) (defaults to: nil)

    The environment variables.

  • settings (Hash, nil) (defaults to: nil)

    The runtime settings.

  • yaml_config (Hash, nil) (defaults to: nil)

    The YAML configuration.



54
55
56
# File 'lib/ultra_settings/field.rb', line 54

def value(env: nil, settings: nil, yaml_config: nil)
  fetch_value_and_source(env: env, settings: settings, yaml_config: yaml_config).first
end