module Triplifier::Node

Public Class Methods

included(base) click to toggle source
# File lib/triplifier/node.rb, line 6
def included(base)
        base.extend(ClassMethods)
end

Public Instance Methods

eval(object) click to toggle source
# File lib/triplifier/node.rb, line 75
def eval(object)
        if object.kind_of? Proc
                object = instance_eval(&object)
        elsif object.kind_of? Class
                object = object.new(self)
        end
        object = [object] unless object.kind_of? Array
        object
end
objects(predicate = nil) click to toggle source
# File lib/triplifier/node.rb, line 61
def objects(predicate = nil)
        objs = []
        if predicate.nil?
                predicates.each do |pred|
                        objs.concat objects(pred)
                end
        else
                self.class.subjects[predicate].each do |obj|
                        objs.concat(eval(obj))
                end
        end
        objs
end
predicates() click to toggle source
# File lib/triplifier/node.rb, line 57
def predicates
        self.class.subjects.keys
end
resource() click to toggle source
# File lib/triplifier/node.rb, line 48
def resource
        res = self.class.resource
        if res.kind_of? Proc
                res = instance_eval(&res)
        end
        res = Triplifier::URI.new(res) unless Triplifier::URI.is_uri?(res)
        res
end
to_turtle() click to toggle source
# File lib/triplifier/node.rb, line 15
def to_turtle
        subject = resource
        ttl = ""
        predicates.each do |predicate|
                objects(predicate).each do |object|
                        turtlified, extras = turtlify(object)
                        pred = predicate.to_s.split('@')
                        if pred.length == 2
                                predicate, suffix = pred
                                suffix = "@#{suffix}"
                        else
                                suffix = ''
                        end
                        pred = predicate.to_s.split('^^')
                        if pred.length == 2
                                predicate, suffix = pred
                                suffix = "^^#{suffix}"
                        else
                                suffix = ''
                        end
                        ttl += "#{subject} #{predicate} #{turtlified}#{suffix} .\n" unless turtlified.nil?
                        if !extras.nil? && extras.kind_of?(SimpleNode)
                                ttl += extras.to_turtle
                        end
                end
        end
        self.class.defines.each do |klass|
                object = klass.new(self)
                ttl += object.to_turtle
        end
        ttl
end
turtlify(object) click to toggle source
# File lib/triplifier/node.rb, line 85
def turtlify(object)
        if URI.is_uri? object
                return [object.to_s]
        elsif object.kind_of? Numeric
                return [object.to_s]
        elsif object.kind_of? String
                object = object.gsub("\r","\n").gsub('\\','\\\\\\\\').gsub('"','\\"')
                return nil if object.length == 0
                return ["\"#{object}\""] if object.scan("\n").length == 0
                return ["\"\"\"#{object}\"\"\""]
        elsif object.kind_of?(TrueClass) || object.kind_of?(FalseClass)
                ["'#{object}'^^xsd:boolean"]
        elsif object.kind_of? Date
                ["\"#{object.iso8601}\"^^xsd:date"]
        elsif object.respond_to? :resource
                [object.resource, object]
        end

end
uri(string) click to toggle source
# File lib/triplifier/node.rb, line 11
def uri(string)
        self.class.uri(string)
end