Module: XmlSchema
- Defined in:
- lib/xml_schema.rb,
lib/xml_schema/version.rb
Constant Summary collapse
- TYPES =
%w(integer boolean float dateTime time date string decimal double duration gYearMonth gYear gMonthDay gDay gMonth hexBinary base64Binary anyURI QName NOTATION)
- VERSION =
"0.1.3"
Class Method Summary collapse
-
.datatype_of(object) ⇒ Object
Obtain XML Schema datatype URI for a Ruby object.
-
.instantiate(literal, datatype_uri = nil) ⇒ Object
Instantiate a Ruby object from a literal string and datatype URI.
Class Method Details
.datatype_of(object) ⇒ Object
Obtain XML Schema datatype URI for a Ruby object.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/xml_schema.rb', line 9 def self.datatype_of(object) #-- # TODO: decimal, double, duration (Range?), gYearMonth, gYear, gMonthDay, gDay, gMonth, hexBinary, base64Binary, anyURI, QName, NOTATION # TODO: language tag? #++ case object.class.name when "Fixnum", "Integer" NS::XMLSchema['integer'] when "TrueClass", "FalseClass" NS::XMLSchema['boolean'] when "Float" NS::XMLSchema['float'] when "DateTime" NS::XMLSchema['dateTime'] when "Time" NS::XMLSchema['time'] when "Date" NS::XMLSchema['date'] when "String" NS::XMLSchema['string'] else raise "#{object.class} cannot be coerced into any XMLSchema datatype" end end |
.instantiate(literal, datatype_uri = nil) ⇒ Object
Instantiate a Ruby object from a literal string and datatype URI. If datatype_uri
is not specified, literal
is interpreted as RDF typed literal (with datatype postfix).
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 74 75 |
# File 'lib/xml_schema.rb', line 37 def self.instantiate(literal, datatype_uri = nil) # NOTE: removing language tag (e.g. "@en") full_literal = datatype_uri ? literal.sub(/@\w+$/, '') + "^^<#{datatype_uri}>" : literal.sub(/@\w+$/, '') literal_value, literal_type = full_literal.split('^^') datatype = if literal_type ns, l_type = literal_type.delete("<>").split('#') # TODO: somehow use a better comparison of URIs which ignores "/" at the end? if URI(ns.sub(/\/*$/, "/")) == NS::XMLSchema.uri && TYPES.include?(l_type) l_type else raise "Incompatible datatype URI! (#{ns})" end else 'string' end # clean-up the literal_value literal_value.sub!(/^["']/, '') literal_value.sub!(/["']$/, '') case datatype when 'integer' literal_value.to_i when 'boolean' %w(1 true).include?(literal_value) when 'dateTime' DateTime.parse(literal_value) when 'date' Date.parse(literal_value) when 'time' Time.parse(literal_value) when 'float' literal_value.to_f else # FIXME: fallback for unknown datatypes and 'string' literal_value end end |