Module: Occi::Core::Helpers::MixinSelector

Included in:
Entity
Defined in:
lib/occi/core/helpers/mixin_selector.rb

Overview

Introduces mixin-based helpers to every receiver class. Provides methods to access mixin sub-sets defined by dependencies.

Author:

Instance Method Summary collapse

Instance Method Details

#dependent_term(mixin) ⇒ Object

See Also:

  • `dependent_terms`

47
48
49
# File 'lib/occi/core/helpers/mixin_selector.rb', line 47

def dependent_term(mixin)
  [select_mixin(mixin)].compact.map(&:term).first
end

#dependent_term!(mixin) ⇒ Object

See Also:

  • `dependent_term`

52
53
54
# File 'lib/occi/core/helpers/mixin_selector.rb', line 52

def dependent_term!(mixin)
  select_mixin!(mixin).term
end

#dependent_terms(mixin) ⇒ Array

Returns a list of term of mixins dependent on the given mixin.

Parameters:

Returns:

  • (Array)

    terms of mixins


42
43
44
# File 'lib/occi/core/helpers/mixin_selector.rb', line 42

def dependent_terms(mixin)
  select_mixins(mixin).map(&:term)
end

#select_mixin(filter) ⇒ Occi::Core::Mixin, NilClass

Selects at most one mixin defined by the given dependence. This dependence must be provided as `filter` and be an instance of `Occi::Core::Mixin` or its subclass.

Parameters:

Returns:


27
28
29
# File 'lib/occi/core/helpers/mixin_selector.rb', line 27

def select_mixin(filter)
  mixins.detect { |m| m.depends?(filter) }
end

#select_mixin!(filter) ⇒ Object

Selects exactly one mixin or raises an error.

See Also:

  • `select_mixin`

33
34
35
36
# File 'lib/occi/core/helpers/mixin_selector.rb', line 33

def select_mixin!(filter)
  select_mixin(filter) \
    || raise(Occi::Core::Errors::InstanceLookupError, "Mixin dependent on #{filter} not found")
end

#select_mixins(filter) ⇒ Set

Selects a set of mixins defined by a common dependence. This dependence must be provided as `filter` and be an instance of `Occi::Core::Mixin` or its subclass.

Parameters:

Returns:

  • (Set)

    found mixins


16
17
18
# File 'lib/occi/core/helpers/mixin_selector.rb', line 16

def select_mixins(filter)
  Set.new(mixins.select { |m| m.depends?(filter) })
end