Class: Yoda::Model::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/model/path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Path

Returns a new instance of Path.

Parameters:

  • name (String)


19
20
21
22
# File 'lib/yoda/model/path.rb', line 19

def initialize(name)
  fail ArgumentError, name unless name.is_a?(String)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/yoda/model/path.rb', line 4

def name
  @name
end

Class Method Details

.build(path) ⇒ Path

Parameters:

  • path (Path, String)

Returns:



8
9
10
# File 'lib/yoda/model/path.rb', line 8

def self.build(path)
  path.is_a?(Path) ? path : new(path)
end

.from_names(names) ⇒ Path

Parameters:

  • names (Array<Path, String>)

Returns:



14
15
16
# File 'lib/yoda/model/path.rb', line 14

def self.from_names(names)
  new(names.join('::'))
end

Instance Method Details

#==(another) ⇒ Object



103
104
105
# File 'lib/yoda/model/path.rb', line 103

def ==(another)
  eql?(another)
end

#absolute!Path

Returns:



30
31
32
# File 'lib/yoda/model/path.rb', line 30

def absolute!
  absolute? ? self : Path.new("::#{to_s}")
end

#absolute?Boolean

Returns:

  • (Boolean)


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

def absolute?
  name.start_with?('::')
end

#absolute_pathsArray<Path>

Returns:



81
82
83
# File 'lib/yoda/model/path.rb', line 81

def absolute_paths
  [self.absolute!]
end

#basenameString

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
# File 'lib/yoda/model/path.rb', line 35

def basename
  @basename ||= begin
    if name.end_with?('::')
      ''
    else
      name.split('::').last || ''
    end
  end
end

#concat(another) ⇒ Path

Parameters:

  • another (Path, String)

Returns:



67
68
69
70
71
72
73
# File 'lib/yoda/model/path.rb', line 67

def concat(another)
  if self.class.build(another).absolute?
    self
  else
    self.class.new([self.to_s, another.to_s].reject(&:empty?).join('::'))
  end
end

#eql?(another) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/yoda/model/path.rb', line 107

def eql?(another)
  another.is_a?(Path) && name == another.name
end

#hashObject



99
100
101
# File 'lib/yoda/model/path.rb', line 99

def hash
  [self.class.name, name].hash
end

#namespacesArray<String>

Returns:

  • (Array<String>)


76
77
78
# File 'lib/yoda/model/path.rb', line 76

def namespaces
  name.split('::')
end

#parent_pathsArray<Path>

Returns:



91
92
93
94
95
96
97
# File 'lib/yoda/model/path.rb', line 91

def parent_paths
  if spacename.empty?
    []
  else
    [spacename] + Path.new(spacename).parent_paths
  end
end

#pathsArray<Path>

Returns:



86
87
88
# File 'lib/yoda/model/path.rb', line 86

def paths
  [self]
end

#spacenameString

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
# File 'lib/yoda/model/path.rb', line 46

def spacename
  @spacename ||= begin
    if name.end_with?('::')
      name.gsub(/::\Z/, '')
    else
      name.split('::').slice(0..-2).join('::')
    end
  end
end

#splitObject



61
62
63
# File 'lib/yoda/model/path.rb', line 61

def split
  name.gsub(/\A::/, '').split('::') + (name.end_with?('::') ? [''] : [])
end

#to_sString

Returns:

  • (String)


57
58
59
# File 'lib/yoda/model/path.rb', line 57

def to_s
  name
end