-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_semantic_entity.rb
76 lines (55 loc) · 1.35 KB
/
active_semantic_entity.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require File.join(File.dirname(__FILE__),'semantic_connector')
class ActiveSemantic
class Entity
def initialize(data)
@data = data
end
def uri
return attributes['about'].first
end
def method_missing(method, *args)
attribute_by_name(method.to_s)
end
protected
def attribute_by_name(name)
value = attributes[ name.to_s ]
return [] if value.nil?
value
end
def attributes
return @attributes unless @attributes.nil?
@attributes = {}
@data.each do |key,value|
@attributes[ key.split(':')[1] ] = prepare_attribute_value( value )
end
@attributes
end
private
def prepare_attribute_value(value)
return [value] unless value.is_a? Array
if value.first.is_a?(Hash) and not value.first['rdf:resource'].nil?
value = value.collect do |resource|
LazyEntity.new( resource['rdf:resource'] )
end
end
value
end
end
class LazyEntity < Entity
def initialize(uri)
@uri = uri
end
def uri
@uri
end
def method_missing(method, *args)
load_data
attribute_by_name(method.to_s)
end
private
def load_data
return unless @data.nil?
@data = ActiveSemantic.find_data_by_uri(@uri)
end
end
end