Class: TaliaCore::Source
- Inherits:
-
ActiveSource
- Object
- ActiveRecord::Base
- ActiveSource
- TaliaCore::Source
- Defined in:
- lib/talia_core/source.rb
Overview
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
Direct Known Subclasses
Collection, TaliaCore::SourceTypes::Agent, TaliaCore::SourceTypes::DcResource, TaliaCore::SourceTypes::DummySource, TaliaCore::SourceTypes::MarcontResource, TaliaCore::SourceTypes::SkosConcept
Instance Attribute Summary collapse
-
#predicates_attributes ⇒ Object
TODO: Delete this/old backend method?.
Class Method Summary collapse
-
.find_or_instantiate_by_uri(uri, local_name) ⇒ Object
Try to find a source for the given uri, if not exists it instantiate a new one, combining the N::LOCAL namespace and the given local name.
-
.groups_by_property(property, values, params = {}) ⇒ Object
Searches for sources where
property
has one of the values given to this method. -
.normalize_uri(uri, label = '') ⇒ Object
Normalize the given uri.
Instance Method Summary collapse
-
#==(value) ⇒ Object
Equality test.
-
#associated?(namespace, name, stringified_predicate) ⇒ Boolean
Check if the current source is related with the given rdf object (triple endpoint).
-
#collections ⇒ Object
Returns the Collection (or collections) this source is in.
-
#grouped_direct_predicates ⇒ Object
Return an hash of direct predicates, grouped by namespace.
-
#grouped_predicates_attributes ⇒ Object
Return an hash of new predicated attributes, grouped by namespace.
-
#label(type = N::RDFS::label) ⇒ Object
This returns a single label of the given type.
-
#labels(type = N::RDFS::label) ⇒ Object
Returns an array of labels for this source.
-
#local ⇒ Object
Indicates if this source belongs to the local store.
-
#normalize_uri(uri, label = '') ⇒ Object
See Source.normalize_uri.
-
#predicate_changed?(namespace, name, objects) ⇒ Boolean
Check if a predicate is changed.
-
#predicate_objects(namespace, name) ⇒ Object
TODO: Delete this/old backend method?.
-
#primary_source ⇒ Object
Indicates if the current source is considered “primary” in the local library.
-
#primary_source=(value) ⇒ Object
Shortcut for assigning the primary_source status.
-
#save_predicates_attributes ⇒ Object
Save, associate/disassociate given predicates attributes.
-
#titleized ⇒ Object
Return the titleized uri local name.
-
#uri ⇒ Object
The uri will be wrapped into an object.
Methods inherited from ActiveSource
#[], #[]=, #add_additional_rdf_types, #add_semantic_attributes, #attach_files, #data, #db_attr?, #defined_property?, #direct_predicates, #inverse, #inverse_predicates, #predicate, #predicate_replace, #predicate_set, #predicate_set_uniq, #property_options_for, #rdf_selftype, #reload, #rewrite_attributes, #rewrite_attributes!, #short_uri, #to_rdf, #to_s, #to_uri, #to_xml, #update_attributes, #update_attributes!, #update_attributes_orig, #update_attributes_orig!, #update_source, #value_for, #write_predicate_direct
Methods included from ActiveSourceParts::ClassMethods
#additional_rdf_types, #create_from_xml, #create_multi_from, #create_source, #db_attr?, #defined_property?, #exists?, #expand_uri, #new, #paginate, #property_options_for, #props_to_destroy, #rewrite, #split_attribute_hash, #update, #value_for
Methods included from ActiveSourceParts::Finders
#count, #find, #find_by_partial_local, #find_by_partial_uri, #find_by_uri_token
Methods included from ActiveSourceParts::SqlHelper
#default_inv_joins, #default_joins, #props_join, #sources_join
Methods included from ActiveSourceParts::PredicateHandler::ClassMethods
Methods included from TaliaUtil::Progressable
#progressor, #progressor=, #run_with_progress
Methods included from ActiveSourceParts::RdfHandler
#autosave_rdf=, #autosave_rdf?, #create_rdf, #my_rdf, #to_rdf
Methods included from ActiveSourceParts::PredicateHandler
#each_cached_wrapper, #get_objects_on, #get_wrapper_on, #has_type?, #inject_predicate, #reset!, #save_wrappers, #types
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (protected)
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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/talia_core/source.rb', line 273 def method_missing(method_name, *args) # TODO: Add permission checking for all updates to the model # TODO: Add permission checking for read access? update = method_name.to_s[-1..-1] == '=' shortcut = if update method_name.to_s[0..-2] else method_name.to_s end # Otherwise, check for the RDF predicate registered = N::URI[shortcut.to_s] return super(method_name, *args) unless(registered) # normal handler if not a registered uri raise(ArgumentError, "Must give a namspace as argument") unless(registered.is_a?(N::Namespace)) DummyHandler.new(registered, self) end |
Instance Attribute Details
#predicates_attributes ⇒ Object
TODO: Delete this/old backend method?
132 133 134 |
# File 'lib/talia_core/source.rb', line 132 def predicates_attributes @predicates_attributes end |
Class Method Details
.find_or_instantiate_by_uri(uri, local_name) ⇒ Object
Try to find a source for the given uri, if not exists it instantiate a new one, combining the N::LOCAL namespace and the given local name
Example:
ActiveSource.find_or_instantiate_by_uri('http://talia.org/existent')
# => #<TaliaCore::ActiveSource id: 1, uri: "http://talia.org/existent">
ActiveSource.find_or_instantiate_by_uri('http://talia.org/unexistent', 'Foo Bar')
# => #<TaliaCore::ActiveSource id: nil, uri: "http://talia.org/Foo_Bar">
TODO: Delete this/old backend method?
96 97 98 99 |
# File 'lib/talia_core/source.rb', line 96 def self.find_or_instantiate_by_uri(uri, local_name) # :nodoc: result = find_by_uri(uri) result ||= self.new(N::LOCAL.to_s + local_name.to_permalink) end |
.groups_by_property(property, values, params = {}) ⇒ Object
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)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/talia_core/source.rb', line 64 def self.groups_by_property(property, values, params = {}) # First create the joins joins = 'LEFT JOIN semantic_relations ON semantic_relations.subject_id = active_sources.id ' joins << "LEFT JOIN active_sources AS t_sources ON semantic_relations.object_id = t_sources.id AND semantic_relations.object_type = 'TaliaCore::ActiveSource' " joins << "LEFT JOIN semantic_properties ON semantic_relations.object_id = semantic_properties.id AND semantic_relations.object_type = 'TaliaCore::SemanticProperty' " property = uri_string_for(property, false) results = {} for val in values find(:all ) val_str = uri_string_for(val, false) find_parms = params.merge( :conditions => ['semantic_properties.value = ? OR t_sources.uri = ?', val_str, val_str], :joins => joins ) results[val] = find(:all, find_parms) end results end |
.normalize_uri(uri, label = '') ⇒ Object
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
257 258 259 260 261 |
# File 'lib/talia_core/source.rb', line 257 def normalize_uri(uri, label = '') uri = N::LOCAL if uri.blank? uri = N::LOCAL+label.gsub(' ', '_') if uri == N::LOCAL.to_s uri.to_s end |
Instance Method Details
#==(value) ⇒ Object
Equality test. Two sources are equal if they have the same URI
192 193 194 |
# File 'lib/talia_core/source.rb', line 192 def ==(value) value.is_a?(Source) && (value.uri == uri) end |
#associated?(namespace, name, stringified_predicate) ⇒ Boolean
Check if the current source is related with the given rdf object (triple endpoint). TODO: Delete this/old backend method?
122 123 124 |
# File 'lib/talia_core/source.rb', line 122 def associated?(namespace, name, stringified_predicate) # :nodoc: predicate_objects(namespace, name).include?(stringified_predicate) end |
#collections ⇒ Object
Returns the Collection (or collections) this source is in.
202 203 204 |
# File 'lib/talia_core/source.rb', line 202 def collections Collection.find(:all, :find_through => [N::DCT.hasPart, self]) end |
#grouped_direct_predicates ⇒ Object
Return an hash of direct predicates, grouped by namespace. TODO: Delete this/old backend method?
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/talia_core/source.rb', line 103 def grouped_direct_predicates # :nodoc: #TODO should it be memoized? direct_predicates.inject({}) do |result, predicate| predicates = self[predicate].collect { |p| SourceTransferObject.new(p.to_s) } namespace = predicate.namespace.to_s result[namespace] ||= {} result[namespace][predicate.local_name] ||= [] result[namespace][predicate.local_name] << predicates result end end |
#grouped_predicates_attributes ⇒ Object
Return an hash of new predicated attributes, grouped by namespace. TODO: Delete this/old backend method?
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/talia_core/source.rb', line 142 def grouped_predicates_attributes # :nodoc: @grouped_predicates_attributes ||= predicates_attributes.inject({}) do |result, predicate| namespace, name = predicate['namespace'], predicate['name'] predicate = SourceTransferObject.new(predicate['titleized']) result[namespace] ||= {} result[namespace][name] ||= [] result[namespace][name] << predicate result end end |
#label(type = N::RDFS::label) ⇒ Object
This returns a single label of the given type. (If multiple labels exist in the RDF, just the first is returned.)
180 181 182 |
# File 'lib/talia_core/source.rb', line 180 def label(type = N::RDFS::label) labels(type)[0] end |
#labels(type = N::RDFS::label) ⇒ Object
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.
169 170 171 172 173 174 175 176 |
# File 'lib/talia_core/source.rb', line 169 def labels(type = N::RDFS::label) labels = get_attribute(type) unless(labels && labels.size > 0) labels = [uri.local_name] end labels end |
#local ⇒ Object
Indicates if this source belongs to the local store
35 36 37 |
# File 'lib/talia_core/source.rb', line 35 def local uri.local? end |
#normalize_uri(uri, label = '') ⇒ Object
See Source.normalize_uri
197 198 199 |
# File 'lib/talia_core/source.rb', line 197 def normalize_uri(uri, label = '') self.class.normalize_uri(uri, label) end |
#predicate_changed?(namespace, name, objects) ⇒ Boolean
Check if a predicate is changed. TODO: Delete this/old backend method?
127 128 129 |
# File 'lib/talia_core/source.rb', line 127 def predicate_changed?(namespace, name, objects) # :nodoc: not predicate_objects(namespace, name).eql?(objects.map(&:to_s)) end |
#predicate_objects(namespace, name) ⇒ Object
TODO: Delete this/old backend method?
116 117 118 |
# File 'lib/talia_core/source.rb', line 116 def predicate_objects(namespace, name) #:nodoc: predicate(namespace, name).values.flatten.map(&:to_s) end |
#primary_source ⇒ Object
Indicates if the current source is considered “primary” in the local library
47 48 49 |
# File 'lib/talia_core/source.rb', line 47 def primary_source predicate(:talia, :primary_source) == true end |
#primary_source=(value) ⇒ Object
Shortcut for assigning the primary_source status
40 41 42 43 |
# File 'lib/talia_core/source.rb', line 40 def primary_source=(value) value = value ? 'true' : 'false' predicate_set(:talia, :primary_source, value) end |
#save_predicates_attributes ⇒ Object
Save, associate/disassociate given predicates attributes. TODO: Delete this/ old backend method?
155 156 157 158 159 160 |
# File 'lib/talia_core/source.rb', line 155 def save_predicates_attributes # :nodoc: each_predicate do |namespace, name, objects| objects.each { |object| object.save if object.is_a?(Source) && object.new_record? } self.predicate_replace(namespace, name, objects.to_s) if predicate_changed?(namespace, name, objects) end end |
#titleized ⇒ Object
Return the titleized uri local name.
http://localnode.org/source # => Source
187 188 189 |
# File 'lib/talia_core/source.rb', line 187 def titleized self.uri.local_name.titleize end |
#uri ⇒ Object
The uri will be wrapped into an object
30 31 32 |
# File 'lib/talia_core/source.rb', line 30 def uri N::URI.new(self[:uri]) end |