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
-
#initialize(inputContext = nil) ⇒ HashSerializer
constructor
The inputContext must be a hash like { prefix => uri }.
-
#process(subject) ⇒ Object
This is the main method to begin the serialization.
Constructor Details
#initialize(inputContext = nil) ⇒ HashSerializer
The inputContext must be a hash like { prefix => uri }
29 30 31 |
# File 'lib/virtual_assembly/semantizer/hash_serializer.rb', line 29 def initialize(inputContext = nil) @inputContext = inputContext end |
Instance Method Details
#process(subject) ⇒ Object
This is the main method to begin the serialization.
The subject should be a SemanticObject.
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 67 68 69 70 71 72 73 |
# File 'lib/virtual_assembly/semantizer/hash_serializer.rb', line 36 def process(subject) result = {} # We iterate over all the semantic properties of the subject. subject.semanticProperties.each do |property| name = @inputContext? self.prefixName(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) if (!value.empty?) result[name] = exportCollection(value) end 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 |