Class: EacConfig::EntryPath

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

Constant Summary collapse

PART_SEPARATOR =
'.'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ EntryPath

Returns a new instance of EntryPath.



33
34
35
# File 'lib/eac_config/entry_path.rb', line 33

def initialize(parts)
  @parts = parts.to_a.freeze
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



31
32
33
# File 'lib/eac_config/entry_path.rb', line 31

def parts
  @parts
end

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

#to_sObject



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

def to_s
  "#{self.class}[#{parts.join(PART_SEPARATOR)}]"
end