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



8
9
10
# File 'lib/eac_config/entry_path.rb', line 8

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

.build_parts(source) ⇒ Object



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

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



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

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



38
39
40
# File 'lib/eac_config/entry_path.rb', line 38

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

#[]Object



42
43
44
# File 'lib/eac_config/entry_path.rb', line 42

def [](*)
  slice(*)
end

#sliceObject



46
47
48
49
# File 'lib/eac_config/entry_path.rb', line 46

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

#to_sObject



51
52
53
# File 'lib/eac_config/entry_path.rb', line 51

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

#to_stringString

Returns:

  • (String)


56
57
58
# File 'lib/eac_config/entry_path.rb', line 56

def to_string
  parts.join(PART_SEPARATOR)
end

#with_first(part) ⇒ EacConfig::EntryPath

Parameters:

  • part (String)

Returns:



62
63
64
65
66
# File 'lib/eac_config/entry_path.rb', line 62

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

#with_last(part) ⇒ EacConfig::EntryPath



76
77
78
# File 'lib/eac_config/entry_path.rb', line 76

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

#without_firstEacConfig::EntryPath



69
70
71
72
73
# File 'lib/eac_config/entry_path.rb', line 69

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

#without_lastEacConfig::EntryPath



81
82
83
84
85
# File 'lib/eac_config/entry_path.rb', line 81

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