Class: DmozSax::Path

Inherits:
Array
  • Object
show all
Defined in:
lib/dmoz_sax/path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, level = 0) ⇒ Path

Returns a new instance of Path.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dmoz_sax/path.rb', line 8

def initialize str, level = 0
  resource = str.gsub('_', ' ').split(':')

  @name = resource.first if resource.length == 2
  @level = level.to_i

  unless resource.empty?
    super(resource.last.split('/'))
  else
    super([])
  end
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/dmoz_sax/path.rb', line 6

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dmoz_sax/path.rb', line 6

def name
  @name
end

Class Method Details

.normalize(arr) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/dmoz_sax/path.rb', line 33

def self.normalize arr
  if arr.nil? or arr.length == 0
    []
  else
    dup = arr.dup.reject {|a| a =~ /^[A-Z0-9]$/}
    dup.shift if arr.first == 'Top'
    dup
  end
end

.path_str(arr) ⇒ Object



29
30
31
# File 'lib/dmoz_sax/path.rb', line 29

def self.path_str arr
  arr.empty? ? '/' : "/#{ arr.join('/') }"
end

Instance Method Details

#parent_to_sObject



25
26
27
# File 'lib/dmoz_sax/path.rb', line 25

def parent_to_s
  DmozSax::Path.path_str(DmozSax::Path.normalize(self[0...-1]))
end

#to_sObject



21
22
23
# File 'lib/dmoz_sax/path.rb', line 21

def to_s
  DmozSax::Path.path_str(DmozSax::Path.normalize(self))
end