lib/gloo/core/pn.rb in gloo-0.3.0 vs lib/gloo/core/pn.rb in gloo-0.4.0

- old
+ new

@@ -7,126 +7,141 @@ module Gloo module Core class Pn < Baseo + ROOT = 'root'.freeze + IT = 'it'.freeze + ERROR = 'error'.freeze + attr_reader :src, :elements - + # Set up the object given a source string, # ie: the full path and name. def initialize( src ) set_to src end - + # Reference to the root object path. def self.root - return Pn.new( "root" ) + return Pn.new( ROOT ) end # Reference to it. def self.it - return Pn.new( "it" ) + return Pn.new( IT ) end - - # Does the pathname reference refer to the root? - def is_root? - return @src.downcase == "root" + + # Reference to the error message. + def self.error + return Pn.new( ERROR ) end - + # Does the pathname reference refer to the root? - def is_it? - return @src.downcase == "it" + def root? + return @src.downcase == ROOT end - + + # Does the pathname reference refer to it? + def it? + return @src.downcase == IT + end + + # Does the pathname reference refer to error? + def error? + return @src.downcase == ERROR + end + # Does the pathname reference refer to the gloo system object? - def is_gloo_sys? - return false unless @elements && @elements.count > 0 + 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 # Get the string representation of the pathname. def to_s return @src end # Set the object pathname to the given value. - def set_to value + def set_to( value ) @src = value.strip unless value.nil? - if @src.nil? - @elements = [] - else - @elements = @src.split( '.' ) - end + @elements = @src.nil? ? [] : @src.split( '.' ) end - + # Convert the raw string to a list of segments. def segments return @elements end - + # Get the name element. def name - return "" unless self.has_name? + return '' unless self.named? + return @elements.last end # Does the value include path elements? - def has_name? - return @elements.count > 0 + def named? + return @elements.count.positive? end # Does the value include a name? - def has_path? + def includes_path? return @elements.count > 1 end - + # Get the parent that contains the object referenced. def get_parent o = $engine.heap.root - - if self.has_path? - @elements[0..-2].each do |e| + + 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 - + # Does the object at the path exist? def exists? - return true if self.is_root? - return true if self.is_it? - + 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.has_child? name + + return parent.contains_child? name end - - # Is the reference to a color? - def is_color? - colors = [ "red", "blue", "green", "white", "black", "yellow" ] - return true if colors.include?( @src.downcase ) - end - + + # Is the reference to a color? + def named_color? + colors = %w[red blue green white black yellow] + return true if colors.include?( @src.downcase ) + end + # Resolve the pathname reference. # Find the object referenced or return nil if it is not found. def resolve - return $engine.heap.root if self.is_root? - return $engine.heap.it if self.is_it? - if self.is_gloo_sys? - return Gloo::Core::GlooSystem.new( self ) - end - + 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? + parent = self.get_parent return nil unless parent + return parent.find_child( self.name ) end end end