Sha256: 95bd7151b07c7d2ec9c47437b9c03474ab16e4cadfbb32a7ff0f76cbaf476b37

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

#   Copyright 2011 innoQ Deutschland GmbH
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

module IqRdf
  class Collection

    attr_reader :elements

    def initialize(collection)
      @elements = []
      collection.each do |element|
        element = Literal.new(element) unless element.is_a?(IqRdf::Uri)
        @elements << element
      end
    end

    def to_s(lang = nil)
      "(#{@elements.map{|e| e.to_s(lang)}.join(" ")})"
    end

    def build_xml(xml, elements = nil, &block)
      elements ||= @elements.dup
      block.call({},
        lambda {
          xml.rdf :List do
            elements.shift.build_xml(xml) do |*args|
              xml.rdf(:first, *args)
            end
            if elements.size > 0
              build_xml(xml, elements) do |opts, block|
                xml.rdf :rest do
                  block.call
                end
              end
            else
              xml.rdf :rest, "rdf:resource" => Rdf.nil.full_uri
            end
          end
        }
      )
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
iq_rdf-0.1.2 lib/iq_rdf/collection.rb
iq_rdf-0.1.0 lib/iq_rdf/collection.rb
iq_rdf-0.0.15 lib/iq_rdf/collection.rb