Class: Occi::Core::InstanceBuilder

Inherits:
Object
  • Object
show all
Includes:
Helpers::ArgumentValidator, Yell::Loggable
Defined in:
lib/occi/core/instance_builder.rb

Overview

Provides mechanisms for easy instantiation of various Entity sub-types such as Resource or Link (and their sub-types). Unknown sub-types will result in generic `Occi::Core::Resource` and `Occi::Core::Link` instances. Known (pre-defined) sub-types will be provided as instances of unique classes inheriting from the abovementioned.

Author:

Direct Known Subclasses

Infrastructure::InstanceBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ InstanceBuilder

Constructs and instance of the InstanceBuilder. It can be used to quickly and easily get get instances of various `Occi::Core::Entity` sub-types based on their kind identifier.

Options Hash (args):


24
25
26
27
28
29
30
31
# File 'lib/occi/core/instance_builder.rb', line 24

def initialize(args = {})
  pre_initialize(args)
  default_args! args

  @model = args.fetch(:model)

  post_initialize(args)
end

Instance Attribute Details

#modelOcci::Core::Model

model filled with known category definitions


13
14
15
# File 'lib/occi/core/instance_builder.rb', line 13

def model
  @model
end

Class Method Details

.klass_mapObject

:nodoc:


135
136
137
# File 'lib/occi/core/instance_builder.rb', line 135

def klass_map
  {}
end

Instance Method Details

#build(identifier, args = {}) ⇒ Object, NilClass Also known as: get

Constructs an instance based on the given category identifier. This method can construct instances of Entity sub-types.

Examples:

build 'http://schemas.ogf.org/occi/core#resource'
  # => Occi::Core::Resource

44
45
46
47
48
# File 'lib/occi/core/instance_builder.rb', line 44

def build(identifier, args = {})
  logger.debug "Building instance of #{identifier.inspect} with #{args.inspect}"
  k_args = args_with_kind(identifier, args)
  klass(identifier, parent_klass(k_args[:kind])).new k_args
end

Constructs an instance based on the given category identifier. This method can construct instances of Link sub-types.

Examples:

build_link 'http://schemas.ogf.org/occi/core#link' # => Occi::Core::Link

75
76
77
# File 'lib/occi/core/instance_builder.rb', line 75

def build_link(identifier, args = {})
  klass(identifier, Occi::Core::Link).new args_with_kind(identifier, args)
end

#build_resource(identifier, args = {}) ⇒ Object, NilClass

Constructs an instance based on the given category identifier. This method can construct instances of Resource sub-types.

Examples:

build_resource 'http://schemas.ogf.org/occi/core#resource' # => Occi::Core::Resource

61
62
63
# File 'lib/occi/core/instance_builder.rb', line 61

def build_resource(identifier, args = {})
  klass(identifier, Occi::Core::Resource).new args_with_kind(identifier, args)
end

#kind_instance(identifier) ⇒ Occi::Core::Kind

Looks up the given identifier in the model. Returns `Occi::Core::Kind` instance if found and raises an error otherwise. Look-up results not related to `Occi::Core::Kind` will also raise an error.


106
107
108
109
110
111
112
113
# File 'lib/occi/core/instance_builder.rb', line 106

def kind_instance(identifier)
  kind = model.find_by_identifier!(identifier)
  unless kind.is_a? Occi::Core::Kind
    raise Occi::Core::Errors::CategoryValidationError, "#{identifier.inspect} " \
          'is not a kind'
  end
  kind
end

#klass(identifier, known_ancestor) ⇒ Class

Looks up the appropriate candidate class for the given identifier. If no class is found in static tables, the last known ancestor is returned. For Core, this method ALWAYS returns the last known ancestor given as `known_ancestor`, for compatibility reasons.


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/occi/core/instance_builder.rb', line 87

def klass(identifier, known_ancestor)
  found_klass = self.class.klass_map[identifier]
  return known_ancestor unless found_klass

  unless found_klass.ancestors.include?(known_ancestor)
    raise Occi::Core::Errors::InstanceValidationError,
          "#{found_klass} is not a sub-type of #{known_ancestor}"
  end

  logger.debug "Found class #{found_klass} for #{identifier.inspect}"
  found_klass
end

#parent_klass(kind) ⇒ Class

Locates the closes known parent class for instances of the given kind. This usually means `Occi::Core::Resource`, `Occi::Core::Link`, or error.


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/occi/core/instance_builder.rb', line 120

def parent_klass(kind)
  if kind.related? kind_instance(Occi::Core::Constants::RESOURCE_KIND)
    logger.debug "Identified #{kind.identifier} as Resource"
    Occi::Core::Resource
  elsif kind.related? kind_instance(Occi::Core::Constants::LINK_KIND)
    logger.debug "Identified #{kind.identifier} as Link"
    Occi::Core::Link
  else
    raise Occi::Core::Errors::ModelLookupError,
          "Could not identify #{kind.identifier} as a Link or Resource"
  end
end