class Lebowski::Foundation::Mixins::Support::DefinedPathPart

Represents a part of a defined path. As an example, if 'foo.bar' was a defined path then the string would be split up an represented as two parts: foo and bar where foo is the parent of bar.

Attributes

expected_type[R]
name[R]
parent[R]
rel_path[R]

Public Class Methods

new(name=nil, parent=nil, rel_path=nil, expected_type=nil) click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 148
def initialize(name=nil, parent=nil, rel_path=nil, expected_type=nil)
  @name = name
  @parent = parent
  @rel_path = rel_path
  @expected_type = expected_type
  @child_path_parts = {}
end

Public Instance Methods

[](value) click to toggle source

Used access a descendent part based on the given string representing relative symbolic path to this part. As an example, if “foo.bar” was provided, the string would be split up and each part of the string would be traversed in order to find the last descendent part. If the given path can not be followed then nil is returned

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 179
def [](value)
  return nil if value.nil?
  
  if not value.kind_of? String
    raise ArgumentInvalidTypeError.new 'value', value, String
  end
  
  current_path_part = self
  path_parts = value.split('.')
  counter = 1
  path_parts.each do |part|
    current_path_part = current_path_part.child_path_part(part)
    return nil if current_path_part.nil?
  end
  return current_path_part
end
add_path_part(name, rel_path=nil, expected_type=nil) click to toggle source

Add as path part to this object. The part can optionally be associated with a relative path and expected type

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 200
def add_path_part(name, rel_path=nil, expected_type=nil)
  if not name.kind_of? String
    raise ArgumentInvalidTypeError.new 'name', name, String
  end
    
  if has_path_part? name
    raise ArgumentError "path part #{name} has already been added"
  end
  
  rel_path_parts = (not rel_path.kind_of? String) ? [] : rel_path.split('.')
  
  if (not rel_path_parts.empty?) and has_path_part?(rel_path_parts[0])
    rel_path = ""
    rel_path << self[rel_path_parts[0]].full_rel_path
    if (rel_path_parts.length > 1)
      rel_path << "." << rel_path_parts.last(rel_path_parts.length - 1).join('.')
    end
  end
    
  path_part = create_path_part(name, self, rel_path, expected_type)
    
  @child_path_parts[path_part.name] = path_part
  return path_part
end
count() click to toggle source

Returns the number of child parts this part has

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 159
def count()
  return @child_path_parts.count
end
defined_paths() click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 289
def defined_paths()
  return self
end
each() { |value| ... } click to toggle source

Used to iterate over this part's child parts

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 166
def each(&block)
  @child_path_parts.each do |key, value|
    yield value
  end
end
full_defined_path() click to toggle source

Will generate a full defined path for this part

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 259
def full_defined_path()
  return nil if is_root?
  return name if parent.is_root?
  return "#{parent.full_defined_path}.#{name}"
end
full_rel_path() click to toggle source

Will generate a full relative path for this part. The relative path generated is based on this part and all of its parent parts' relative paths.

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 270
def full_rel_path()
  return nil if rel_path.nil?
  
  path = nil
  current_part = self
  while not current_part.nil? do
    if (not current_part.rel_path.nil?)
      if path.nil?
        path = current_part.rel_path
      else
        path = "#{current_part.rel_path}.#{path}"
      end
    end
    current_part = current_part.parent
  end
  
  return path
end
has_expected_type?() click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 239
def has_expected_type?()
  return (not expected_type.nil?)
end
has_path_part?(part) click to toggle source

Checks if this part has a child part matching the given value

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 246
def has_path_part?(part)
  if part.kind_of? DefinedPathPart
    part = part.name
  elsif not part.kind_of? String
    raise ArgumentInvalidTypeError.new 'part', part, String, DefinedPathPart
  end
  
  return @child_path_parts.has_key?(part)
end
is_place_holder?() click to toggle source

Checks if this part is just a placed holder.

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 235
def is_place_holder?()
  return rel_path.nil?
end
is_root?() click to toggle source

Checks if this part is a root part

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 228
def is_root?()
  return (name.nil? and parent.nil?) 
end
to_s() click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 293
def to_s()
  return "DefinedPathPart<name=#{name}, rel_path=#{rel_path}, full_defined_path=#{full_defined_path}>"
end

Protected Instance Methods

child_path_part(part) click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 303
def child_path_part(part)
  return @child_path_parts[part]
end
create_path_part(name, parent, rel_path, expected_type) click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 307
def create_path_part(name, parent, rel_path, expected_type)
  return DefinedPathPart.new name, parent, rel_path, expected_type
end
root_defined_path_part() click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 299
def root_defined_path_part()
  return self
end