Class: Gloo::Core::Pn

Inherits:
Baseo
  • Object
show all
Defined in:
lib/gloo/core/pn.rb

Constant Summary collapse

ROOT =
'root'.freeze
IT =
'it'.freeze
ERROR =
'error'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Baseo

#type_display

Constructor Details

#initialize(src) ⇒ Pn

Set up the object given a source string, ie: the full path and name.



22
23
24
# File 'lib/gloo/core/pn.rb', line 22

def initialize( src )
  set_to src
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



16
17
18
# File 'lib/gloo/core/pn.rb', line 16

def elements
  @elements
end

#srcObject (readonly)

Returns the value of attribute src.



16
17
18
# File 'lib/gloo/core/pn.rb', line 16

def src
  @src
end

Class Method Details

.errorObject

Reference to the error message.



43
44
45
# File 'lib/gloo/core/pn.rb', line 43

def self.error
  return Pn.new( ERROR )
end

.itObject

Reference to it.



36
37
38
# File 'lib/gloo/core/pn.rb', line 36

def self.it
  return Pn.new( IT )
end

.rootObject

Reference to the root object path.



29
30
31
# File 'lib/gloo/core/pn.rb', line 29

def self.root
  return Pn.new( ROOT )
end

Instance Method Details

#error?Boolean

Does the pathname reference refer to error?

Returns:

  • (Boolean)


64
65
66
# File 'lib/gloo/core/pn.rb', line 64

def error?
  return @src.downcase == ERROR
end

#exists?Boolean

Does the object at the path exist?

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
# File 'lib/gloo/core/pn.rb', line 148

def exists?
  return true if self.root?
  return true if self.it?
  return true if self.error?

  parent = self.get_parent
  return false unless parent

  return parent.contains_child? name
end

#get_parentObject

Get the parent that contains the object referenced.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gloo/core/pn.rb', line 129

def get_parent
  o = $engine.heap.root

  if self.includes_path?
    @elements[ 0..-2 ].each do |e|
      o = o.find_child( e )
      if o.nil?
        $log.error "Object '#{e}' was not found."
        return nil
      end
    end
  end

  return o
end

#gloo_sys?Boolean

Does the pathname reference refer to the gloo system object?

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/gloo/core/pn.rb', line 71

def gloo_sys?
  return false unless @elements&.count&.positive?

  o = @elements.first.downcase
  return true if o == Gloo::Core::GlooSystem.typename
  return true if o == Gloo::Core::GlooSystem.short_typename

  return false
end

#includes_path?Boolean

Does the value include a name?

Returns:

  • (Boolean)


122
123
124
# File 'lib/gloo/core/pn.rb', line 122

def includes_path?
  return @elements.count > 1
end

#it?Boolean

Does the pathname reference refer to it?

Returns:

  • (Boolean)


57
58
59
# File 'lib/gloo/core/pn.rb', line 57

def it?
  return @src.downcase == IT
end

#nameObject

Get the name element.



106
107
108
109
110
# File 'lib/gloo/core/pn.rb', line 106

def name
  return '' unless self.named?

  return @elements.last
end

#named?Boolean

Does the value include path elements?

Returns:

  • (Boolean)


115
116
117
# File 'lib/gloo/core/pn.rb', line 115

def named?
  return @elements.count.positive?
end

#named_color?Boolean

Is the reference to a color?

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/gloo/core/pn.rb', line 162

def named_color?
  colors = %w[red blue green white black yellow]
  return true if colors.include?( @src.downcase )
end

#resolveObject

Resolve the pathname reference. Find the object referenced or return nil if it is not found.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gloo/core/pn.rb', line 171

def resolve
  return $engine.heap.root if self.root?
  return $engine.heap.it if self.it?
  return $engine.heap.error if self.error?
  return Gloo::Core::GlooSystem.new( self ) if self.gloo_sys?

  Here.expand_here( self ) if Here.includes_here_ref?( @elements )

  parent = self.get_parent
  return nil unless parent

  obj = parent.find_child( self.name )
  return Gloo::Objs::Alias.resolve_alias( obj, self.src )
end

#root?Boolean

Does the pathname reference refer to the root?

Returns:

  • (Boolean)


50
51
52
# File 'lib/gloo/core/pn.rb', line 50

def root?
  return @src.downcase == ROOT
end

#segmentsObject

Convert the raw string to a list of segments.



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

def segments
  return @elements
end

#set_to(value) ⇒ Object

Set the object pathname to the given value.



91
92
93
94
# File 'lib/gloo/core/pn.rb', line 91

def set_to( value )
  @src = value.nil? ? nil : value.strip
  @elements = @src.nil? ? [] : @src.split( '.' )
end

#to_sObject

Get the string representation of the pathname.



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

def to_s
  return @src
end