lib/lorj_account.rb in lorj-1.0.8 vs lib/lorj_account.rb in lorj-1.0.9

- old
+ new

@@ -15,20 +15,24 @@ # See the License for the specific language governing permissions and # limitations under the License. require 'rubygems' +require 'erb' + # Lorj implements Lorj::Accounts module Lorj # Simple List of accounts class. class Accounts # Class to query FORJ Accounts list. def initialize @account_path = File.join(PrcLib.data_path, 'accounts') end def dump + return [] unless File.directory?(@account_path) + accounts = [] Dir.foreach(@account_path) do |x| accounts << x unless x.match(/^\..?$/) end accounts @@ -53,10 +57,26 @@ p_data_options(options) end end end +module Lorj + # This class limits ERB template to access only to config object data. + class ERBConfig + attr_reader :config + + def initialize(config) + @config = config + end + + # Bind this limited class with ERB templates + def get_binding # rubocop: disable AccessorMethodName + binding + end + end +end + # Lorj implements Lorj::Account module Lorj # Lorj::Account manage a list of key/value grouped by section. # The intent of Lorj::Account is to attach some keys/values to # an account to help end users to switch between accounts. @@ -152,10 +172,12 @@ # get function. # If the Application meta data option of the key is set with # :account_exclusive => true, get will limit to runtime then account. # otherwise, search in all layers. # + # The data found is parse through ERB with self as context. + # # * *Args* : # - +key+ : key name. It do not support it to be a key tree (Arrays of # keys). # - +default+ : default value, if not found. # - +options+ : Options for get: @@ -181,14 +203,17 @@ 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) + options[:data_options] = _set_data_options_per_names(names, section) - return p_get(options) if p_exist?(options) - + if p_exist?(options) + value = p_get(options) + return value unless value.is_a?(String) + return ERB.new(value).result ERBConfig.new(self).get_binding + end default end # Simple get call with default options # Alternative is to use Account::get @@ -230,11 +255,11 @@ where_options = { :keys => [key], :section => section, :indexes => indexes, - :data_options => _set_data_options_per_names(names) + :data_options => _set_data_options_per_names(names, section) } p_where?(where_options) end @@ -273,11 +298,11 @@ 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) + options[:data_options] = _set_data_options_per_names(names, section) p_exist?(options) end # Return true if readonly. set won't be able to update this value. @@ -561,33 +586,40 @@ options[:indexes] = indexes indexes end - def _set_data_options_per_names(names) + def _set_data_options_per_names(names, section) data_options = [] - names.each { |name| data_options << _data_options_per_layer(name) } + names.each do |name| + data_options << _data_options_per_layer(name, section) + end data_options end - # TODO: Change local and default way to get default values, not in /:default - # This internal function defines default section name per config index. - def _data_options_per_layer(layer_name) + def _data_options_per_layer(layer_name, section) # runtime and local and default uses :default section case layer_name - when 'local', 'default' + when 'default' + return { :section => :default, :metadata_section => section } + when 'local' # local & default are SectionConfig and is forced to use :default as # section name for each data. - { :section => :default } + return { :section => :default } 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. + nil end def _do_load(config, account_file) result = config.load account_file return result unless result == true