Class: IqRdf::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/iq_rdf/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Collection

Returns a new instance of Collection.



20
21
22
23
24
25
26
# File 'lib/iq_rdf/collection.rb', line 20

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

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



18
19
20
# File 'lib/iq_rdf/collection.rb', line 18

def elements
  @elements
end

Instance Method Details

#build_xml(xml, elements = nil, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/iq_rdf/collection.rb', line 32

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

#to_s(options = {}) ⇒ Object



28
29
30
# File 'lib/iq_rdf/collection.rb', line 28

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