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? ⇒ Boolean
- #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>
- #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
87 88 89 |
# File 'lib/yoda/model/path.rb', line 87 def ==(another) eql?(another) end |
#absolute? ⇒ Boolean
24 25 26 |
# File 'lib/yoda/model/path.rb', line 24 def absolute? name.start_with?('::') end |
#basename ⇒ 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
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
91 92 93 |
# File 'lib/yoda/model/path.rb', line 91 def eql?(another) another.is_a?(Path) && name == another.name end |
#hash ⇒ Object
83 84 85 |
# File 'lib/yoda/model/path.rb', line 83 def hash [self.class.name, name].hash end |
#namespaces ⇒ Array<String>
70 71 72 |
# File 'lib/yoda/model/path.rb', line 70 def namespaces name.split('::') end |
#parent_paths ⇒ Array<Path>
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 |
#spacename ⇒ 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 |
#split ⇒ Object
55 56 57 |
# File 'lib/yoda/model/path.rb', line 55 def split name.gsub(/\A::/, '').split('::') + (name.end_with?('::') ? [''] : []) end |
#to_s ⇒ String
51 52 53 |
# File 'lib/yoda/model/path.rb', line 51 def to_s name end |