Class: Lebowski::Foundation::Mixins::Support::DefinedPathPart

Inherits:
Object
  • Object
show all
Includes:
Lebowski::Foundation, DefinePathsSupport
Defined in:
lib/lebowski/foundation/mixins/define_paths_support.rb

Overview

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.

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 Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from DefinePathsSupport

#define_path, #define_paths_for, #defined_path, #path_defined?, #root_defined_path_part=

Constructor Details

- (DefinedPathPart) initialize(name = nil, parent = nil, rel_path = nil, expected_type = nil)

A new instance of DefinedPathPart



148
149
150
151
152
153
154
# 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

Instance Attribute Details

- (Object) expected_type (readonly)

Returns the value of attribute expected_type



146
147
148
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 146

def expected_type
  @expected_type
end

- (Object) name (readonly)

Returns the value of attribute name



146
147
148
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 146

def name
  @name
end

- (Object) parent (readonly)

Returns the value of attribute parent



146
147
148
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 146

def parent
  @parent
end

- (Object) rel_path (readonly)

Returns the value of attribute rel_path



146
147
148
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 146

def rel_path
  @rel_path
end

Instance Method Details

- (Object) [](value)

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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

- (Object) add_path_part(name, rel_path = nil, expected_type = nil)

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 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

- (Object) count

Returns the number of child parts this part has



159
160
161
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 159

def count()
  return @child_path_parts.count
end

- (Object) defined_paths



289
290
291
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 289

def defined_paths()
  return self
end

- (Object) each(&block)

Used to iterate over this part's child parts



166
167
168
169
170
# 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

- (Object) full_defined_path

Will generate a full defined path for this part



259
260
261
262
263
# 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

- (Object) full_rel_path

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.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# 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

- (Boolean) has_expected_type?

Returns:

  • (Boolean)


239
240
241
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 239

def has_expected_type?()
  return (not expected_type.nil?)
end

- (Boolean) has_path_part?(part)

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

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
# 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

- (Boolean) is_place_holder?

Checks if this part is just a placed holder.

Returns:

  • (Boolean)


235
236
237
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 235

def is_place_holder?()
  return rel_path.nil?
end

- (Boolean) is_root?

Checks if this part is a root part

Returns:

  • (Boolean)


228
229
230
# File 'lib/lebowski/foundation/mixins/define_paths_support.rb', line 228

def is_root?()
  return (name.nil? and parent.nil?) 
end

- (Object) to_s



293
294
295
# 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