Module: Occi::Core::Helpers::ParserDereferencer

Included in:
Parsers::Json::Category, Parsers::Text::Category
Defined in:
lib/occi/core/helpers/parser_dereferencer.rb

Overview

Introduces dereferencing capabilities to various parsers. This allowes parsers to convert `Occi::Core::Category` sub-types from identifier into proper objects.

Author:

Instance Method Summary collapse

Instance Method Details

#dereference_identifiers!(derefd, parsed) ⇒ Array

Dereferences cross-category references. Takes existing string identifiers for actions, mixins, and kinds and replaces them with actual instances.


16
17
18
19
20
21
22
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 16

def dereference_identifiers!(derefd, parsed)
  derefd.each do |cat|
    next if cat.is_a?(Occi::Core::Action) # nothing to do here
    lookup_references!(cat, derefd, parsed)
  end
  derefd
end

#first_or_die(where, what) ⇒ Object


87
88
89
90
91
92
93
94
95
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 87

def first_or_die(where, what)
  logger.debug "Looking for #{what.inspect} in #{where.class}"
  found = where.detect { |elm| elm.identifier == what }
  unless found
    raise Occi::Core::Errors::ParsingError,
          "Category #{what.to_s.inspect} referenced but not provided"
  end
  found
end

#lookup_action_references!(cat, derefd, parsed_actions) ⇒ Object


46
47
48
49
50
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 46

def lookup_action_references!(cat, derefd, parsed_actions)
  logger.debug "Dereferencing actions #{parsed_actions.inspect} for #{cat.identifier.inspect}"
  return if parsed_actions.blank?
  parsed_actions.each { |action| cat.actions << first_or_die(derefd, action) }
end

#lookup_applies_references!(_mixin, _derefd, _parsed_rel) ⇒ Object


73
74
75
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 73

def lookup_applies_references!(_mixin, _derefd, _parsed_rel)
  raise Occi::Core::Errors::ParserError, 'Must be implemented in the parser class'
end

#lookup_depends_references!(_mixin, _derefd, _parsed_rel) ⇒ Object


80
81
82
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 80

def lookup_depends_references!(_mixin, _derefd, _parsed_rel)
  raise Occi::Core::Errors::ParserError, 'Must be implemented in the parser class'
end

#lookup_parent_references!(kind, derefd, parsed_rel) ⇒ Object


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

def lookup_parent_references!(kind, derefd, parsed_rel)
  return if parsed_rel.blank? || kind.parent.is_a?(Occi::Core::Kind)
  logger.debug "Dereferencing parent #{parsed_rel.inspect} for #{kind.identifier.inspect}"
  if parsed_rel.is_a?(Enumerable)
    if parsed_rel.count > 1
      raise Occi::Core::Errors::ParsingError,
            "Kind #{kind} with multiple parents #{parsed_rel.inspect}"
    end
    parsed_rel = parsed_rel.first
  end

  kind.parent = first_or_die(derefd, parsed_rel)
  kind.send(:load_parent_attributes!) # this is safe because there was no previous parent!
end

#lookup_references!(cat, derefd, parsed) ⇒ Object

Looks up inter-category references and replaces them with existing objects.


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/occi/core/helpers/parser_dereferencer.rb', line 29

def lookup_references!(cat, derefd, parsed)
  parsed_cat = parsed.detect { |pcat| "#{pcat[:scheme]}#{pcat[:term]}" == cat.identifier }
  raise Occi::Core::Errors::ParsingError, "Category #{cat.identifier} not in the model" unless parsed_cat
  lookup_action_references!(cat, derefd, parsed_cat[:actions])

  if cat.is_a?(Occi::Core::Mixin)
    lookup_depends_references!(cat, derefd, parsed_cat[self::DEPENDS_KEY])
    lookup_applies_references!(cat, derefd, parsed_cat[self::APPLIES_KEY])
  else
    # only Occi::Core::Kind is left here
    lookup_parent_references!(cat, derefd, parsed_cat[self::PARENT_KEY])
  end
end