Class: GlooLang::Core::Pn

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

Constant Summary collapse

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

Constants inherited from Baseo

Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Baseo

#type_display

Constructor Details

#initialize(engine, src) ⇒ Pn

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



23
24
25
26
# File 'lib/gloo_lang/core/pn.rb', line 23

def initialize( engine, src )
  @engine = engine
  set_to src
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



17
18
19
# File 'lib/gloo_lang/core/pn.rb', line 17

def elements
  @elements
end

#srcObject (readonly)

Returns the value of attribute src.



17
18
19
# File 'lib/gloo_lang/core/pn.rb', line 17

def src
  @src
end

Class Method Details

.error(engine) ⇒ Object

Reference to the error message.



45
46
47
# File 'lib/gloo_lang/core/pn.rb', line 45

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

.it(engine) ⇒ Object

Reference to it.



38
39
40
# File 'lib/gloo_lang/core/pn.rb', line 38

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

.root(engine) ⇒ Object

Reference to the root object path.



31
32
33
# File 'lib/gloo_lang/core/pn.rb', line 31

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

Instance Method Details

#error?Boolean

Does the pathname reference refer to error?

Returns:

  • (Boolean)


66
67
68
# File 'lib/gloo_lang/core/pn.rb', line 66

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

#exists?Boolean

Does the object at the path exist?

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
# File 'lib/gloo_lang/core/pn.rb', line 165

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

#expand_contextObject

Expand the context so we have the full path.



138
139
140
141
# File 'lib/gloo_lang/core/pn.rb', line 138

def expand_context
  # return unless @engine.heap.context
  self.set_to( "#{@engine.heap.context}#{@src[1..-1]}" )
end

#get_parentObject

Get the parent that contains the object referenced.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gloo_lang/core/pn.rb', line 146

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?
        @engine.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)


73
74
75
76
77
78
79
80
81
# File 'lib/gloo_lang/core/pn.rb', line 73

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

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

  return false
end

#includes_context?Boolean

Does the path start with the context?

Returns:

  • (Boolean)


131
132
133
# File 'lib/gloo_lang/core/pn.rb', line 131

def includes_context?
  return @src.start_with?( "#{CONTEXT}." )
end

#includes_path?Boolean

Does the value include a name?

Returns:

  • (Boolean)


124
125
126
# File 'lib/gloo_lang/core/pn.rb', line 124

def includes_path?
  return @elements.count > 1
end

#it?Boolean

Does the pathname reference refer to it?

Returns:

  • (Boolean)


59
60
61
# File 'lib/gloo_lang/core/pn.rb', line 59

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

#nameObject

Get the name element.



108
109
110
111
112
# File 'lib/gloo_lang/core/pn.rb', line 108

def name
  return '' unless self.named?

  return @elements.last
end

#named?Boolean

Does the value include path elements?

Returns:

  • (Boolean)


117
118
119
# File 'lib/gloo_lang/core/pn.rb', line 117

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

#named_color?Boolean

Is the reference to a color?

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/gloo_lang/core/pn.rb', line 179

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/gloo_lang/core/pn.rb', line 188

def resolve
  return @engine.heap.root if self.root?
  return @engine.heap.it if self.it?
  return @engine.heap.error if self.error?
  return GlooLang::Core::GlooSystem.new(
    @engine, self ) if self.gloo_sys?

  if Here.includes_here_ref?( @elements )
    Here.expand_here( @engine, self )
  end

  if self.includes_context?
    expand_context
  end

  parent = self.get_parent
  return nil unless parent

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

#root?Boolean

Does the pathname reference refer to the root?

Returns:

  • (Boolean)


52
53
54
# File 'lib/gloo_lang/core/pn.rb', line 52

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

#segmentsObject

Convert the raw string to a list of segments.



101
102
103
# File 'lib/gloo_lang/core/pn.rb', line 101

def segments
  return @elements
end

#set_to(value) ⇒ Object

Set the object pathname to the given value.



93
94
95
96
# File 'lib/gloo_lang/core/pn.rb', line 93

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.



86
87
88
# File 'lib/gloo_lang/core/pn.rb', line 86

def to_s
  return @src
end