Class: TaliaCore::Source

Inherits:
ActiveSource show all
Defined in:
lib/talia_core/source.rb

Overview

This represents a Source in the Talia core system.

Since data for the Source exists both in the database and in the RDF store, the handling/saving of data is a bit peculiar at the moment (subject to change in the future):

  • When a new Source is created, no data is saved

  • RDF properties cannot be written until the Source has been saved for the first time

  • Database properties are only written when the save method is called

  • RDF properties are written immediately when they are assigned

  • To ensure that the data is written, the save method should be called as necessary.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveSource

#[], #[]=, #add_additional_rdf_types, #add_semantic_attributes, #attach_files, #data, #db_attr?, #direct_predicates, #inverse, #inverse_predicates, #predicate, #predicate_replace, #predicate_set, #predicate_set_uniq, #rdf_selftype, #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?, #exists?, #expand_uri, #new, #paginate, #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

#prefetch_relations_for

Methods included from TaliaUtil::Progressable

#progressor, #progressor=, #run_with_progress

Methods included from ActiveSourceParts::Rdf

#autosave_rdf=, #autosave_rdf?, #create_rdf, #my_rdf, #to_rdf

Methods included from ActiveSourceParts::PredicateHandler

#each_cached_wrapper, #get_objects_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 “dummy” handler that allows access to properties.

This will allow invocations as namespace::name

Raises:

  • (ArgumentError)


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/talia_core/source.rb', line 253

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_attributesObject

Returns the value of attribute predicates_attributes.



123
124
125
# File 'lib/talia_core/source.rb', line 123

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">


91
92
93
94
# File 'lib/talia_core/source.rb', line 91

def self.find_or_instantiate_by_uri(uri, local_name)
  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)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/talia_core/source.rb', line 61

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)
  results = {}
  for val in values
    find(:all )
    val_str = uri_string_for(val)
    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


237
238
239
240
241
# File 'lib/talia_core/source.rb', line 237

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



181
182
183
# File 'lib/talia_core/source.rb', line 181

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).

Returns:

  • (Boolean)


114
115
116
# File 'lib/talia_core/source.rb', line 114

def associated?(namespace, name, stringified_predicate)
  predicate_objects(namespace, name).include?(stringified_predicate)
end

#grouped_direct_predicatesObject

Return an hash of direct predicates, grouped by namespace.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/talia_core/source.rb', line 97

def grouped_direct_predicates
  #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_attributesObject

Return an hash of new predicated attributes, grouped by namespace.



132
133
134
135
136
137
138
139
140
141
# File 'lib/talia_core/source.rb', line 132

def grouped_predicates_attributes
  @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.)



169
170
171
# File 'lib/talia_core/source.rb', line 169

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.



158
159
160
161
162
163
164
165
# File 'lib/talia_core/source.rb', line 158

def labels(type = N::RDFS::label)
  labels = get_attribute(type)
  unless(labels && labels.size > 0)
    labels = [uri.local_name]
  end

  labels
end

#localObject

Indicates if this source belongs to the local store



32
33
34
# File 'lib/talia_core/source.rb', line 32

def local
  uri.local?
end

#normalize_uri(uri, label = '') ⇒ Object



185
186
187
# File 'lib/talia_core/source.rb', line 185

def normalize_uri(uri, label = '')
  self.class.normalize_uri(uri, label)
end

#predicate_changed?(namespace, name, objects) ⇒ Boolean

Check if a predicate is changed.

Returns:

  • (Boolean)


119
120
121
# File 'lib/talia_core/source.rb', line 119

def predicate_changed?(namespace, name, objects)
  not predicate_objects(namespace, name).eql?(objects.map(&:to_s))
end

#predicate_objects(namespace, name) ⇒ Object

:nodoc:



109
110
111
# File 'lib/talia_core/source.rb', line 109

def predicate_objects(namespace, name) #:nodoc:
  predicate(namespace, name).values.flatten.map(&:to_s)
end

#primary_sourceObject

Indicates if the current source is considered “primary” in the local library



44
45
46
# File 'lib/talia_core/source.rb', line 44

def primary_source
  predicate(:talia, :primary_source) == true
end

#primary_source=(value) ⇒ Object

Shortcut for assigning the primary_source status



37
38
39
40
# File 'lib/talia_core/source.rb', line 37

def primary_source=(value)
  value = value ? 'true' : 'false'
  predicate_set(:talia, :primary_source, value)
end

#save_predicates_attributesObject

Save, associate/disassociate given predicates attributes.



144
145
146
147
148
149
# File 'lib/talia_core/source.rb', line 144

def save_predicates_attributes
  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

#titleizedObject

Return the titleized uri local name.

http://localnode.org/source # => Source


176
177
178
# File 'lib/talia_core/source.rb', line 176

def titleized
  self.uri.local_name.titleize
end

#uriObject

The uri will be wrapped into an object



27
28
29
# File 'lib/talia_core/source.rb', line 27

def uri
  N::URI.new(self[:uri])
end