Class: PRC::SectionsConfig

Inherits:
SectionConfig show all
Defined in:
lib/prc_section_config.rb

Overview

SectionsConfig class layer based on SectionConfig.

It supports a data_options :sections/default for #[] and #exist? etc…

The main difference with SectionConfig is :

  • :sections options is replacing :section for [] and exist?. search in collection of ordered sections. First found, first returned.

  • :section is still use like SectionConfig designed it.

  • :default is the default section to use. if not set, it will be :default.

Instance Attribute Summary

Attributes inherited from BaseConfig

#data, #filename, #latest_version, #version

Instance Method Summary collapse

Methods inherited from SectionConfig

#del

Methods included from PRC::SectionConfigRubySpec::Public

#[]=

Methods inherited from BaseConfig

#data_options, #del, #erase, #initialize, #latest_version?, #load, #rh_key_to_symbol, #rh_key_to_symbol?, #save, #to_s

Methods included from BaseConfigRubySpec::Private

#p_set

Methods included from BaseConfigRubySpec::Public

#[]=

Constructor Details

This class inherits a constructor from PRC::BaseConfig

Instance Method Details

#[](*keys) ⇒ Object

Get the value of a specific key under a section. You have to call #data_options(:section => ‘MySection’)

  • Args :

    • keys : keys to get values from a sections/section set by data_options:

      • :sections: if not set, it will search only in what is set in :default_section.

      • :default_section : default section name to use. by default is ‘:default’

  • Returns :

    • first found value or nil if not found.

  • Raises : Nothing



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/prc_section_config.rb', line 110

def [](*keys)
  return nil if keys.length == 0

  if @data_options[:default_section].nil?
    section = :default
  else
    section = @data_options[:default_section]
  end

  sections = @data_options[:sections]

  if sections.is_a?(Array)
    sections << section unless sections.include?(section)
  else
    sections = [section]
  end

  sections.each { |s| return p_get(s, *keys) if p_exist?(s, *keys) }

  nil
end

#exist?(*keys) ⇒ Boolean

Check key existence under a section. You have to call #data_options(:section => ‘MySection’)

  • Args :

    • keys : keys to get values from a section set by data_options:

      • :sections: if not set, it will search only in what is set in :default_section.

      • :default_section : default section name to use. by default is ‘:default’

  • Returns :

    • true if first found.

  • Raises : Nothing

  • hint :

    • If you want to know where to find a value, use where?

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/prc_section_config.rb', line 150

def exist?(*keys)
  return nil if keys.length == 0

  if @data_options[:default_section].nil?
    section = :default
  else
    section = @data_options[:default_section]
  end

  sections = @data_options[:sections]

  if sections.is_a?(Array)
    sections << section unless sections.include?(section)
  else
    sections = [section]
  end

  sections.each { |s| return true if p_exist?(s, *keys) }

  false
end

#where?(keys, name) ⇒ Boolean

where layer helper format Used by CoreConfig where?

In the context of CoreConfig, this class is a layer with a name. CoreConfig will query this function to get a layer name. If the layer needs to add any other data, this function will need to be redefined.

  • Args :

    • name : name of this layer managed by CoreConfig

  • Returns :

    • name: Composed layer name return by the layer to CoreConfig return ‘<name>(<sections found sep by |>)’

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/prc_section_config.rb', line 186

def where?(keys, name)
  return name unless exist?(*keys)

  if @data_options[:default_section].nil?
    section = :default
  else
    section = @data_options[:default_section]
  end

  sections = @data_options[:sections]

  if sections.is_a?(Array)
    sections << section unless sections.include?(section)
  else
    sections = [section]
  end

  sections_found = []
  sections.each { |s| sections_found << s if p_exist?(s, *keys) }

  format('%s(%s)', name, sections_found.join('|'))
end