Class: VirtualAssembly::Semantizer::HashSerializer
- Inherits:
-
Object
- Object
- VirtualAssembly::Semantizer::HashSerializer
- Defined in:
- lib/virtual_assembly/semantizer/hash_serializer.rb
Overview
The HashSerializer turns all the semantic properties of a SemanticObject into a Hash.
Lets consider the following SemanticObject with a single semantic property like:
-
name: “xmlns.com/foaf/0.1/name”
-
value: “John”
The HashSerializer will return the following Hash: href="http://xmlns.com/foaf/0.1/name">xmlns.com/foaf/0.1/name” => “John”.
Instance Method Summary collapse
-
#process(subject) ⇒ Object
This is the main method to begin the serialization.
Instance Method Details
#process(subject) ⇒ Object
This is the main method to begin the serialization.
The subject should be a SemanticObject.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/virtual_assembly/semantizer/hash_serializer.rb', line 31 def process(subject) result = {} # We iterate over all the semantic properties of the subject. subject.semanticProperties.each do |property| name = property.name value = property.value if (value == nil) next end # In case the property is a primitive, we simply get its value. if ([ String, Integer, Float, TrueClass, FalseClass ].include?(value.class)) result[name] = value # In case the property is a SemanticObject, we get its semanticId # or we process it if it is a blank node. elsif (value.class < VirtualAssembly::Semantizer::SemanticObject) if (value.isBlankNode?) result[name] = process(value) else result[name] = value.semanticId end # In case the property is an Array, we export each item. elsif (value.class == Array) result[name] = exportCollection(value) else raise "The type of the property '" + name + "' is '" + value.class.to_s + "' but a primitive, a SemanticObject or an Array is required." end end return result; end |