Class: Yoda::Model::Path
- Inherits:
-
Object
- Object
- Yoda::Model::Path
- Defined in:
- lib/yoda/model/path.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(another) ⇒ Object
- #absolute! ⇒ Path
- #absolute? ⇒ Boolean
- #absolute_paths ⇒ Array<Path>
- #basename ⇒ String
- #concat(another) ⇒ Path
- #eql?(another) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name) ⇒ Path
constructor
A new instance of Path.
- #namespaces ⇒ Array<String>
- #parent_paths ⇒ Array<Path>
- #paths ⇒ Array<Path>
- #spacename ⇒ String
- #split ⇒ Object
- #to_s ⇒ String
Constructor Details
#initialize(name) ⇒ Path
Returns a new instance of Path.
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
#name ⇒ Object (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
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
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
30 31 32 |
# File 'lib/yoda/model/path.rb', line 30 def absolute! absolute? ? self : Path.new("::#{to_s}") end |
#absolute? ⇒ Boolean
25 26 27 |
# File 'lib/yoda/model/path.rb', line 25 def absolute? name.start_with?('::') end |
#absolute_paths ⇒ Array<Path>
81 82 83 |
# File 'lib/yoda/model/path.rb', line 81 def absolute_paths [self.absolute!] end |
#basename ⇒ 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
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
107 108 109 |
# File 'lib/yoda/model/path.rb', line 107 def eql?(another) another.is_a?(Path) && name == another.name end |
#hash ⇒ Object
99 100 101 |
# File 'lib/yoda/model/path.rb', line 99 def hash [self.class.name, name].hash end |
#namespaces ⇒ Array<String>
76 77 78 |
# File 'lib/yoda/model/path.rb', line 76 def namespaces name.split('::') end |
#parent_paths ⇒ Array<Path>
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 |
#spacename ⇒ 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 |
#split ⇒ Object
61 62 63 |
# File 'lib/yoda/model/path.rb', line 61 def split name.gsub(/\A::/, '').split('::') + (name.end_with?('::') ? [''] : []) end |
#to_s ⇒ String
57 58 59 |
# File 'lib/yoda/model/path.rb', line 57 def to_s name end |