class VagrantCloud::Data::Mutable

Mutable data class

Public Class Methods

attr_mutable(*args) click to toggle source

Define attributes which are mutable

# File lib/vagrant_cloud/data.rb, line 187
def self.attr_mutable(*args)
  sync do
    args.each do |attr|
      if !attr_required.include?(attr.to_sym) && !attr_optional.include?(attr.to_sym)
        raise ArgumentError, "Unknown attribute name provided `#{attr}`"
      end
      define_method("#{attr}=") { |v| dirty[attr.to_sym] = v }
    end
  end
end
load(options={}) click to toggle source

Load data and create a new instance

@param [Hash] options Value to initialize instance @return [Mutable]

# File lib/vagrant_cloud/data.rb, line 202
def self.load(options={})
  opts = {}.tap do |o|
    (attr_required + attr_optional +
      self.instance_method(:initialize).parameters.find_all { |i|
      i.first == :key || i.first == :keyreq
    }.map(&:last)).each do |k|
      o[k.to_sym] = options[k.to_sym]
    end
  end
  self.new(**opts)
end
new(**opts) click to toggle source

Create a new instance

@return [Mutable]

Calls superclass method VagrantCloud::Data::Immutable::new
# File lib/vagrant_cloud/data.rb, line 217
def initialize(**opts)
  super
  @dirty = {}
end

Public Instance Methods

[](k) click to toggle source

Fetch value from data

@param [String, Symbol] k Name of value @return [Object]

Calls superclass method VagrantCloud::Data#[]
# File lib/vagrant_cloud/data.rb, line 226
def [](k)
  if dirty?(k)
    @dirty[k.to_sym]
  else
    super
  end
end
clean(data:, ignores: [], only: []) click to toggle source

Load given data and ignore any fields that are provided. Flush dirty state.

@param [Hash] data Attribute data to load @param [Array<Symbol>] ignores Fields to skip @param [Array<Symbol>] only Fields to update @return [self]

# File lib/vagrant_cloud/data.rb, line 254
def clean(data:, ignores: [], only: [])
  raise TypeError, "Expected type `Hash` but received `#{data.inspect}`" if
    !data.is_a?(Hash)
  new_data = @data.dup
  ignores = Array(ignores).map(&:to_sym)
  only = Array(only).map(&:to_sym)
  data.each do |k, v|
    k = k.to_sym
    next if ignores.include?(k)
    next if !only.empty? && !only.include?(k)
    if self.respond_to?(k)
      new_data[k] = v
      @dirty.delete(k)
    end
  end
  @data = freezer(new_data)
  self
end
clean!() click to toggle source

Merge values from dirty cache into data

@return [self]

# File lib/vagrant_cloud/data.rb, line 276
def clean!
  @data = freezer(@data.merge(@dirty))
  @dirty.clear
  self
end
dirty?(key=nil, **opts) click to toggle source

Check if instance is dirty or specific attribute if key is provided

@param [Symbol] key Key to check @return [Boolean] instance is dirty

# File lib/vagrant_cloud/data.rb, line 239
def dirty?(key=nil, **opts)
  if key.nil?
    !@dirty.empty?
  else
    @dirty.key?(key.to_sym)
  end
end
freeze() click to toggle source

@return [self] disable freezing

# File lib/vagrant_cloud/data.rb, line 283
def freeze
  self
end

Protected Instance Methods

dirty() click to toggle source

@return [Hash] updated attributes

# File lib/vagrant_cloud/data.rb, line 288
def dirty; @dirty; end