Class: ENV

Inherits:
Object show all
Defined in:
lib/ruuuby/virtual/env.rb

Overview

modify the Ruby-‘singleton’ ENV

Class Method Summary collapse

Class Method Details

.fetch🔑(*env_key_then_opts) ⇒ String, *

Returns the value found or (w/ 2 args) the default value provided.

Parameters:

  • env_key_then_opts (*)

Returns:

  • (String, *)

    the value found or (w/ 2 args) the default value provided

Raises:

  • (RuntimeError)

    when provided 1 arg and the key DNE

  • (ArgumentError)

    when a non-string type is provided for the first argument

  • (ArgumentError)

    when no arguments, or too many were provided



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruuuby/virtual/env.rb', line 59

def fetch🔑(*env_key_then_opts)
  case (env_key_then_opts.𝔠)
  when 1
    the_key = env_key_then_opts[0]
    🛑str❓(:the_key, the_key)
    if self.∃?(the_key)
      return self[the_key]
    else
      🛑 RuntimeError.new("c{ENV_VARS}-> m{fetch🔑} did not have the ENV_VAR{#{the_key}}")
    end
  when 2
    the_key = env_key_then_opts[0]
    🛑str❓(:the_key, the_key)
    if self.∃?(the_key)
      return self[the_key]
    else
      return env_key_then_opts[1]
    end
  else
    🛑 ArgumentError.new("c{ENV_VARS}-> m{fetch🔑} got an invalid number of arguments{#{env_key_then_opts.𝔠.to_s}}")
  end
end

.parse_feature_behaviors(feature_uid, max_uid, min_allowed = -1,, max_allowed = -1)) ⇒ Array

Parameters:

  • feature_uid (String)

    | the name of the ENV_VAR

  • max_uid (Integer)
  • min_allowed (Integer) (defaults to: -1,)
  • max_allowed (Integer) (defaults to: -1))

Returns:

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruuuby/virtual/env.rb', line 90

def parse_feature_behaviors(feature_uid, max_uid, min_allowed=-1, max_allowed=-1)
  🛑str❓(:feature_uid, feature_uid)
  if self.∃?(feature_uid)
    content = ENV[feature_uid]
    if content.∋?('|')
      raw_nodes = content.split('|')
      nodes     = []
      raw_nodes.each do |n|
        parsed_node = self.validate_feature_behavior_syntax!(n)
        if nodes.∋?(parsed_node)
          🛑 ArgumentError.new("c{ENV}-> m{parse_feature_behaviors} got duplicate val{#{parsed_node.to_s}} for ENV{#{content.to_s}}")
        else
          as_num = parsed_node.to_str
          if as_num.∋?('{')
            as_num = as_num.♻️⟵('{')
          end
          as_num = as_num.♻️⟶('b').to_i
          if as_num > max_uid
            🛑 ArgumentError.new("c{ENV}-> m{parse_feature_behaviors} got out-of-bounds val{#{as_num.to_s}} for ENV{#{content.to_s}}")
          else
            nodes << parsed_node
            if max_allowed != -1 && max_allowed < nodes.length
              🛑 ArgumentError.new("c{ENV}-> m{parse_feature_behaviors} received more than the max{#{max_allowed.to_s}} number of allowed behaviors for feature{#{feature_uid.to_s}} in ENV{#{content.to_s}}")
            end
          end
        end
      end
      if min_allowed != -1 && nodes.length < min_allowed
        🛑 ArgumentError.new("c{ENV}-> m{parse_feature_behaviors} did not receive at least{#{min_allowed.to_s}} behaviors for feature{#{feature_uid.to_s}} in ENV{#{content.to_s}}")
      end
      return nodes
    else
      return [self.validate_feature_behavior_syntax!(content)]
    end
  else
    🛑 ArgumentError.new("c{ENV}-> m{parse_feature_behaviors} did not find ENV{#{feature_uid.to_s}}")
  end
end

.validate_feature_behavior_syntax!(content) ⇒ String

Returns the same param(content) provided.

Parameters:

Returns:

  • (String)

    the same param(content) provided

Raises:

  • (ArgumentError)

    if the provided param(content) does not have the syntax of a feature behavior



134
135
136
137
138
139
140
141
# File 'lib/ruuuby/virtual/env.rb', line 134

def validate_feature_behavior_syntax!(content)
  🛑str❓(:content, content)
  if ::Ruuuby::MetaData::RuuubyEngine.syntax_feature_behavior.match?(content)
    content
  else
    🛑 ArgumentError.new("c{ENV}-> m{validate_feature_behavior_syntax!} got invalid content for ENV{#{content.to_s}}")
  end
end

.∀🔑∃_value?(keys_to_find, expected_value) ⇒ Boolean

TODO: MISSING TDD!

‘does each provided key exist w/ the the same provided value?`

Parameters:

  • keys_to_find (Array)
  • expected_value (*)

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruuuby/virtual/env.rb', line 19

def ∀🔑∃_value?(keys_to_find, expected_value)
  matched_keys = 0
  num_to_find   = keys_to_find.length
  keys_to_find. do |key|
    if ::ENV.∃?(key)
      if ::ENV[key] == expected_value
        if matched_keys + 1 == num_to_find
          return true
        else
          matched_keys += 1
        end
      end
    else
      return false
    end
  end
  if matched_keys == num_to_find
    true
  else
    🛑 ::RuntimeError.new("| {ENV}-> m{∀🔑∃_value?} called w/ keys_to_find as{#{keys_to_find.to_s}} and expected_value as {#{expected_value.to_s}} which did not match the result length of{#{matched_keys.to_s}} |")
  end
end

.∃?(the_key) ⇒ Boolean

Returns true, if there exists an ENV_VAR w/ matching name.

Parameters:

Returns:

  • (Boolean)

    true, if there exists an ENV_VAR w/ matching name

Raises:

  • (ArgumentError)

    if the provided arg(the_key) is not of type String



47
48
49
50
# File 'lib/ruuuby/virtual/env.rb', line 47

def ∃?(the_key)
  🛑str❓(:the_key, the_key)
  self.has_key?(the_key)
end