Class: EasySparql::Resource
- Inherits:
-
Object
- Object
- EasySparql::Resource
show all
- Defined in:
- lib/easy_sparql/resource.rb
Constant Summary
collapse
- @@namespaces =
{
'ws' => 'http://wsarchive.prototype0.net/ontology/',
'po' => 'http://purl.org/ontology/po/',
'dc' => 'http://purl.org/dc/elements/1.1/',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
}
- @@property_map =
{}
- @@cache =
nil
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(uri = nil, bindings = []) ⇒ Resource
Returns a new instance of Resource.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/easy_sparql/resource.rb', line 62
def initialize(uri = nil, bindings = [])
@properties = []
@method_bindings = {}
@uri = uri
bindings.each do |property, value|
value = value.object if value.respond_to? :object
namespaces_i = @@namespaces.invert
namespaces_i.each do |base, short|
if property.to_s.start_with?(base)
method_name = property.to_s.sub(base, short + '_')
if @method_bindings.has_key? method_name.pluralize.to_sym
@method_bindings[method_name.pluralize.to_sym] << value
elsif @method_bindings.has_key? method_name.to_sym
old_value = @method_bindings[method_name.to_sym]
@method_bindings[method_name.pluralize.to_sym] = [ old_value, value ]
else
@method_bindings[method_name.to_sym] = value
end
@properties << method_name.to_sym
end
end
end
@properties.uniq!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/easy_sparql/resource.rb', line 120
def method_missing(name, *args, &block)
if args.empty? && @method_bindings.has_key?(name.to_sym)
value = @method_bindings[name.to_sym]
if value.class == RDF::URI
if @@property_map.has_key? name.to_sym
@method_bindings[name.to_sym] = @@property_map[name.to_sym].find_by_uri(value)
else
@method_bindings[name.to_sym] = Resource.find_by_uri(value)
end
else
value
end
else
nil
end
end
|
Instance Attribute Details
#method_bindings ⇒ Object
Returns the value of attribute method_bindings.
34
35
36
|
# File 'lib/easy_sparql/resource.rb', line 34
def method_bindings
@method_bindings
end
|
#namespaces ⇒ Object
Returns the value of attribute namespaces.
36
37
38
|
# File 'lib/easy_sparql/resource.rb', line 36
def namespaces
@namespaces
end
|
#properties ⇒ Object
Returns the value of attribute properties.
35
36
37
|
# File 'lib/easy_sparql/resource.rb', line 35
def properties
@properties
end
|
#uri ⇒ Object
Returns the value of attribute uri.
33
34
35
|
# File 'lib/easy_sparql/resource.rb', line 33
def uri
@uri
end
|
Class Method Details
.add_namespace(mappings) ⇒ Object
52
53
54
55
56
|
# File 'lib/easy_sparql/resource.rb', line 52
def self.add_namespace(mappings)
mappings.each do |k, v|
@@namespaces[k] = v
end
end
|
.cache ⇒ Object
44
45
46
|
# File 'lib/easy_sparql/resource.rb', line 44
def self.cache
@@cache
end
|
.cache=(cache) ⇒ Object
40
41
42
|
# File 'lib/easy_sparql/resource.rb', line 40
def self.cache=(cache)
@@cache = cache
end
|
.find_by_uri(uri) ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/easy_sparql/resource.rb', line 90
def self.find_by_uri(uri)
cached_resource = @@cache.get(uri) if @@cache
return cached_resource if cached_resource
resource = find_by_uri_from_sparql(uri)
@@cache.set(uri, resource) if @@cache
resource
end
|
.find_by_uri_from_sparql(uri) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/easy_sparql/resource.rb', line 98
def self.find_by_uri_from_sparql(uri)
uri = RDF::URI.new(uri) if uri.class == String
results = sparql.select.where([uri, :p, :o]).execute
bindings = []
results.each do |result|
bindings << [ result.p, result.o ]
end
resource = new(uri, bindings)
end
|
.find_type(uri) ⇒ Object
108
109
110
111
112
|
# File 'lib/easy_sparql/resource.rb', line 108
def self.find_type(uri)
uri = RDF::URI.new(uri) if uri.class == String
results = sparql.select.where([uri, RDF.type, :type]).execute
results[0][:type] unless results.empty?
end
|
.map(mapping) ⇒ Object
48
49
50
|
# File 'lib/easy_sparql/resource.rb', line 48
def self.map(mapping)
@@property_map.merge! mapping
end
|
.namespaces ⇒ Object
58
59
60
|
# File 'lib/easy_sparql/resource.rb', line 58
def self.namespaces
@@namespaces
end
|
.sparql ⇒ Object
114
115
116
|
# File 'lib/easy_sparql/resource.rb', line 114
def self.sparql
EasySparql.store.sparql_client
end
|