Module: KubeDSL::ValueFields::InstanceMethods

Defined in:
lib/kube-dsl/value_fields.rb

Instance Method Summary collapse

Instance Method Details

#merge(other, fields) ⇒ Object



156
157
158
159
160
161
# File 'lib/kube-dsl/value_fields.rb', line 156

def merge(other, fields)
  self.class.new.tap do |result|
    result.merge!(self)
    result.merge!(other, fields: fields)
  end
end

#merge!(other, fields: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kube-dsl/value_fields.rb', line 122

def merge!(other, fields: nil)
  unless other.is_a?(self.class)
    raise ClassMismatchError, 'cannot merge two objects of '\
      "different types, namely '#{self.class}' and '#{other.class}'"
  end

  (self.class.__fields__[:value] + self.class.__fields__[:object]).each do |field|
    next if fields && !fields.include?(field)
    ivar = :"@#{field}"

    if other_val = other.instance_variable_get(ivar)
      instance_variable_set(ivar, other_val)
    end
  end

  self.class.__fields__[:key_value].each do |field|
    next if fields && !fields.include?(field)
    send(field).merge!(other.send(field))
  end

  self.class.__fields__[:array].each do |af|
    next if fields && !fields.include?(af[:field])
    ivar = :"@#{af[:accessor]}"

    arrs = instance_variable_get(ivar) || {}

    arrs.merge!(
      other.instance_variable_get(ivar) || {}
    )

    instance_variable_set(ivar, arrs)
  end
end