Class: EacConfig::EntryPath

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_config/entry_path.rb

Constant Summary collapse

PART_SEPARATOR =
'.'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert(source) ⇒ Object



10
11
12
# File 'lib/eac_config/entry_path.rb', line 10

def assert(source)
  source.is_a?(self) ? source : new(build_parts(source))
end

.build_parts(source) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/eac_config/entry_path.rb', line 14

def build_parts(source)
  return validate_parts!(source.split(PART_SEPARATOR)) if source.is_a?(::String)
  return validate_parts!(source.flat_map { |part| build_parts(part) }) if
  source.is_a?(::Enumerable)

  build_parts(source.to_s)
end

.validate_parts!(parts) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/eac_config/entry_path.rb', line 22

def validate_parts!(parts)
  parts.assert_argument(::Enumerable, 'parts')
  parts.each_with_index do |part, index|
    raise ::ArgumentError, "Part #{index} of #{parts} is blank" if part.blank?
    raise ::ArgumentError, "Part #{index} is not a string" unless part.is_a?(::String)
  end
end

Instance Method Details

#+(other) ⇒ EacConfig::EntryPath



40
41
42
# File 'lib/eac_config/entry_path.rb', line 40

def +(other)
  self.class.new(parts + self.class.assert(other).parts)
end

#[](*args) ⇒ Object



44
45
46
# File 'lib/eac_config/entry_path.rb', line 44

def [](*args)
  slice(*args)
end

#slice(*args) ⇒ Object



48
49
50
51
# File 'lib/eac_config/entry_path.rb', line 48

def slice(*args)
  r = parts.slice(*args)
  r.is_a?(::Enumerable) ? self.class.new(r) : r
end

#to_sObject



53
54
55
# File 'lib/eac_config/entry_path.rb', line 53

def to_s
  "#{self.class}[#{to_string}]"
end

#to_stringString

Returns:

  • (String)


58
59
60
# File 'lib/eac_config/entry_path.rb', line 58

def to_string
  parts.join(PART_SEPARATOR)
end

#with_first(part) ⇒ EacConfig::EntryPath

Parameters:

  • part (String)

Returns:



64
65
66
67
68
# File 'lib/eac_config/entry_path.rb', line 64

def with_first(part)
  new_parts = parts.dup
  new_parts.unshift(part)
  self.class.new(new_parts)
end

#with_last(part) ⇒ EacConfig::EntryPath



78
79
80
# File 'lib/eac_config/entry_path.rb', line 78

def with_last(part)
  self.class.new(parts + [part])
end

#without_firstEacConfig::EntryPath



71
72
73
74
75
# File 'lib/eac_config/entry_path.rb', line 71

def without_first
  new_parts = parts.dup
  new_parts.shift
  self.class.new(new_parts)
end

#without_lastEacConfig::EntryPath



83
84
85
86
87
# File 'lib/eac_config/entry_path.rb', line 83

def without_last
  new_parts = parts.dup
  new_parts.pop
  self.class.new(new_parts)
end