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 # => :test puts oKey.key # => nil puts oKey.fpath # => ‘:test’ puts oKey.tree # => [:test]

oKey = KeyPath() puts oKey.to_s # => ‘test/test2/test3’ puts oKey.key # => :test3 puts oKey.key # => :test puts oKey.key # => :test2 puts oKey.fpath # => ‘:test/:test2/:test3’ puts oKey.tree # => [:test,:test2,:test3]

Instance Method Summary collapse

Constructor Details

#initialize(sKeyPath = nil) ⇒ KeyPath

Returns a new instance of KeyPath.



51
52
53
54
# File 'lib/core/lorj_keypath.rb', line 51

def initialize(sKeyPath = nil)
  @keypath = []
  set sKeyPath
end

Instance Method Details

#fpathObject



74
75
76
77
78
79
80
81
82
# File 'lib/core/lorj_keypath.rb', line 74

def fpath
  return nil if @keypath.length == 0
  key_access = @keypath.clone
  key_access.each_index do |iIndex|
    next unless key_access[iIndex].is_a?(Symbol)
    key_access[iIndex] = ':' + key_access[iIndex].to_s
  end
  key_access.join('/')
end

#key(iIndex = -1)) ⇒ Object



94
95
96
97
# File 'lib/core/lorj_keypath.rb', line 94

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

#key=(sKeyPath) ⇒ Object



56
57
58
# File 'lib/core/lorj_keypath.rb', line 56

def key=(sKeyPath)
  set(sKeyPath)
end

#lengthObject



99
100
101
# File 'lib/core/lorj_keypath.rb', line 99

def length
  @keypath.length
end

#set(sKeyPath) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/core/lorj_keypath.rb', line 60

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
end

#to_sObject



84
85
86
87
88
89
90
91
92
# File 'lib/core/lorj_keypath.rb', line 84

def to_s
  return nil if @keypath.length == 0
  key_access = @keypath.clone
  key_access.each_index do |iIndex|
    next unless key_access[iIndex].is_a?(Symbol)
    key_access[iIndex] = key_access[iIndex].to_s
  end
  key_access.join('/')
end

#treeObject

rubocop: disable TrivialAccessors



70
71
72
# File 'lib/core/lorj_keypath.rb', line 70

def tree # rubocop: disable TrivialAccessors
  @keypath
end