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
CONTEXT =
'@'.freeze
NAMED_COLORS =
%w[red blue green white black yellow].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.



24
25
26
27
# File 'lib/gloo/core/pn.rb', line 24

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

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



18
19
20
# File 'lib/gloo/core/pn.rb', line 18

def elements
  @elements
end

#srcObject (readonly)

Returns the value of attribute src.



18
19
20
# File 'lib/gloo/core/pn.rb', line 18

def src
  @src
end

Class Method Details

.error(engine) ⇒ Object

Reference to the error message.



46
47
48
# File 'lib/gloo/core/pn.rb', line 46

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

.it(engine) ⇒ Object

Reference to it.



39
40
41
# File 'lib/gloo/core/pn.rb', line 39

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

.root(engine) ⇒ Object

Reference to the root object path.



32
33
34
# File 'lib/gloo/core/pn.rb', line 32

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

Instance Method Details

#error?Boolean

Does the pathname reference refer to error?

Returns:

  • (Boolean)


67
68
69
# File 'lib/gloo/core/pn.rb', line 67

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

#exists?Boolean

Does the object at the path exist?

Returns:

  • (Boolean)


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

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.



139
140
141
142
# File 'lib/gloo/core/pn.rb', line 139

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.



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

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.err "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)


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

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_context?Boolean

Does the path start with the context?

Returns:

  • (Boolean)


132
133
134
# File 'lib/gloo/core/pn.rb', line 132

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

#includes_path?Boolean

Does the value include a name?

Returns:

  • (Boolean)


125
126
127
# File 'lib/gloo/core/pn.rb', line 125

def includes_path?
  return @elements.count > 1
end

#it?Boolean

Does the pathname reference refer to it?

Returns:

  • (Boolean)


60
61
62
# File 'lib/gloo/core/pn.rb', line 60

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

#nameObject

Get the name element.



109
110
111
112
113
# File 'lib/gloo/core/pn.rb', line 109

def name
  return '' unless self.named?

  return @elements.last
end

#named?Boolean

Does the value include path elements?

Returns:

  • (Boolean)


118
119
120
# File 'lib/gloo/core/pn.rb', line 118

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

#named_color?Boolean

Is the reference to a color?

Returns:

  • (Boolean)


180
181
182
183
184
# File 'lib/gloo/core/pn.rb', line 180

def named_color?
  return true if NAMED_COLORS.include?( @src.downcase )

  return false
end

#resolveObject

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



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

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(
    @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 Gloo::Objs::Alias.resolve_alias( @engine, obj, self.src )
end

#root?Boolean

Does the pathname reference refer to the root?

Returns:

  • (Boolean)


53
54
55
# File 'lib/gloo/core/pn.rb', line 53

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

#segmentsObject

Convert the raw string to a list of segments.



102
103
104
# File 'lib/gloo/core/pn.rb', line 102

def segments
  return @elements
end

#set_to(value) ⇒ Object

Set the object pathname to the given value.



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

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.



87
88
89
# File 'lib/gloo/core/pn.rb', line 87

def to_s
  return @src
end