Class: AppConfigLoader::ConfigEntry
- Inherits:
-
Object
- Object
- AppConfigLoader::ConfigEntry
- Includes:
- Comparable
- Defined in:
- lib/app_config_loader/config_entry.rb
Instance Attribute Summary collapse
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#key_components ⇒ Object
readonly
Returns the value of attribute key_components.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
(also: #eql?)
Comparison.
- #applicable?(target_env, target_domain = nil) ⇒ Boolean
-
#full_key ⇒ String
Config key including env and domain The domain portion is included if
config.use_domainistrue. -
#initialize(full_key, value, use_domain = false) ⇒ ConfigEntry
constructor
Constructor.
-
#key ⇒ String
Config key without env and domain.
- #specificity ⇒ Object
- #to_s ⇒ Object
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
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
5 6 7 |
# File 'lib/app_config_loader/config_entry.rb', line 5 def domain @domain end |
#env ⇒ Object (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_components ⇒ Object (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 |
#value ⇒ Object (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
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_key ⇒ String
Config key including env and domain The domain portion is included if config.use_domain is true
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 |
#key ⇒ String
Config key without env and domain
23 24 25 |
# File 'lib/app_config_loader/config_entry.rb', line 23 def key @key_components.join('.') end |
#specificity ⇒ Object
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_s ⇒ Object
54 55 56 |
# File 'lib/app_config_loader/config_entry.rb', line 54 def to_s "#{self.full_key}: #{self.value}" end |