Class: Nuggets::RDF::Turtle

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/nuggets/rdf/turtle.rb

Constant Summary collapse

NS_SEPARATOR =
':'.freeze
NS =
::RDF::Vocabulary.inject({}) { |h, v| h[v.__prefix__] = v; h }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#_nuggets_original_all?, #_nuggets_original_any?, #_nuggets_original_max, #_nuggets_original_max_by, #_nuggets_original_min, #_nuggets_original_min_by, #_nuggets_original_minmax, #_nuggets_original_minmax_by, #agrep, #all?, #any?, #max, #max_by, #min, #min_by, #minmax, #minmax_by

Constructor Details

#initialize(reader, map = true) ⇒ Turtle



62
63
64
65
# File 'lib/nuggets/rdf/turtle.rb', line 62

def initialize(reader, map = true)
  @reader, @base, @prefixes = reader, *reader.parse_prologue
  self.map = map
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



67
68
69
# File 'lib/nuggets/rdf/turtle.rb', line 67

def base
  @base
end

#fileObject

Returns the value of attribute file.



69
70
71
# File 'lib/nuggets/rdf/turtle.rb', line 69

def file
  @file
end

#mapObject

Returns the value of attribute map.



67
68
69
# File 'lib/nuggets/rdf/turtle.rb', line 67

def map
  @map
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



67
68
69
# File 'lib/nuggets/rdf/turtle.rb', line 67

def prefixes
  @prefixes
end

#readerObject (readonly)

Returns the value of attribute reader.



67
68
69
# File 'lib/nuggets/rdf/turtle.rb', line 67

def reader
  @reader
end

Class Method Details

.foreach(file, *args, &block) ⇒ Object



56
57
58
# File 'lib/nuggets/rdf/turtle.rb', line 56

def foreach(file, *args, &block)
  open(file, *args) { |turtle| turtle.each(&block) }
end

.open(file, *args) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/nuggets/rdf/turtle.rb', line 47

def open(file, *args)
  ::RDF::Reader.open(file, :format => :ttl) { |reader|
    turtle = new(reader, *args)
    turtle.file = ::File.expand_path(file)

    return block_given? ? yield(turtle) : turtle
  }
end

Instance Method Details

#closed?Boolean



112
113
114
# File 'lib/nuggets/rdf/turtle.rb', line 112

def closed?
  reader.closed?
end

#eachObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nuggets/rdf/turtle.rb', line 98

def each
  return enum_for(:each) unless block_given?

  uri, map, base = ::RDF::URI, self.map, self.base

  each_statement { |t|
    s, p, o = *t

    if s.is_a?(uri) and k = map[p] and s.start_with?(base)
      yield s, o, k
    end
  }
end

#each_statement(&block) ⇒ Object



92
93
94
95
96
# File 'lib/nuggets/rdf/turtle.rb', line 92

def each_statement(&block)
  return enum_for(:each_statement) unless block_given?
  reader.parse_statements(&block)
  self
end

#inspectObject



116
117
118
119
120
# File 'lib/nuggets/rdf/turtle.rb', line 116

def inspect
  '#<%s:0x%x @file=%p, @base=%p%s>' % [
    self.class, object_id, file, base, closed? ? ' (closed)' : ''
  ]
end

#statementsObject



88
89
90
# File 'lib/nuggets/rdf/turtle.rb', line 88

def statements
  each_statement.to_a
end