config_layers 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09de39566647f0ec53aeb3241730de770fbe6991
4
- data.tar.gz: b72fa67b9d02647fac54bd0cc21dafbc8161d511
3
+ metadata.gz: 48fd7acf1ba746b4fb4d983923e87661914a7421
4
+ data.tar.gz: 0f933a73f34428bde745d4159ee87bfc6ce44849
5
5
  SHA512:
6
- metadata.gz: a4e778e5a67f2674a065927caea04b9c48006168065da28bfc8d421a8d38c49c99f31d9337966a53ae9db3fa56458fca570f87267b5cd7547b3f2010172f9bf3
7
- data.tar.gz: 8cdd444889903b37247d4d0127e56736846d10a6b78124637b14e0766b147d29ccc6879a0c3b01e66aa30f74f3dc836bc4e5e796b8e2259f5af52f684e03cc0a
6
+ metadata.gz: 9ada986d18ef74c9cf20b21c52dd98af2f17904de639da863a546b08e0a9a653be4b2612d3b132a9da307d41683d0c3de02bf69cded2b0b8dc1ac49741af902a
7
+ data.tar.gz: c199627b9bb5ce2d7fb378aae4c586d2d0f23b2d46b8e6bd460379285c04b6001179d9794a700e692d879775ec36a24b09affd42f85486d96ca2c61114e3c567
data/.rubocop.yml CHANGED
@@ -31,3 +31,8 @@ Metrics/ClassLength:
31
31
  # allow arguments to be longer than 15
32
32
  Metrics/AbcSize:
33
33
  Max: 40
34
+
35
+ Metrics/CyclomaticComplexity:
36
+ Max: 7
37
+ Metrics/PerceivedComplexity:
38
+ Max: 8
@@ -1,5 +1,5 @@
1
1
  # Config Layers
2
2
  module ConfigLayers
3
- VERSION = '0.1.2'
4
- DATE = '2015-05-11'
3
+ VERSION = '0.1.3'
4
+ DATE = '2015-05-20'
5
5
  end
@@ -203,6 +203,24 @@ module PRC
203
203
  p_save(filename)
204
204
  end
205
205
 
206
+ # where layer helper format Used by CoreConfig where?
207
+ #
208
+ # In the context of CoreConfig, this class is a layer with a name.
209
+ # CoreConfig will query this function to get a layer name.
210
+ # If the layer needs to add any other data, this function will need to
211
+ # be redefined.
212
+ #
213
+ # * *Args* :
214
+ # - name : name of this layer managed by CoreConfig
215
+ #
216
+ # * *Returns* :
217
+ # - name: Composed layer name return by the layer to CoreConfig
218
+ # It returns simply name.
219
+ #
220
+ def where?(_keys, name)
221
+ name
222
+ end
223
+
206
224
  # transform keys from string to symbol until deep level. Default is 1.
207
225
  #
208
226
  # * *Args* :
@@ -433,7 +433,10 @@ module PRC
433
433
  data_options.merge!(data_opts[index]) if data_opts[index].is_a?(Hash)
434
434
 
435
435
  config.data_options(data_options)
436
- layer_indexes << config_layers[index][:name] if config.exist?(keys)
436
+
437
+ name = config.where?(keys, config_layers[index][:name])
438
+
439
+ layer_indexes << name if config.exist?(keys)
437
440
  end
438
441
  return layer_indexes if layer_indexes.length > 0
439
442
  false
@@ -790,21 +793,29 @@ module PRC
790
793
  # layer_indexes function
791
794
  #
792
795
  # * *Args*
793
- # - +:name+ : layer to identify.
796
+ # - +:name+ : layer to identify.
797
+ # - +&block+: loop on layer object & index. index added if yield is true
794
798
  #
795
799
  # * *Returns*
796
- # first index found or nil.
800
+ # array of indexes found or nil.
797
801
  #
798
- def layer_indexes(names)
799
- names = [names] if names.is_a?(String)
800
- return nil unless names.is_a?(Array)
801
-
802
+ def layer_indexes(names = nil)
802
803
  layers = []
803
804
 
804
- names.each do |name|
805
- index = layer_index(name)
806
- layers << index unless index.nil?
805
+ if block_given?
806
+ @config_layers.each.with_index do |layer, index|
807
+ layers << index if yield layer, index
808
+ end
809
+ else
810
+ names = [names] if names.is_a?(String)
811
+ return nil unless names.is_a?(Array)
812
+
813
+ names.each do |name|
814
+ index = layer_index(name)
815
+ layers << index unless index.nil?
816
+ end
807
817
  end
818
+
808
819
  return layers if layers.length > 0
809
820
  nil
810
821
  end
@@ -87,4 +87,129 @@ module PRC
87
87
  p_del(@data_options[:section], *keys)
88
88
  end
89
89
  end
90
+
91
+ # SectionsConfig class layer based on SectionConfig.
92
+ #
93
+ # It supports a data_options :sections/default for #[] and #exist? etc...
94
+ #
95
+ # The main difference with SectionConfig is :
96
+ # - :sections options is replacing :section for [] and exist?.
97
+ # search in collection of ordered sections. First found, first returned.
98
+ # - :section is still use like SectionConfig designed it.
99
+ # - :default is the default section to use. if not set, it will be :default.
100
+ #
101
+ class SectionsConfig < PRC::SectionConfig
102
+ # Get the value of a specific key under a section.
103
+ # You have to call #data_options(:section => 'MySection')
104
+ #
105
+ # * *Args* :
106
+ # - +keys+ : keys to get values from a sections/section set by
107
+ # data_options:
108
+ # - :sections: if not set, it will search only in what is set in
109
+ # :default_section.
110
+ # - :default_section : default section name to use.
111
+ # by default is ':default'
112
+ # * *Returns* :
113
+ # - first found value or nil if not found.
114
+ # * *Raises* :
115
+ # Nothing
116
+ def [](*keys)
117
+ return nil if keys.length == 0
118
+
119
+ if @data_options[:default_section].nil?
120
+ section = :default
121
+ else
122
+ section = @data_options[:default_section]
123
+ end
124
+
125
+ sections = @data_options[:sections]
126
+
127
+ if sections.is_a?(Array)
128
+ sections << section unless sections.include?(section)
129
+ else
130
+ sections = [section]
131
+ end
132
+
133
+ sections.each { |s| return p_get(s, *keys) if p_exist?(s, *keys) }
134
+
135
+ nil
136
+ end
137
+
138
+ # Check key existence under a section.
139
+ # You have to call #data_options(:section => 'MySection')
140
+ #
141
+ # * *Args* :
142
+ # - +keys+ : keys to get values from a section set by data_options:
143
+ # - :sections: if not set, it will search only in what is set in
144
+ # :default_section.
145
+ # - :default_section : default section name to use.
146
+ # by default is ':default'
147
+ #
148
+ # * *Returns* :
149
+ # - true if first found.
150
+ #
151
+ # * *Raises* :
152
+ # Nothing
153
+ #
154
+ # * *hint* :
155
+ # - If you want to know where to find a value, use where?
156
+ def exist?(*keys)
157
+ return nil if keys.length == 0
158
+
159
+ if @data_options[:default_section].nil?
160
+ section = :default
161
+ else
162
+ section = @data_options[:default_section]
163
+ end
164
+
165
+ sections = @data_options[:sections]
166
+
167
+ if sections.is_a?(Array)
168
+ sections << section unless sections.include?(section)
169
+ else
170
+ sections = [section]
171
+ end
172
+
173
+ sections.each { |s| return true if p_exist?(s, *keys) }
174
+
175
+ false
176
+ end
177
+
178
+ # where layer helper format Used by CoreConfig where?
179
+ #
180
+ # In the context of CoreConfig, this class is a layer with a name.
181
+ # CoreConfig will query this function to get a layer name.
182
+ # If the layer needs to add any other data, this function will need to
183
+ # be redefined.
184
+ #
185
+ # * *Args* :
186
+ # - name : name of this layer managed by CoreConfig
187
+ #
188
+ # * *Returns* :
189
+ # - name: Composed layer name return by the layer to CoreConfig
190
+ # return '<name>(<sections found sep by |>)'
191
+ #
192
+ def where?(keys, name)
193
+ return name unless exist?(*keys)
194
+
195
+ if @data_options[:default_section].nil?
196
+ section = :default
197
+ else
198
+ section = @data_options[:default_section]
199
+ end
200
+
201
+ sections = @data_options[:sections]
202
+
203
+ if sections.is_a?(Array)
204
+ sections << section unless sections.include?(section)
205
+ else
206
+ sections = [section]
207
+ end
208
+
209
+ sections_found = []
210
+ sections.each { |s| sections_found << s if p_exist?(s, *keys) }
211
+
212
+ format('%s(%s)', name, sections_found.join('|'))
213
+ end
214
+ end
90
215
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_layers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christophe Larsonneur
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-11 00:00:00.000000000 Z
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: subhash
@@ -140,4 +140,3 @@ signing_key:
140
140
  specification_version: 4
141
141
  summary: ConfigLayers, a simple multiple configuration management.
142
142
  test_files: []
143
- has_rdoc: