Class: Builderator::Config::List

Inherits:
Array
  • Object
show all
Defined in:
lib/builderator/config/list.rb

Overview

Extend Array with context about how its values should be merged with other configuration layers. Possible modes are:

  • ‘override’ - Do not merge. Replace the other node’s elements

  • ‘union’ - Perform a set-union on the elements of this and the other node

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from = nil, **options) ⇒ List

Returns a new instance of List.



23
24
25
26
27
# File 'lib/builderator/config/list.rb', line 23

def initialize(from = nil, **options)
  @mode = options.fetch(:mode, :union)

  merge!(from) unless from.nil?
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



21
22
23
# File 'lib/builderator/config/list.rb', line 21

def mode
  @mode
end

Class Method Details

.coerce(somehting, options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/builderator/config/list.rb', line 12

def coerce(somehting, options = {})
  return somehting if somehting.is_a?(self)
  return new(options).push(*somehting) if somehting.is_a?(Array)

  ## `somehting` is not a valid input. Just give back an instance.
  new([], options)
end

Instance Method Details

#cloneObject



29
30
31
# File 'lib/builderator/config/list.rb', line 29

def clone
  self.class.new(self, :mode => mode)
end

#merge!(other) ⇒ Object

Combine elements with ‘other` according to `other`’s ‘mode`



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/builderator/config/list.rb', line 41

def merge!(other)
  other = self.class.coerce(other)

  case other.mode
  when :override
    return false if self == other
    set(*other)

  when :union
    merged = self | other
    return false if merged == self

    set(*merged)

  else
    fail "Invalid List mode #{other.mode}!"
  end

  true
end

#set(*elements) ⇒ Object



33
34
35
36
# File 'lib/builderator/config/list.rb', line 33

def set(*elements)
  clear
  push(*elements)
end