Class: YOWL::DocObject

Inherits:
Object
  • Object
show all
Defined in:
lib/yowl/util.rb

Direct Known Subclasses

LabelledDocObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, schema) ⇒ DocObject

Returns a new instance of DocObject.



8
9
10
11
12
13
14
# File 'lib/yowl/util.rb', line 8

def initialize(resource, schema)
  @resource = resource
  @schema = schema
  if (@resource and not @resource.uri?)
    raise "ERROR: Instantiating an object with a non-URI resource: #{@resource.to_s} (#{@resource.inspect})"
  end
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/yowl/util.rb', line 5

def resource
  @resource
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/yowl/util.rb', line 6

def schema
  @schema
end

Instance Method Details

#escaped_short_nameObject



65
66
67
68
69
70
71
72
73
# File 'lib/yowl/util.rb', line 65

def escaped_short_name()
  str = short_name()
  # TODO: optimize this into one pattern
  str = str.gsub("://", "_")
  str = str.gsub(".", "_")
  str = str.gsub(",", "_")
  str = str.gsub("/", "_")
  return str
end

#escaped_uriObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yowl/util.rb', line 75

def escaped_uri()
  str = short_name().to_s
  # TODO: optimize this into one pattern
  str = str.gsub("://", "__")
  str = str.gsub(".", "_")
  str = str.gsub("/", "_")
  str = str.gsub("#", "_")
  str = str.gsub(":", "_")
  str = str.gsub(",", "_")
  return str
end

#get_literal(property) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/yowl/util.rb', line 91

def get_literal(property)
  if @resource.nil?
    return nil
  end
  value = @schema.model.first_value(RDF::Query::Pattern.new(@resource, property))
  if not value
    return nil
  end
  return value.strip
end

#hasOtherNamespace?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yowl/util.rb', line 41

def hasOtherNamespace?
  schemaUri = @schema.uri
  case
  when schemaUri == ns
    return false
  when "#{schemaUri}#" == ns
    return false
  when "#{schemaUri}/" == ns
    return false
  end
  return true
end

#hasUri?(uri_) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/yowl/util.rb', line 24

def hasUri?(uri_)
  case uri
  when String
    return uri == uri_
  end
  return @resource == uri_
end

#nsObject



32
33
34
35
36
37
38
39
# File 'lib/yowl/util.rb', line 32

def ns()
  _uri = uri()
  lastChar = _uri[-1,1]
  if  lastChar == '/' or lastChar == '#'
    return _uri
  end
  return _uri[0.._uri.rindex(/[#\/]/)]
end

#ontologyObject



102
103
104
# File 'lib/yowl/util.rb', line 102

def ontology
  return @schema.ontology ? @schema.ontology : nil
end

#repositoryObject



16
17
18
# File 'lib/yowl/util.rb', line 16

def repository
  return @schema.repository
end

#short_nameObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/yowl/util.rb', line 54

def short_name()
  name = @schema.prefixedUri(uri)
  prefix = name.sub(/:.*/, '')
  #
  # If the short name looks like this then fix it a little:
  #    <prefix>:<prefix>-name -> <prefix>:name
  #
  name = name.sub(/:#{prefix}-/, ':')
  return name
end

#to_sObject



87
88
89
# File 'lib/yowl/util.rb', line 87

def to_s()
  return short_name()
end

#uriObject



20
21
22
# File 'lib/yowl/util.rb', line 20

def uri 
  return @resource ? @resource.to_s : nil
end