class Lorj::KeyPath
Class to handle key or keypath on needs The application configuration can configure a key tree, instead of a key. KeyPath
is used to commonly handle key or key tree. Thus, a Keypath can be converted in different format:
Ex: oKey = KeyPath(:test) puts oKey.to_s # => 'test' puts oKey.key # => :test puts oKey.key(0) # => :test puts oKey.key(1) # => nil puts oKey.fpath # => ':test' puts oKey.tree # => [:test] puts oKey.key_tree # => :test
oKey = KeyPath() puts oKey.to_s # => 'test/test2/test3' puts oKey.key # => :test3 puts oKey.key(0) # => :test puts oKey.key(1) # => :test2 puts oKey.fpath # => ':test/:test2/:test3' puts oKey.tree # => [:test,:test2,:test3] puts oKey.key_tree # => ':test/:test2/:test3'
oKey = KeyPath([:test, '{/.*/}', :test3]) puts oKey.to_s # => 'test/{/.*/}/test3' puts oKey.key # => :test3 puts oKey.key(0) # => :test puts oKey.key(1) # => '{/.*/}' puts oKey.fpath # => ':test/{/.*/}/:test3' puts oKey.tree # => [:test, '{/.*/}',:test3] puts oKey.key_tree # => ':test/{/.*/}/:test3'
Public Class Methods
# File lib/core/lorj_keypath.rb, line 62 def initialize(sKeyPath = nil, max_level = -1) @keypath = [] @max_level = max_level set sKeyPath unless sKeyPath.nil? end
Public Instance Methods
# File lib/core/lorj_keypath.rb, line 93 def fpath return nil if @keypath.length == 0 akey = @keypath.clone akey.each_index do |i| akey[i] = akey[i].gsub(%r{/}, '\/') if akey[i].is_a?(String) next unless akey[i].is_a?(Symbol) akey[i] = ':' + akey[i].to_s end akey.join('/') end
# File lib/core/lorj_keypath.rb, line 115 def key(iIndex = -1) return nil if @keypath.length == 0 @keypath[iIndex] if length >= 1 end
# File lib/core/lorj_keypath.rb, line 68 def key=(sKeyPath) set(sKeyPath) end
# File lib/core/lorj_keypath.rb, line 88 def key_tree return @keypath[0] if @keypath.length == 1 fpath end
# File lib/core/lorj_keypath.rb, line 120 def length @keypath.length end
# File lib/core/lorj_keypath.rb, line 72 def set(sKeyPath) if sKeyPath.is_a?(Symbol) @keypath = [sKeyPath] elsif sKeyPath.is_a?(Array) @keypath = sKeyPath elsif sKeyPath.is_a?(String) @keypath = string_to_sarray(sKeyPath) end PrcLib.error 'key path size limit (%s) reached', @max_level if @max_level > 0 && @keypath.length > @max_level end
# File lib/core/lorj_keypath.rb, line 104 def to_s return nil if @keypath.length == 0 akey = @keypath.clone akey.each_index do |i| akey[i] = akey[i].gsub(%r{/}, '\/') if akey[i].is_a?(String) next unless akey[i].is_a?(Symbol) akey[i] = akey[i].to_s end akey.join('/') end
# File lib/core/lorj_keypath.rb, line 84 def tree # rubocop: disable TrivialAccessors @keypath end
Private Instance Methods
# File lib/core/lorj_keypath.rb, line 126 def string_to_sarray(sKeyPath) # rubocop: disable Style/RegexpLiteral if %r{[^\\/]?/[^/]} =~ sKeyPath || %r{:[^:/]} =~ sKeyPath # rubocop: enable Style/RegexpLiteral res = [] # split then rejoin / prefixed by \ sKeyPath.split('/').each do |s| if res[-1] && res[-1].match(/\\$/) res[-1][-1] = '' res[-1] += '/' + s else res << s end end res.each_index do |iIndex| # Ruby 1.8 : 'ab'[1] => 98 and 'ab'[1, 1] => 'b' # Ruby 1.9 + : 'ab'[1] => 'b' and 'ab'[1, 1] => 'b' res[iIndex] = res[iIndex][1..-1].to_sym if res[iIndex][0, 1] == ':' end @keypath = res else @keypath = [sKeyPath] end end