Class: Lbp::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lbp/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_url, results) ⇒ Resource

Returns a new instance of Resource.



48
49
50
51
52
53
54
# File 'lib/lbp/resource.rb', line 48

def initialize(resource_url, results)
	# if there are problems with results being empty 
	# and, for example, dup or filter being called on a null class
	# consider changing the following line to @results = results || <an empty object for whatever results normally is>
	@results = results || RDF::Query::Solutions.new()
	@identifier = ResourceIdentifier.new(resource_url)
end

Instance Attribute Details

#identifierObject (readonly)

end class level methods



44
45
46
# File 'lib/lbp/resource.rb', line 44

def identifier
  @identifier
end

#resultsObject (readonly)

end class level methods



44
45
46
# File 'lib/lbp/resource.rb', line 44

def results
  @results
end

Class Method Details

.create(resource_url, results) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lbp/resource.rb', line 27

def create(resource_url, results)
	type = results.dup.filter(:p => RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")).first[:o].to_s.split("/").last
	klass = if type == "workGroup" 
			Lbp.const_get("WorkGroup") 
		elsif type == "expressionType"
			Lbp.const_get("ExpressionType")
		else
			Lbp.const_get(type.capitalize)
		end
	klass.new(resource_url, results)
rescue NameError
	Resource.new(resource_url, results)
end

.find(resource_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lbp/resource.rb', line 12

def find(resource_id)
	#adding the to_s method allows a resource to be created 
	#by passing in an RDF::URL object as well as the url string.
	if resource_id.to_s.include? "http"
		query = Query.new
		results = query.subject("<" + resource_id.to_s + ">")
		resource_url = resource_id.to_s
		create(resource_url, results)
	else
		query = Query.new
		results = query.subject_with_short_id(resource_id)
		resource_url = "http://scta.info/resource/" + resource_id
		create(resource_url, results)
	end
end

Instance Method Details

#descriptionObject



86
87
88
# File 'lib/lbp/resource.rb', line 86

def description
	value(RDF::Vocab::DC11.description).to_s
end

#has_partsObject



89
90
91
# File 'lib/lbp/resource.rb', line 89

def has_parts
	values(RDF::Vocab::DC.hasPart)
end

#is_part_ofObject



92
93
94
# File 'lib/lbp/resource.rb', line 92

def is_part_of
	value(RDF::Vocab::DC.isPartOf)
end

#titleObject



78
79
80
81
82
83
84
85
# File 'lib/lbp/resource.rb', line 78

def title
	#careful here; title in db is not actualy a uri, but a litteral
	#to_s method should work, but it might not be correct for this to be initially 
	#instantiated into a resource identifer. 
	# This is why I'm forcing the to_s method in the return value rather than 
	# return the ResourceIdentifer object itself as in the case of type above
	value(RDF::Vocab::DC11.title).to_s
end

#typeObject

query for properties global to all resources



75
76
77
# File 'lib/lbp/resource.rb', line 75

def type
	value("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
end

#value(property) ⇒ Object

should return a single resource identifier; and error if there is more than one property for this value



63
64
65
66
67
68
69
70
71
72
# File 'lib/lbp/resource.rb', line 63

def value(property) # should return a single resource identifier; and error if there is more than one property for this value
	value = @results.dup.filter(:p => RDF::URI(property))
	if value.count > 0 
		value = value.first[:o]
		ResourceIdentifier.new(value)
	else 
		nil
	end
	
end

#values(property) ⇒ Object

generic query methods for all resources



57
58
59
60
61
# File 'lib/lbp/resource.rb', line 57

def values(property) # should return an array of resource identifiers
	results = self.results.dup.filter(:p => RDF::URI(property))
	array = results.map {|m| ResourceIdentifier.new(m[:o])}
	return array
end