| Class | TaliaCore::Source |
| In: |
lib/talia_core/source.rb
|
| Parent: | ActiveSource |
Base class for most sources in the Talia system. The Source class has some additional features over the basic ActiveSource class.
Most importantly, it contains the "smart" accessor in the same style as ActiveRDF:
source.rdf::something => SemanticCollection Wrapper # is the same as: source[N::RDF.something]
There are also
Searches for sources where property has one of the values given to this method. The result is a hash that contains one result list for each of the values, with the value as a key.
This performs a find operation for each value, and the params passed to this method are added to the find parameters for each of those finds.
Example
# Returns all Sources that are of the RDFS type Class or Property. This # will return a hash with 2 lists (one for the Classes, and one for the # properties, and each list will be limited to 5 elements. Source.groups_by_property(N::RDF::type, [N::RDFS.Class, N::RDFS.Property], :limit => 5)
# File lib/talia_core/source.rb, line 64
64: def self.groups_by_property(property, values, params = {})
65: # First create the joins
66: joins = 'LEFT JOIN semantic_relations ON semantic_relations.subject_id = active_sources.id '
67: joins << "LEFT JOIN active_sources AS t_sources ON semantic_relations.object_id = t_sources.id AND semantic_relations.object_type = 'TaliaCore::ActiveSource' "
68: joins << "LEFT JOIN semantic_properties ON semantic_relations.object_id = semantic_properties.id AND semantic_relations.object_type = 'TaliaCore::SemanticProperty' "
69:
70: property = uri_string_for(property, false)
71: results = {}
72: for val in values
73: find(:all )
74: val_str = uri_string_for(val, false)
75: find_parms = params.merge(
76: :conditions => ['semantic_properties.value = ? OR t_sources.uri = ?', val_str, val_str],
77: :joins => joins
78: )
79: results[val] = find(:all, find_parms)
80: end
81:
82: results
83: end
Normalize the given uri.
Example:
normalize_uri('Lucca') # => http://www.talia.discovery-project.org/sources/Lucca
normalize_uri('http://xmlns.com/foaf/0.1/Group') # => http://xmlns.com/foaf/0.1/Group
normalize_uri('http://www.talia.discovery-project.org/sources/Lucca')
# => http://www.talia.discovery-project.org/sources/Lucca
# File lib/talia_core/source.rb, line 257
257: def normalize_uri(uri, label = '')
258: uri = N::LOCAL if uri.blank?
259: uri = N::LOCAL+label.gsub(' ', '_') if uri == N::LOCAL.to_s
260: uri.to_s
261: end
Equality test. Two sources are equal if they have the same URI
# File lib/talia_core/source.rb, line 192
192: def ==(value)
193: value.is_a?(Source) && (value.uri == uri)
194: end
Returns the Collection (or collections) this source is in.
# File lib/talia_core/source.rb, line 202
202: def collections
203: Collection.find(:all, :find_through => [N::DCT.hasPart, self])
204: end
Returns an array of labels for this source. You may give the name of the property that is used as a label, by default it uses rdf:label(s). If the given property is not set, it will return the local part of this Source‘s URI.
In any case, the result will always be an Array with at least one elment.
# File lib/talia_core/source.rb, line 169
169: def labels(type = N::RDFS::label)
170: labels = get_attribute(type)
171: unless(labels && labels.size > 0)
172: labels = [uri.local_name]
173: end
174:
175: labels
176: end
# File lib/talia_core/source.rb, line 197
197: def normalize_uri(uri, label = '')
198: self.class.normalize_uri(uri, label)
199: end
Shortcut for assigning the primary_source status
# File lib/talia_core/source.rb, line 40
40: def primary_source=(value)
41: value = value ? 'true' : 'false'
42: predicate_set(:talia, :primary_source, value)
43: end
Missing methods: This just check if the given method corresponds to a registered namespace. If yes, this will return a DummyHandler that allows access to properties.
This will allow invocations such as namespace::name
# File lib/talia_core/source.rb, line 273
273: def method_missing(method_name, *args)
274: # TODO: Add permission checking for all updates to the model
275: # TODO: Add permission checking for read access?
276:
277: update = method_name.to_s[-1..-1] == '='
278:
279: shortcut = if update
280: method_name.to_s[0..-2]
281: else
282: method_name.to_s
283: end
284:
285: # Otherwise, check for the RDF predicate
286: registered = N::URI[shortcut.to_s]
287:
288: return super(method_name, *args) unless(registered) # normal handler if not a registered uri
289: raise(ArgumentError, "Must give a namspace as argument") unless(registered.is_a?(N::Namespace))
290:
291: DummyHandler.new(registered, self)
292: end