Class: RoadForest::MediaType::Handlers::RESTfulRDFaWriter
- Inherits:
-
RDF::RDFa::Writer
- Object
- RDF::RDFa::Writer
- RoadForest::MediaType::Handlers::RESTfulRDFaWriter
- Defined in:
- lib/roadforest/content-handling/type-handlers/rdfa.rb
Constant Summary collapse
- HAML =
::RDF::RDFa::Writer::BASE_HAML.merge(:property_values => %q{ - objects.each do |object| / = object.inspect %div.property %span.label = get_predicate_name(predicate) %ul - objects.each do |object| - if res = yield(object, :inlist => inlist, :element => :li) != res - elsif object.node? %li{:property => get_curie(predicate), :resource => get_curie(object), :inlist => inlist}= get_curie(object) - elsif object.uri? %li %a{:property => get_curie(predicate), :href => object.to_s, :inlist => inlist}= object.to_s - elsif object.datatype == RDF.XMLLiteral %li{:property => get_curie(predicate), :lang => get_lang(object), :datatype => get_curie(object.datatype), :inlist => inlist}<!= get_value(object) - else %li{:property => get_curie(predicate), :content => get_content(object), :lang => get_lang(object), :datatype => get_dt_curie(object), :inlist => inlist}= escape_entities(get_value(object)) })
Instance Method Summary collapse
-
#initialize(output = $stdout, options = nil, &block) ⇒ RESTfulRDFaWriter
constructor
A new instance of RESTfulRDFaWriter.
-
#predicate(predicate, objects, options = nil) ⇒ String
Write a predicate with one or more values.
-
#render_property(predicate, objects, options = {}) {|object| ... } ⇒ Object
Render a single- or multi-valued predicate using ‘haml_template` or `haml_template`.
Constructor Details
#initialize(output = $stdout, options = nil, &block) ⇒ RESTfulRDFaWriter
Returns a new instance of RESTfulRDFaWriter.
29 30 31 32 33 |
# File 'lib/roadforest/content-handling/type-handlers/rdfa.rb', line 29 def initialize(output = $stdout, = nil, &block) ||= {} = {:haml => HAML} super(output, , &block) end |
Instance Method Details
#predicate(predicate, objects, options = nil) ⇒ String
Write a predicate with one or more values.
Values may be a combination of Literal and Resource (Node or URI).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/roadforest/content-handling/type-handlers/rdfa.rb', line 44 def predicate(predicate, objects, = nil) add_debug {"predicate: #{predicate.inspect}, objects: #{objects}"} return if objects.to_a.empty? add_debug {"predicate: #{get_curie(predicate)}"} render_property(predicate, objects, || {}) do |o, opts| # Yields each object, for potential recursive definition. # If nil is returned, a leaf is produced opts = {:rel => get_curie(predicate), :element => (:li if objects.length > 1)}.merge(opts||{}) if !is_done?(o) && @subjects.include?(o) depth {subject(o, opts)} end end end |
#render_property(predicate, objects, options = {}) {|object| ... } ⇒ Object
Render a single- or multi-valued predicate using ‘haml_template` or `haml_template`. Yields each object for optional rendering. The block should only render for recursive subject definitions (i.e., where the object is also a subject and is rendered underneath the first referencing subject).
If a multi-valued property definition is not found within the template, the writer will use the single-valued property definition multiple times.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/roadforest/content-handling/type-handlers/rdfa.rb', line 86 def render_property(predicate, objects, = {}, &block) add_debug {"render_property(#{predicate}): #{objects.inspect}"} # If there are multiple objects, and no :property_values is defined, call recursively with # each object template = [:haml] template ||= objects.length > 1 ? haml_template[:property_values] : haml_template[:property_value] # Separate out the objects which are lists and render separately list_objects = objects.select {|o| o != ::RDF.nil && ::RDF::List.new(o, @graph).valid?} unless list_objects.empty? # Render non-list objects add_debug {"properties with lists: non-lists: #{objects - list_objects} lists: #{list_objects}"} nl = render_property(predicate, objects - list_objects, , &block) unless objects == list_objects return nl.to_s + list_objects.map do |object| # Render each list as multiple properties and set :inlist to true list = ::RDF::List.new(object, @graph) list.each_statement {|st| subject_done(st.subject)} add_debug {"list: #{list.inspect} #{list.to_a}"} render_property(predicate, list.to_a, .merge(:inlist => "true"), &block) end.join(" ") end if objects.length > 1 && template.nil? # Uf there is no property_values template, render each property using property_value template objects.map do |object| render_property(predicate, [object], , &block) end.join(" ") else raise ::RDF::WriterError, "Missing property template" if template.nil? template = [:haml] || ( objects.to_a.length > 1 && haml_template.has_key?(:property_values) ? :property_values : :property_value) = { :objects => objects, :object => objects.first, :predicate => predicate, :property => get_curie(predicate), :rel => get_curie(predicate), :inlist => nil, }.merge() hamlify(template, ) do |object, | yield(object, ) if block_given? end end end |