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.



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

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



46
47
48
# File 'lib/lbp/resource.rb', line 46

def identifier
  @identifier
end

#resultsObject (readonly)

end class level methods



46
47
48
# File 'lib/lbp/resource.rb', line 46

def results
  @results
end

Class Method Details

.create(resource_url, results) ⇒ Object



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

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



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

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



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

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

#has_partsObject



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

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

#is_part_ofObject



94
95
96
# File 'lib/lbp/resource.rb', line 94

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

#titleObject



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

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



77
78
79
# File 'lib/lbp/resource.rb', line 77

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



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

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



59
60
61
62
63
# File 'lib/lbp/resource.rb', line 59

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