Module: Lebowski::Foundation::Mixins::DefinePathsSupport

Includes:
Lebowski::Foundation
Included in:
Support::DefinedPathPart, ProxyObject
Defined in:
lib/lebowski/foundation/mixins/define_paths_support.rb

Overview

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

Constant Summary

Constant Summary

Constants included from Lebowski::Foundation

SC_BRANCH_CLOSED, SC_BRANCH_OPEN, SC_BUTTON1_STATUS, SC_BUTTON2_STATUS, SC_BUTTON3_STATUS, SC_LEAF_NODE, SC_MIXED_STATE, SC_PICKER_FIXED, SC_PICKER_MENU, SC_PICKER_POINTER, SC_T_ARRAY, SC_T_BOOL, SC_T_CLASS, SC_T_ERROR, SC_T_FUNCTION, SC_T_HASH, SC_T_NULL, SC_T_NUMBER, SC_T_OBJECT, SC_T_STRING, SC_T_UNDEFINED

Instance Method Summary (collapse)

Instance Method Details

- (Object) define_path(path, rel_path = nil, expected_type = nil)

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


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

- (Object) define_paths_for(path) {|path_part| ... }

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'

Yields:

  • (path_part)


89
90
91
92
93
94
95
96
# 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

- (Object) defined_path(path)

Gets a symbolic path object if it has been defined



108
109
110
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 108

def defined_path(path)
  return root_defined_path_part[path]
end

- (Object) defined_paths

Returns the root symbolic path object for the given object



115
116
117
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 115

def defined_paths()
  return root_defined_path_part
end

- (Boolean) path_defined?(path)

Checks if a given path has been defined

Returns:

  • (Boolean)


101
102
103
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 101

def path_defined?(path)
  return (not root_defined_path_part[path].nil?)
end

- (Object) root_defined_path_part



119
120
121
122
123
124
# 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

- (Object) root_defined_path_part=(path)



126
127
128
129
130
131
# 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