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



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

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

#absolute?Boolean

Returns:

  • (Boolean)


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

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

#basenameString

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
# File 'lib/yoda/model/path.rb', line 29

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

#concat(another) ⇒ Path

Parameters:

  • another (Path, String)

Returns:



61
62
63
64
65
66
67
# File 'lib/yoda/model/path.rb', line 61

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)


91
92
93
# File 'lib/yoda/model/path.rb', line 91

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

#hashObject



83
84
85
# File 'lib/yoda/model/path.rb', line 83

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

#namespacesArray<String>

Returns:

  • (Array<String>)


70
71
72
# File 'lib/yoda/model/path.rb', line 70

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

#parent_pathsArray<Path>

Returns:



75
76
77
78
79
80
81
# File 'lib/yoda/model/path.rb', line 75

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

#spacenameString

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
# File 'lib/yoda/model/path.rb', line 40

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

#splitObject



55
56
57
# File 'lib/yoda/model/path.rb', line 55

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

#to_sString

Returns:

  • (String)


51
52
53
# File 'lib/yoda/model/path.rb', line 51

def to_s
  name
end