Class: Lorj::KeyPath

Inherits:
Object show all
Defined in:
lib/core/lorj_keypath.rb

Overview

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’

Instance Method Summary collapse

Constructor Details

#initialize(sKeyPath = nil, max_level = -1)) ⇒ KeyPath

Returns a new instance of KeyPath.



62
63
64
65
66
# 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

Instance Method Details

#fpathObject



93
94
95
96
97
98
99
100
101
102
# 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

#key(iIndex = -1)) ⇒ Object



115
116
117
118
# File 'lib/core/lorj_keypath.rb', line 115

def key(iIndex = -1)
  return nil if @keypath.length == 0
  @keypath[iIndex] if length >= 1
end

#key=(sKeyPath) ⇒ Object



68
69
70
# File 'lib/core/lorj_keypath.rb', line 68

def key=(sKeyPath)
  set(sKeyPath)
end

#key_treeObject



88
89
90
91
# File 'lib/core/lorj_keypath.rb', line 88

def key_tree
  return @keypath[0] if @keypath.length == 1
  fpath
end

#lengthObject



120
121
122
# File 'lib/core/lorj_keypath.rb', line 120

def length
  @keypath.length
end

#set(sKeyPath) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# 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

#to_sObject



104
105
106
107
108
109
110
111
112
113
# 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

#treeObject

rubocop: disable TrivialAccessors



84
85
86
# File 'lib/core/lorj_keypath.rb', line 84

def tree # rubocop: disable TrivialAccessors
  @keypath
end