Class: RoadForest::MediaType::Handlers::RESTfulRDFaWriter

Inherits:
RDF::RDFa::Writer
  • Object
show all
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

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, options = nil, &block)
  options ||= {}
  options = {:haml => HAML}
  super(output, options, &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).

Parameters:

  • predicate (RDF::Resource)

    Predicate to serialize

  • objects (Array<RDF::Resource>)

    Objects to serialize

Returns:

  • (String)


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, options = nil)
  add_debug {"predicate: #{predicate.inspect}, objects: #{objects}"}

  return if objects.to_a.empty?

  add_debug {"predicate: #{get_curie(predicate)}"}
  render_property(predicate, objects, options || {}) 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.

Parameters:

  • predicate (Array<RDF::Resource>)

    Predicate to render.

  • objects (Array<RDF::Resource>)

    List of objects to render. If the list contains only a single element, the :property_value template will be used. Otherwise, the :property_values template is used.

  • options (Hash{Symbol => Object}) (defaults to: {})

    Rendering options passed to Haml render.

Options Hash (options):

  • haml (String) — default: haml_template[:property_value], haml_template[:property_values]

    Haml template to render. Otherwise, uses ‘haml_template or haml_template` depending on the cardinality of objects.

Yields:

  • (object)

    Yields object.

Yield Parameters:

  • object (RDF::Resource)

Yield Returns:

  • (String, nil)

    The block should only return a string for recursive object definitions.

Returns:

  • String The rendered document is returned as a string



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, options = {}, &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 = options[: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, options, &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, options.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], options, &block)
    end.join(" ")
  else
    raise ::RDF::WriterError, "Missing property template" if template.nil?

    template = options[:haml] || (
      objects.to_a.length > 1 &&
      haml_template.has_key?(:property_values) ?
      :property_values :
      :property_value)
      options = {
        :objects    => objects,
        :object     => objects.first,
        :predicate  => predicate,
        :property   => get_curie(predicate),
        :rel        => get_curie(predicate),
        :inlist     => nil,
      }.merge(options)
      hamlify(template, options) do |object, options|
        yield(object, options) if block_given?
      end
  end
end