Class: Occi::Core::InstanceBuilder
- Inherits:
-
Object
- Object
- Occi::Core::InstanceBuilder
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#model ⇒ Occi::Core::Model
model filled with known category definitions.
Class Method Summary collapse
-
.klass_map ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#build(identifier, args = {}) ⇒ Object, NilClass
(also: #get)
Constructs an instance based on the given category identifier.
-
#build_link(identifier, args = {}) ⇒ Object, NilClass
Constructs an instance based on the given category identifier.
-
#build_resource(identifier, args = {}) ⇒ Object, NilClass
Constructs an instance based on the given category identifier.
-
#initialize(args = {}) ⇒ InstanceBuilder
constructor
Constructs and instance of the InstanceBuilder.
-
#kind_instance(identifier) ⇒ Occi::Core::Kind
Looks up the given identifier in the model.
-
#klass(identifier, known_ancestor) ⇒ Class
Looks up the appropriate candidate class for the given identifier.
-
#parent_klass(kind) ⇒ Class
Locates the closes known parent class for instances of the given kind.
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.
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
#model ⇒ Occi::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_map ⇒ Object
: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.
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 |
#build_link(identifier, args = {}) ⇒ Object, NilClass
Constructs an instance based on the given category identifier. This method can construct instances of Link sub-types.
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.
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. kind_instance(Occi::Core::Constants::RESOURCE_KIND) logger.debug { "Identified #{kind.identifier} as Resource" } Occi::Core::Resource elsif kind. 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 |