Class: RDF::Microdata::Registry
- Inherits:
-
Object
- Object
- RDF::Microdata::Registry
- Defined in:
- lib/rdf/microdata/registry.rb
Overview
Interface to registry
Instance Attribute Summary collapse
-
#properties ⇒ Hash
readonly
Properties.
-
#uri ⇒ RDF::URI
readonly
Prefix of vocabulary.
Class Method Summary collapse
-
.find(type) ⇒ Registry
Find a registry entry given a type URI.
-
.load_registry(registry_uri) ⇒ Object
Initialize the registry from a URI or file path.
Instance Method Summary collapse
-
#expand(predicateURI) {|equiv| ... } ⇒ Object
Yield a equivalentProperty or subPropertyOf if appropriate.
-
#frag_escape(name) ⇒ Object
Fragment escape a name.
-
#initialize(prefixURI, properties = {}) ⇒ Registry
constructor
Initialize registry for a particular prefix URI.
-
#predicateURI(name, base_uri) ⇒ RDF::URI
Generate a predicateURI given a
name
. -
#tokenize(predicateURI) ⇒ String
Turn a predicateURI into a simple token.
Constructor Details
#initialize(prefixURI, properties = {}) ⇒ Registry
Initialize registry for a particular prefix URI
35 36 37 38 39 40 41 |
# File 'lib/rdf/microdata/registry.rb', line 35 def initialize(prefixURI, properties = {}) @uri = prefixURI @properties = properties @property_base = prefixURI.to_s # Append a '#' for fragment if necessary @property_base += '#' unless %w(/ #).include?(@property_base[-1,1]) end |
Instance Attribute Details
#properties ⇒ Hash (readonly)
Returns properties.
10 11 12 |
# File 'lib/rdf/microdata/registry.rb', line 10 def properties @properties end |
#uri ⇒ RDF::URI (readonly)
Returns Prefix of vocabulary.
7 8 9 |
# File 'lib/rdf/microdata/registry.rb', line 7 def uri @uri end |
Class Method Details
.find(type) ⇒ Registry
Find a registry entry given a type URI
48 49 50 51 52 |
# File 'lib/rdf/microdata/registry.rb', line 48 def self.find(type) @prefixes ||= {} k = @prefixes.keys.detect {|key| type.to_s.index(key) == 0 } @prefixes[k] if k end |
.load_registry(registry_uri) ⇒ Object
Initialize the registry from a URI or file path
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rdf/microdata/registry.rb', line 16 def self.load_registry(registry_uri) return if @registry_uri == registry_uri json = RDF::Util::File.open_file(registry_uri) { |f| ::JSON.load(f) } @prefixes = {} json.each do |prefix, elements| next unless elements.is_a?(Hash) properties = elements.fetch("properties", {}) @prefixes[prefix] = Registry.new(prefix, properties) end @registry_uri = registry_uri end |
Instance Method Details
#expand(predicateURI) {|equiv| ... } ⇒ Object
Yield a equivalentProperty or subPropertyOf if appropriate
84 85 86 87 88 89 90 91 92 |
# File 'lib/rdf/microdata/registry.rb', line 84 def (predicateURI) tok = tokenize(predicateURI) if @properties[tok].is_a?(Hash) value = @properties[tok].fetch("subPropertyOf", nil) value ||= @properties[tok].fetch("equivalentProperty", nil) Array(value).each {|equiv| yield RDF::URI(equiv)} end end |
#frag_escape(name) ⇒ Object
Fragment escape a name
104 105 106 |
# File 'lib/rdf/microdata/registry.rb', line 104 def frag_escape(name) name.to_s.gsub(/["#%<>\[\\\]^{|}]/) {|c| '%' + c.unpack('H2' * c.bytesize).join('%').upcase} end |
#predicateURI(name, base_uri) ⇒ RDF::URI
Generate a predicateURI given a name
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rdf/microdata/registry.rb', line 60 def predicateURI(name, base_uri) u = RDF::URI(name) # 1) If _name_ is an _absolute URL_, return _name_ as a _URI reference_ return u if u.absolute? n = frag_escape(name) if uri.nil? # 2) If current vocabulary from context is null, there can be no current vocabulary. # Return the URI reference that is the document base with its fragment set to the fragment-escaped value of name u = RDF::URI(base_uri.to_s) u.fragment = frag_escape(name) u else # 4) If scheme is vocabulary return the URI reference constructed by appending the fragment escaped value of name to current vocabulary, separated by a U+0023 NUMBER SIGN character (#) unless the current vocabulary ends with either a U+0023 NUMBER SIGN character (#) or SOLIDUS U+002F (/). RDF::URI(@property_base + n) end end |
#tokenize(predicateURI) ⇒ String
Turn a predicateURI into a simple token
98 99 100 |
# File 'lib/rdf/microdata/registry.rb', line 98 def tokenize(predicateURI) predicateURI.to_s.sub(@property_base, '') end |