Module: Occi::Core::Helpers::InstanceAttributeResetter

Included in:
ActionInstance, Entity
Defined in:
lib/occi/core/helpers/instance_attribute_resetter.rb

Overview

Introduces instance attribute resetting functionality to the receiver. Provides methods for applying defaults and adding attributes on top of existing base attributes. `base_attributes` and `added_attributes` must be implemented in the receiver.

Author:

Instance Method Summary collapse

Instance Method Details

#attribute_namesArray

Collects all available attribute names into a list. Without definitions or values.

Returns:

  • (Array)

    list available attribute names


65
66
67
68
69
70
71
72
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 65

def attribute_names
  names = added_attributes.collect(&:keys)
  names << base_attributes.keys
  names.flatten!
  names.compact!

  names
end

#remove_undef_attributesHash

Removes attributes (definition and value) if they are no longer defined for this instance. This is automatically called when invoking reset via `reset_attributes` or `reset_attributes!`, in all other cases it has to be triggered explicitly. Attributes without definitions will be removed as well.

Returns:

  • (Hash)

    updated attribute hash


52
53
54
55
56
57
58
59
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 52

def remove_undef_attributes
  name_cache = attribute_names
  attributes.keep_if do |key, value|
    defined = name_cache.include?(key) && value && value.attribute_definition
    logger.debug "Removing undefined attribute #{key.inspect} on #{self.class}" unless defined
    defined
  end
end

#reset_added_attributes(force = false) ⇒ Array

Iterates over available added attribute definitions and sets corresponding fields in `attributes`. When using the `force` flag, all existing attribute values will be replaced by defaults from definitions or reset to `nil`. No longer defined attributes will be kept unchanged.

Parameters:

  • force (TrueClass, FalseClass) (defaults to: false)

    forcibly change attribute values to defaults

Returns:

  • (Array)

    list of processed attribute names


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 99

def reset_added_attributes(force = false)
  processed_attrs = []

  added_attributes.each do |attrs|
    attrs.each_pair do |name, definition|
      if processed_attrs.include?(name)
        raise Occi::Core::Errors::AttributeDefinitionError,
              "Attribute #{name.inspect} already modified by another mixin"
      end
      processed_attrs << name
      reset_attribute(name, definition, force)
    end
  end

  processed_attrs
end

#reset_added_attributes!Object

Shorthand for running `reset_added_attributes` with the `force` flag on. This method will force defaults from definitions in all available attributes. No longer defined attributes will be kept unchanged.


29
30
31
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 29

def reset_added_attributes!
  reset_added_attributes true
end

#reset_attribute(name, definition, force) ⇒ Object

Sets corresponding attribute fields in `attributes`. When using the `force` flag, any existing attribute value will be replaced by the default from its definition or reset to `nil`.

Parameters:


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 123

def reset_attribute(name, definition, force)
  if attributes[name]
    logger.debug "Setting attribute definition for existing #{name.inspect} on #{self.class}"
    attributes[name].attribute_definition = definition
  else
    logger.debug "Creating attribute definition for new #{name.inspect} on #{self.class}"
    attributes[name] = Attribute.new(nil, definition)
  end

  force ? attributes[name].default! : attributes[name].default
end

#reset_attributes(force = false) ⇒ Object

Iterates over available attribute definitions and sets corresponding fields in `attributes`. When using the `force` flag, all existing attribute values will be replaced by defaults from definitions or reset to `nil`. No longer defined attributes will be automatically removed.

Parameters:

  • force (TrueClass, FalseClass) (defaults to: false)

    forcibly change attribute values to defaults


39
40
41
42
43
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 39

def reset_attributes(force = false)
  reset_base_attributes force
  reset_added_attributes force
  remove_undef_attributes
end

#reset_attributes!Object

Shorthand for running `reset_attributes` with the `force` flag on. This method will force defaults from definitions in all available attributes. No longer defined attributes will be automatically removed.


15
16
17
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 15

def reset_attributes!
  reset_attributes true
end

#reset_base_attributes(force = false) ⇒ Array

Iterates over available base attribute definitions and sets corresponding fields in `attributes`. When using the `force` flag, all existing attribute values will be replaced by defaults from definitions or reset to `nil`. No longer defined attributes will be kept unchanged.

Parameters:

  • force (TrueClass, FalseClass) (defaults to: false)

    forcibly change attribute values to defaults

Returns:

  • (Array)

    list of processed attribute names


81
82
83
84
85
86
87
88
89
90
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 81

def reset_base_attributes(force = false)
  processed_attrs = []

  base_attributes.each_pair do |name, definition|
    processed_attrs << name
    reset_attribute(name, definition, force)
  end

  processed_attrs
end

#reset_base_attributes!Object

Shorthand for running `reset_base_attributes` with the `force` flag on. This method will force defaults from definitions in all available attributes. No longer defined attributes will be kept unchanged.


22
23
24
# File 'lib/occi/core/helpers/instance_attribute_resetter.rb', line 22

def reset_base_attributes!
  reset_base_attributes true
end