module Lebowski::Foundation::Mixins::DefinePathsSupport

Mixin provides objects with behavior to define symbolic paths that can be used to help access remote objects

Public Instance Methods

define_path(path, rel_path=nil, expected_type=nil) click to toggle source

Defines symbolic path

The path given must be a string. The path itself follows a standard dot-path notatition, such as the following examples:

"foo"
"foo.bar"
"foo.bar.mah"

You can optionally provide a relative path and an expected type. When a relative path is not provided, then the given path acts as a place holder, which can be useful to organize symbolic paths into groups. If a relative path is provided then it is used to access an actual object in the web browser.

If an expected type is provided, then it will be used to check the type of the returned object whenever the defined symbolic path is used to access a remote object. if the object being proxied is not of the expectec type then an exception will be thrown

Examples of how to define a path:

obj.define_path 'foo'               # foo acts as a place holder
obj.define_path 'bar', 'x.y.x'      # bar is a symolic path for 'x.y.x'
obj.define_path 'foo.aaa', 'a.b.c'  # aaa is a symbolic path for 'a.b.c'
obj.define_path 'foo.bbb', 'm.n.o'  # bbb is a symbolic path for 'm.n.o'
obj.define_path 'stuff.xxx', 'h.j.k'  # xxx is a symbolic path for 'h.j.k'. stuff is a place holder
obj.define_path 'mah', 'bar.thing'  # mah is a symbolic path for 'x.y.z.thing'. bar is automatically replaced
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 47
def define_path(path, rel_path=nil, expected_type=nil)
  if (not path.kind_of?(String)) or path.empty?
    raise ArgumentError.new "path must be a valid string"
  end
  
  current_path_part = root_defined_path_part
  
  path_parts = path.split('.')
  counter = 1
  path_parts.each do |part|
    if current_path_part.has_path_part? part 
      current_path_part = current_path_part[part]
    else
      if counter == path_parts.length
        current_path_part = current_path_part.add_path_part part, rel_path, expected_type
      else
        current_path_part = current_path_part.add_path_part part
      end
    end
    counter = counter.next
  end
  
  return current_path_part
end
define_paths_for(path) { |path_part| ... } click to toggle source

Defines relative symbolic paths for a given symbolic path that has been defined.

Use when you are trying to define many symbolic paths that are all relative to another symbolic path that has already been defined. The following is an example of how it is used:

obj.define_path 'foo', 'a.b.c' # first define the parent symbolic path

obj.define_paths_for 'foo' do |path|
  # Paths defined are all relative to foo
  path.define_path 'bar', 'x.y.z'
  path.define_path 'mah', 'm.n.o'
end

x = obj['foo.bar'] # path will be converted to actual path 'a.b.c.x.y.z'
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 89
def define_paths_for(path, &block)
  path_part = root_defined_path_part[path]
  if path_part.nil?
    raise ArgumentError.new "can not define paths for #{path} since it has not yet been defined"
  end
  
  yield path_part
end
defined_path(path) click to toggle source

Gets a symbolic path object if it has been defined

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 108
def defined_path(path)
  return root_defined_path_part[path]
end
defined_paths() click to toggle source

Returns the root symbolic path object for the given object

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 115
def defined_paths()
  return root_defined_path_part
end
path_defined?(path) click to toggle source

Checks if a given path has been defined

# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 101
def path_defined?(path)
  return (not root_defined_path_part[path].nil?)
end
root_defined_path_part() click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 119
def root_defined_path_part()
  if @root_defined_path_part.nil?
    @root_defined_path_part = Support::DefinedPathPart.new
  end
  return @root_defined_path_part
end
root_defined_path_part=(path) click to toggle source
# File lib/lebowski/foundation/mixins/define_paths_support.rb, line 126
def root_defined_path_part=(path)
  if not path.kind_of? Support::DefinedPathPart
    raise ArgumentInvalidTypeError.new 'part', part, DefinedPathPart
  end
  @root_defined_path_part = path
end