Class: BlueprintConfig::OptionsArray

Inherits:
Array
  • Object
show all
Defined in:
lib/blueprint_config/options_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = [], path: nil, source: nil) ⇒ OptionsArray

Returns a new instance of OptionsArray.



7
8
9
10
11
12
13
14
15
# File 'lib/blueprint_config/options_array.rb', line 7

def initialize(options = [], path: nil, source: nil)
  super() # important - brackets needed to create an empty array
  @__path = path
  @__sources = []
  @__indeces = []
  options.each do |elem|
    __push(elem, source:)
  end
end

Instance Attribute Details

#__indecesObject

Returns the value of attribute __indeces.



5
6
7
# File 'lib/blueprint_config/options_array.rb', line 5

def __indeces
  @__indeces
end

#__pathObject

Returns the value of attribute __path.



5
6
7
# File 'lib/blueprint_config/options_array.rb', line 5

def __path
  @__path
end

#__sourcesObject

Returns the value of attribute __sources.



5
6
7
# File 'lib/blueprint_config/options_array.rb', line 5

def __sources
  @__sources
end

Instance Method Details

#__assign(other) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blueprint_config/options_array.rb', line 17

def __assign(other)
  if other.first.to_s == '__append'
    concat other[1..]
    @__sources.concat other.__sources[1..]
    @__indeces.concat other.__indeces[1..]
  else
    clear
    concat(other)
    __sources.clear
    __sources.concat(other.__sources)
    __indeces.clear
    __indeces.concat(other.__indeces)
  end
  self
end

#__push(elem, source: nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/blueprint_config/options_array.rb', line 42

def __push(elem, source: nil)
  elem = OptionsHash.new(elem, path: [@__path, size].compact.join('.'), source:) if elem.is_a?(Hash)
  elem = self.class.new(elem, path: [@__path, size].compact.join('.'), source:) if elem.is_a?(Array)
  @__sources.push source
  @__indeces.push size
  push elem
end

#dig(key, *identifiers) ⇒ Object



33
34
35
# File 'lib/blueprint_config/options_array.rb', line 33

def dig(key, *identifiers)
  super(key, *identifiers)
end

#dig!(*args, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/blueprint_config/options_array.rb', line 64

def dig!(*args, &block)
  leading = args.shift
  if args.empty?
    fetch(leading, &block)
  else
    fetch(leading, &block).dig!(*args, &block)
  end
rescue IndexError => e
  raise e, e.message, caller[1..], cause: nil
end

#fetch(index, *args, &block) ⇒ Object



75
76
77
78
79
# File 'lib/blueprint_config/options_array.rb', line 75

def fetch(index, *args, &block)
  super(index, *args, &block)
rescue IndexError
  raise IndexError, "Configuration key '#{[@__path, index].compact.join('.')}' is not set", caller[1..], cause: nil
end

#merge!(other, &block) ⇒ Object



37
38
39
40
# File 'lib/blueprint_config/options_array.rb', line 37

def merge!(other, &block)
  @__sources.reverse_merge!(other.__sources) if other.is_a?(OptionsHash)
  super
end

#source(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/blueprint_config/options_array.rb', line 50

def source(*args)
  index = args.shift
  if index >= size
    raise IndexError, "Configuration key '#{[@__path, index].compact.join('.')}' is not set", caller[1..],
          cause: nil
  end

  if args.empty?
    "#{@__sources[index]} #{[@__path, @__indeces[index]].compact.join('.')}"
  else
    self[index].source(*args)
  end
end