lib/lorj_account.rb in lorj-1.0.10 vs lib/lorj_account.rb in lorj-1.0.11

- old
+ new

@@ -195,15 +195,17 @@ def get(key, default = nil, options = {}) key = key.to_sym if key.class == String options = {} unless options.is_a?(Hash) section = options[:section] - section = Lorj.data.first_section(key) if section.nil? + section, key = Lorj.data.first_section(key) if section.nil? - options = options.merge(:keys => [key], :section => section) + options = options.merge(:keys => [key]) + options.delete(:section) indexes = _identify_indexes(options, exclusive?(key, section)) + names = [] indexes.each { |index| names << @config_layers[index][:name] } options[:data_options] = _set_data_options_per_names(names, section) @@ -244,20 +246,19 @@ def where?(key, options = {}) key = key.to_sym if key.class == String options = {} unless options.is_a?(Hash) section = options[:section] - section = Lorj.data.first_section(key) if section.nil? + section, key = Lorj.data.first_section(key) if section.nil? indexes = _identify_indexes(options, exclusive?(key, section)) names = [] indexes.each { |index| names << @config_layers[index][:name] } where_options = { :keys => [key], - :section => section, :indexes => indexes, :data_options => _set_data_options_per_names(names, section) } p_where?(where_options) @@ -290,12 +291,13 @@ def exist?(key, options = nil) key = key.to_sym if key.class == String options = {} unless options.is_a?(Hash) section = options[:section] - section = Lorj.data.first_section(key) if section.nil? - options = options.merge(:keys => [key], :section => section) + section, key = Lorj.data.first_section(key) if section.nil? + options = options.merge(:keys => [key]) + options.delete(:section) indexes = _identify_indexes(options, exclusive?(key, section)) names = [] indexes.each { |index| names << @config_layers[index][:name] } @@ -321,10 +323,11 @@ def readonly?(key, section = nil) return nil unless key key = key.to_sym if key.class == String section = Lorj.defaults.get_meta_section(key) if section.nil? + section, key = _detect_section(key, section) return nil if section.nil? result = Lorj.data.section_data(section, key, :readonly) return result if result.boolean? @@ -347,11 +350,11 @@ # - section was not found def exclusive?(key, section = nil) return nil unless key key = key.to_sym if key.class == String - section = Lorj.data.first_section(key) if section.nil? + section, key = Lorj.data.first_section(key) if section.nil? return nil if section.nil? result = Lorj.data[:sections, section, key, :account_exclusive] return result if result.boolean? result @@ -384,13 +387,14 @@ def set(key, value, options = {}) parameters = validate_key_and_options(key, options) return nil if parameters.nil? key = parameters[0][0] - layer_name, section = parameters[1] + layer_name, section = parameters[1][0] - section = Lorj.data.first_section(key) if section.nil? + found_section, key = Lorj.data.first_section(key) + section = found_section if section.nil? section = :default if section.nil? return nil if readonly?(key, section) options = { :keys => [key], :section => section, :value => value } @@ -437,10 +441,11 @@ key = parameters[0][0] layer_name, section = parameters[1] section = Lorj.defaults.get_meta_section(key) if section.nil? section = :default if section.nil? + section, key = _detect_section(key, section) return nil if readonly?(key, section) options = { :keys => [key], :section => section } @@ -542,10 +547,16 @@ # private functions private + def _detect_section(key, default_section) + m = key.to_s.match(/^(.*)#(.*)$/) + return [m[1].to_sym, m[2].to_sym] if m && m[1] != '' && m[2] != '' + [default_section, key] + end + def _identify_array_indexes(options, account_exclusive) def_indexes = options[:indexes] if options.key?(:indexes) if options[:names].is_a?(Array) def_indexes = layers_indexes(options[:names]) end @@ -569,12 +580,13 @@ end indexes end def exclusive_indexes(account_exclusive) - return [0, 1] if account_exclusive - [0, 1, 2, 3, 4] + return layer_indexes { true } unless account_exclusive + + layer_indexes { |n, _i| %w(runtime account).include?(n[:name]) } end def _identify_indexes(options, account_exclusive) index = options[:index] if options.key?(:index) index = layer_index(options[:name]) if options.key?(:name) @@ -586,10 +598,13 @@ options[:indexes] = indexes indexes end + # Internal functions to generate the list of options + # for each layer name. + # The names order needs to be kept in options. def _set_data_options_per_names(names, section) data_options = [] names.each do |name| data_options << _data_options_per_layer(name, section) @@ -606,19 +621,24 @@ return { :section => :default, :metadata_section => section } when 'local' # local & default are SectionConfig and is forced to use :default as # section name for each data. return { :section => :default } + when 'account' + # If no section is provided, 'account' layer will use the first section + # name + # otherwise, it will used the section provided. + # account is a SectionConfig and use section value defined by the + # lorj data model. So the section name is not forced. + return { :section => section } unless section.nil? + return nil end - # nil: layer_index = 0 => runtime. runtime is not a SectionConfig. - - # nil: layer_index = 1 => account - # If no section is provided, 'account' layer will use the first section - # name - # otherwise, it will used the section provided. - return { :section => section } unless section.nil? - # account is a SectionConfig and use section value defined by the - # lorj data model. So the section name is not forced. + # For SectionsConfig layer, :default is automatically set. + # Except if there is later a need to set a section, nil is enough. + # For BaseConfig, no options => nil + # + # Process/controller layers are SectionsConfig + # others (runtime/instant) are BaseConfig. nil end def _do_load(config, account_file) result = config.load account_file