Class: Teton::Key
- Inherits:
-
Object
- Object
- Teton::Key
- Defined in:
- lib/teton/key.rb
Overview
Understands a fully-qualified path to a resource or resources.
Instance Attribute Summary collapse
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Instance Method Summary collapse
- #entry? ⇒ Boolean
-
#initialize(path, separator:) ⇒ Key
constructor
A new instance of Key.
- #last_part ⇒ Object
- #resource? ⇒ Boolean
- #to_s(suffix_keys = []) ⇒ Object
- #traverse ⇒ Object
Constructor Details
#initialize(path, separator:) ⇒ Key
Returns a new instance of Key.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/teton/key.rb', line 10 def initialize(path, separator:) raise ArgumentError, 'separator is required' if separator.to_s.empty? raise ArgumentError, 'path is required' if path.to_s.empty? @path = path.to_s @parts = path.to_s.split(separator) @separator = separator.to_s freeze end |
Instance Attribute Details
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
8 9 10 |
# File 'lib/teton/key.rb', line 8 def parts @parts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/teton/key.rb', line 8 def path @path end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
8 9 10 |
# File 'lib/teton/key.rb', line 8 def separator @separator end |
Instance Method Details
#entry? ⇒ Boolean
33 34 35 |
# File 'lib/teton/key.rb', line 33 def entry? parts.length.even? end |
#last_part ⇒ Object
41 42 43 |
# File 'lib/teton/key.rb', line 41 def last_part parts.last end |
#resource? ⇒ Boolean
37 38 39 |
# File 'lib/teton/key.rb', line 37 def resource? parts.length.odd? end |
#to_s(suffix_keys = []) ⇒ Object
21 22 23 24 25 |
# File 'lib/teton/key.rb', line 21 def to_s(suffix_keys = []) suffix_keys = Array(suffix_keys) (parts + suffix_keys).join(separator) end |
#traverse ⇒ Object
27 28 29 30 31 |
# File 'lib/teton/key.rb', line 27 def traverse parts.each_with_index do |_part, index| yield KeyPointer.new(self, index) end end |