Class: Teton::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/teton/key.rb

Overview

Understands a fully-qualified path to a resource or resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, separator:) ⇒ Key

Returns a new instance of Key.

Raises:

  • (ArgumentError)


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

#partsObject (readonly)

Returns the value of attribute parts.



8
9
10
# File 'lib/teton/key.rb', line 8

def parts
  @parts
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/teton/key.rb', line 8

def path
  @path
end

#separatorObject (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/teton/key.rb', line 33

def entry?
  parts.length.even?
end

#last_partObject



41
42
43
# File 'lib/teton/key.rb', line 41

def last_part
  parts.last
end

#resource?Boolean

Returns:

  • (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

#traverseObject



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