Class: Turtle::Builder
- Inherits:
-
Object
- Object
- Turtle::Builder
- Defined in:
- lib/turtle/builder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_(name = nil) ⇒ Object
- #a ⇒ Object
- #base(uri) ⇒ Object
- #blank(name = nil) ⇒ Object
- #comment(text) ⇒ Object (also: #c)
- #encode(s) ⇒ Object
- #indent ⇒ Object
-
#initialize(out) ⇒ Builder
constructor
A new instance of Builder.
- #newline ⇒ Object (also: #nl)
- #object(o) ⇒ Object (also: #o)
- #predicate(p, o = nil, &block) ⇒ Object (also: #p)
- #prefix(prefix, namespace = nil) ⇒ Object
- #resolve(e) ⇒ Object
- #subject(s, p = nil, o = nil, &block) ⇒ Object (also: #s, #triple, #t)
- #xsd_datatype(datatype) ⇒ Object
Constructor Details
#initialize(out) ⇒ Builder
Returns a new instance of Builder.
17 18 19 20 21 22 23 |
# File 'lib/turtle/builder.rb', line 17 def initialize(out) @out = out @state = :object @xsd = nil @blank = 0 @indent = 0 end |
Class Method Details
.build(out = STDOUT, extension = nil, &block) ⇒ Object
220 221 222 223 224 |
# File 'lib/turtle/builder.rb', line 220 def self.build(out=STDOUT, extension=nil, &block) builder = new out builder.extend ext if extension builder.instance_eval &block end |
Instance Method Details
#_(name = nil) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/turtle/builder.rb', line 119 def _(name=nil) if name blank name else :blank end end |
#a ⇒ Object
127 128 129 |
# File 'lib/turtle/builder.rb', line 127 def a() :a end |
#base(uri) ⇒ Object
25 26 27 |
# File 'lib/turtle/builder.rb', line 25 def base(uri) @out.puts "@base <#{uri}> ." end |
#blank(name = nil) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/turtle/builder.rb', line 103 def blank(name=nil) unless name name = "b#{@blank}" @blank += 1 end BlankNode.new name end |
#comment(text) ⇒ Object Also known as: c
95 96 97 |
# File 'lib/turtle/builder.rb', line 95 def comment(text) @out.print "#{indent}# #{text}\n" end |
#encode(s) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/turtle/builder.rb', line 197 def encode(s) a = s.codepoints.collect do |p| case p when 34 then "\\\"" when 92 then "\\\\" when 0..127 then p.chr else "\\u#{sprintf("%X", p).rjust(4, "0")}" end end a.join end |
#indent ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/turtle/builder.rb', line 210 def indent() if block_given? @indent += 1 yield @indent -= 1 else " " * @indent end end |
#newline ⇒ Object Also known as: nl
99 100 101 |
# File 'lib/turtle/builder.rb', line 99 def newline @out.print "\n" end |
#object(o) ⇒ Object Also known as: o
87 88 89 90 91 92 93 |
# File 'lib/turtle/builder.rb', line 87 def object(o) if @state == :object @out.print " ," end @out.print "\n#{indent}#{resolve o}" @state = :object end |
#predicate(p, o = nil, &block) ⇒ Object Also known as: p
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/turtle/builder.rb', line 73 def predicate(p, o=nil, &block) if @state == :object @out.print " ;" end @out.print "\n#{indent}#{resolve p}" if o @out.print " #{resolve o}" @state = :object else @state = :predicate end indent &block end |
#prefix(prefix, namespace = nil) ⇒ Object
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 |
# File 'lib/turtle/builder.rb', line 29 def prefix(prefix, namespace=nil) if namespace case prefix when :blank, :_ @out.puts "@prefix : <#{namespace}> ." else @out.puts "@prefix #{prefix}: <#{namespace}> ." @xsd = prefix if namespace == "http://www.w3.org/2001/XMLSchema#" end else case prefix when :blank, :_ @out.puts "@prefix : <#{namespace}> ." when :xsd prefix(:xsd, "http://www.w3.org/2001/XMLSchema#") when :rdf prefix(:rdf, "http://www.w3.org/1999/02/22-rdf-syntax-ns#") when :rdfs prefix(:rdfs, "http://www.w3.org/2000/01/rdf-schema#") when :owl prefix(:owl, "http://www.w3.org/2002/07/owl#") when String @out.puts "@prefix : <#{prefix}> ." end end end |
#resolve(e) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/turtle/builder.rb', line 139 def resolve(e) case e when Array case e.size when 0 "<>" when 1 first = e.first case first when :blank, :_ ":" when Symbol "#{first}:" when String "<#{e.first}>" end else first, last = e case first when :blank, :_ case last when :blank, :_ ":" else ":#{last}" end when Symbol case last when :blank, :_ "#{first}:" else "#{first}:#{last}" end when String "\"#{first}\"^^#{xsd_datatype last}" end end when :blank, :_ resolve blank when :a "a" when BlankNode e.to_s when Symbol resolve blank(e) when String "\"#{encode(e)}\"" when Fixnum "\"#{e}\"^^#{xsd_datatype :integer}" when Float "\"#{e}\"^^#{xsd_datatype :double}" when true, false "\"#{e}\"^^#{xsd_datatype :boolean}" else e end end |
#subject(s, p = nil, o = nil, &block) ⇒ Object Also known as: s, triple, t
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/turtle/builder.rb', line 56 def subject(s, p=nil, o=nil, &block) @out.print "#{indent}#{resolve s}" if p @out.print " #{resolve p}" if o @out.print " #{resolve o}" @state = :object else @state = :predicate end else @state = :subject end indent &block @out.puts " ." end |
#xsd_datatype(datatype) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/turtle/builder.rb', line 131 def xsd_datatype(datatype) if @xsd "#{@xsd}:#{datatype}" else "<http://www.w3.org/2001/XMLSchema##{datatype}>" end end |