Class: LittleGhost::Sandbox::EnvironmentPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/sandbox/environment_policy.rb

Overview

Declares whether a child inherits the host environment and which explicit values are added or replaced.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inherit: false, values: {}) ⇒ EnvironmentPolicy

Builds an environment policy with explicit String-compatible values.

Raises:



24
25
26
27
28
29
30
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 24

def initialize(inherit: false, values: {})
  raise PolicyError, "sandbox environment values must be a Hash" unless values.is_a?(Hash)

  @inherit = !!inherit
  @values = values.to_h { |key, value| [String(key).freeze, String(value).freeze] }.freeze
  freeze
end

Instance Attribute Details

#valuesObject (readonly)

Explicit child environment values.



33
34
35
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 33

def values
  @values
end

Class Method Details

.coerce(value) ⇒ Object

Returns value unchanged or builds a policy from a Hash.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 9

def self.coerce(value)
  return value if value.is_a?(self)
  raise PolicyError, "sandbox environment must be a Hash" unless value.is_a?(Hash)

  if value.key?(:set) || value.key?("set") || value.key?(:inherit) || value.key?("inherit")
    values = value[:set] || value["set"] || {}
    inherit = value.fetch(:inherit, value.fetch("inherit", false))
  else
    values = value
    inherit = false
  end
  new(inherit:, values:)
end

Instance Method Details

#inherit?Boolean

Indicates whether configured backends may inherit host values.

Returns:

  • (Boolean)


36
37
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 36

def inherit? = @inherit
# Returns the explicit child environment values.

#to_hObject

Returns the explicit child environment values.



38
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 38

def to_h = values