A collection of node-utility functions
Pattern to match ISO8601 durations
Conversion registry defaults for RDF literal -> Ruby object conversion
Conversion registry defaults for Ruby object -> RDF node conversion
Clear the datatype registries of all but the default conversions.
# File lib/redleaf/utils.rb, line 115 115: def clear_custom_types 116: @@typeuri_registry.replace( DEFAULT_TYPEURI_REGISTRY ) 117: @@class_registry.replace( DEFAULT_CLASS_REGISTRY ) 118: end
Convert the specified Ruby object to a typed literal and return it as a tuple of the form:
[ <canonical_string_value>, <datatype_uri> ]
# File lib/redleaf/utils.rb, line 124 124: def make_object_typed_literal( object ) 125: Redleaf.logger.debug "Making typed literal from object %p" % [ object ] 126: 127: if entry = @@class_registry[ object.class ] 128: uri, converter = *entry 129: Redleaf.logger.debug " literal type URI is: %p" % [ uri ] 130: if converter.is_a?( Symbol ) 131: Redleaf.logger.debug " converter is %p#%s" % [ object.class, converter ] 132: 133: literal = object.__send__(converter) 134: Redleaf.logger.debug " converted to: %p" % [ literal ] 135: return [ literal, uri ] 136: else 137: Redleaf.logger.debug " converter is %p" % [ converter ] 138: literal = converter[object] 139: Redleaf.logger.debug " converted to: %p" % [ literal ] 140: return [ literal, uri ] 141: end 142: else 143: raise "no typed-literal conversion for %p objects" % [ object.class ] 144: end 145: end
Transform the given string_value into a Ruby object based on the datatype in typeuri.
# File lib/redleaf/utils.rb, line 151 151: def make_typed_literal_object( typeuri, string_value ) 152: unless typeuri.is_a?( URI ) 153: Redleaf.logger.debug "Converting typeuri %p to a URI object" % [ typeuri ] 154: typeuri = URI( typeuri ) 155: end 156: 157: Redleaf.logger.debug "Making Ruby object from typed literal %p<%s>" % 158: [ string_value, typeuri ] 159: 160: if converter = @@typeuri_registry[ typeuri ] 161: Redleaf.logger.debug " casting function is: %p" % [ converter ] 162: return converter[ string_value ] 163: else 164: Redleaf.logger.error " I only know about the following typeuris: %p" % 165: [ @@typeuri_registry.keys ] 166: raise "No object conversion for typed literal %p (%p)" % [ string_value, typeuri ] 167: end 168: end
Parse the given string containing an ISO8601 duration and return it as a Hash. Returns nil if the string doesn’t appear to contain a valid duration.
# File lib/redleaf/utils.rb, line 41 41: def parse_iso8601_duration( string ) 42: match = ISO8601_DURATION_PATTERN.match( string ) or 43: raise TypeError, "Invalid ISO8601 date %p" % [ string ] 44: 45: sign = (match[1] == '-' ? -1 : 1) 46: Redleaf.logger.debug "Got sign %p (%p)" % [ match[1], sign ] 47: years, months, days, hours, minutes = 48: match.captures[1..-2].collect {|s| s.to_i * sign } 49: seconds = match.captures.last.to_f * sign 50: 51: return { 52: :years => years, 53: :months => months, 54: :days => days, 55: :hours => hours, 56: :minutes => minutes, 57: :seconds => seconds, 58: } 59: end
Register a new class with the type-conversion system. When the object of a Redleaf::Statement is set to an instance of the specified classobj, it will be converted to its canonical string form by using the given converter and associated with the specified typeuri. The converter should either be an object that responds to #[] or a Symbol that specifies a method on the object that should be called.
# File lib/redleaf/utils.rb, line 102 102: def register_new_class( classobj, typeuri, converter=nil, &block ) 103: Redleaf.logger.info "Registering a new typed-literal conversion for %p objects." % 104: [ classobj ] 105: typeuri = URI( typeuri ) unless typeuri.is_a?( URI ) 106: converter ||= block 107: converter ||= :to_s 108: 109: Redleaf.logger.debug " will convert to type: %p via %p" % [ typeuri, converter ] 110: @@class_registry[ classobj ] = [ typeuri, converter ] 111: end
Register a new object that knows how to map strings to Ruby objects for the specified typeuri. The converter is any object that responds to #[].
# File lib/redleaf/utils.rb, line 85 85: def register_new_type( typeuri, converter=nil, &block ) 86: typeuri = URI( typeuri ) unless typeuri.is_a?( URI ) 87: Redleaf.logger.info "Registering a new object converter for %s literals " % [ typeuri ] 88: 89: converter ||= block 90: 91: Redleaf.logger.debug " will convert via %p" % [ converter ] 92: @@typeuri_registry[ typeuri ] = converter 93: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.