Class: AppConfigLoader::ConfigEntry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/app_config_loader/config_entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_key, value, use_domain = false) ⇒ ConfigEntry

Constructor



10
11
12
13
14
# File 'lib/app_config_loader/config_entry.rb', line 10

def initialize(full_key, value, use_domain = false)
  @use_domain = use_domain
  @env, @domain, @key_components = parse_full_key full_key
  @value = value
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



5
6
7
# File 'lib/app_config_loader/config_entry.rb', line 5

def domain
  @domain
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/app_config_loader/config_entry.rb', line 5

def env
  @env
end

#key_componentsObject (readonly)

Returns the value of attribute key_components.



5
6
7
# File 'lib/app_config_loader/config_entry.rb', line 5

def key_components
  @key_components
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/app_config_loader/config_entry.rb', line 5

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/app_config_loader/config_entry.rb', line 71

def <=>(other)
  # first compare key size
  comp = self.key_components.count <=> other.key_components.count
  return comp if comp != 0

  # lexically compare the actual key components
  other_keys = other.key_components
  self.key_components.each_with_index do |key, index|
    comp = key <=> other_keys[index]
    return comp if comp != 0
  end

  # compare specificity
  comp = self.specificity <=> other.specificity
  return comp if comp != 0

  # compare env
  comp = wildcard_compare self.env, other.env
  return comp if comp != 0

  # compare domain
  wildcard_compare self.domain, other.domain
end

#==(other) ⇒ Object Also known as: eql?

Comparison



62
63
64
65
66
67
68
# File 'lib/app_config_loader/config_entry.rb', line 62

def ==(other)
  other.class == self.class &&
    other.env == self.env &&
    other.domain == self.domain &&
    other.key_components == self.key_components &&
    other.value == self.value
end

#applicable?(target_env, target_domain = nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/app_config_loader/config_entry.rb', line 44

def applicable?(target_env, target_domain = nil)
  # first matches enviroment
  return false if env != target_env && env != :any

  # then matches domain
  return false if target_domain && domain != :any && domain != target_domain

  true
end

#full_keyString

Config key including env and domain The domain portion is included if config.use_domain is true

Returns:

  • (String)

    config key including the env and domain



31
32
33
34
35
# File 'lib/app_config_loader/config_entry.rb', line 31

def full_key
  prefix = @env == :any ? '*.' : "#{@env}."
  prefix += @domain == :any ? '*.' : "#{@domain}." if @use_domain
  "#{prefix}#{self.key}"
end

#keyString

Config key without env and domain

Returns:

  • (String)

    config key without the env and domain



23
24
25
# File 'lib/app_config_loader/config_entry.rb', line 23

def key
  @key_components.join('.')
end

#specificityObject



37
38
39
40
41
42
# File 'lib/app_config_loader/config_entry.rb', line 37

def specificity
  score = 0
  score += 1  if @env != :any       # add one point if env is specified
  score += 10 if @domain != :any    # add 10 points if domain is specified
  score
end

#to_sObject



54
55
56
# File 'lib/app_config_loader/config_entry.rb', line 54

def to_s
  "#{self.full_key}: #{self.value}"
end