Module: Occi::Core::Helpers::HashDereferencer

Included in:
Hash
Defined in:
lib/occi/core/helpers/hash_dereferencer.rb

Overview

Introduces hash dereferencing capabilities to ‘Hash`. This allowes hash instances containing `Occi::Core::Category` sub-types by identifier and `Occi::Core::AttributeDefinition` instances by name to be converted into proper objects.

Author:

Instance Method Summary collapse

Instance Method Details

#dereference_action_with!(_model, _attribute_definitions) ⇒ Integer

Replaces all hashes with attribute definitions with valid instnaces of ‘Occi::Core::AttributeDefinition`.

Parameters:

  • model (Occi::Core::Model)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



95
96
97
98
99
100
101
102
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 95

def dereference_action_with!(_model, _attribute_definitions)
  return 0 if self[:attributes].blank?
  # TODO: handle attributes referenced by name only
  self[:attributes].each_pair do |key, val|
    self[:attributes][key] = Occi::Core::AttributeDefinition.new(val.symbolize_keys)
  end
  self[:attributes].count
end

#dereference_attribute_definitions_with!(attribute_definitions) ⇒ Integer

Replaces all name-only references to existing attribute definitions with actual instances from from the given hash.

Parameters:

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 56

def dereference_attribute_definitions_with!(attribute_definitions)
  return 0 if self[:attributes].blank?

  new_attributes = {}
  self[:attributes].each do |attribute|
    new_attributes[attribute] = dereference_via_hash(attribute, attribute_definitions)
    next unless fetch(:attribute_defaults, {})[attribute]
    new_attributes[attribute].default = self[:attribute_defaults][attribute]
  end
  self[:attributes] = new_attributes

  self[:attributes].count
end

#dereference_category_with!(model, attribute_definitions) ⇒ Integer

Replaces all references to existing categories with actual instances from from the given model. Similar processing is done on attribute definitions referenced by attribute names.

Parameters:

  • model (Occi::Core::Model)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 38

def dereference_category_with!(model, attribute_definitions)
  changed = 0

  unless self[:actions].blank?
    self[:actions].map! { |action| dereference_via_model(action, model) }
    changed += self[:actions].count
  end
  self[:actions] = Set.new(self[:actions])
  changed += dereference_attribute_definitions_with!(attribute_definitions)

  changed
end

#dereference_kind_with!(model, attribute_definitions) ⇒ Integer

Replaces all references to existing categories with actual instances from from the given model. Similar processing is done on attribute definitions referenced by attribute names.

Parameters:

  • model (Occi::Core::Model)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 77

def dereference_kind_with!(model, attribute_definitions)
  changed = 0

  if self[:parent]
    self[:parent] = dereference_via_model(self[:parent], model)
    changed += 1
  end
  changed += dereference_category_with!(model, attribute_definitions)

  changed
end

#dereference_mixin_with!(model, attribute_definitions) ⇒ Integer

Replaces all references to existing categories with actual instances from from the given model. Similar processing is done on attribute definitions referenced by attribute names.

Parameters:

  • model (Occi::Core::Model)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 111

def dereference_mixin_with!(model, attribute_definitions)
  changed = 0

  %i[depends applies].each do |symbol|
    unless self[symbol].blank?
      self[symbol].map! { |elm| dereference_via_model(elm, model) }
      changed += self[symbol].count
    end
    self[symbol] = Set.new(self[symbol])
  end
  changed += dereference_category_with!(model, attribute_definitions)

  changed
end

#dereference_with!(klass, model, attribute_definitions) ⇒ Integer

Replaces all references to existing categories with actual instances from from the given model. Similar processing is done on attribute definitions referenced by attribute names.

Parameters:

  • klass (Class)

    klass serialized in this hash

  • model (Occi::Core::Model)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash)

    hash with known attribute definitions for dereferencing

Returns:

  • (Integer)

    number of changes made when dereferencing



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/occi/core/helpers/hash_dereferencer.rb', line 19

def dereference_with!(klass, model, attribute_definitions)
  unless model && attribute_definitions
    raise 'Both `model` and `attribute_definitions` are ' \
          'required for dereferencing'
  end

  send(
    "dereference_#{klass.to_s.demodulize.downcase}_with!",
    model, attribute_definitions
  )
end