• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-rdf / rdf-json / 7451961379

08 Jan 2024 07:01PM UTC coverage: 92.517%. Remained the same
7451961379

push

github

gkellogg
* CI on Ruby 3.3.
* Reference release notes on GitHub.

136 of 147 relevant lines covered (92.52%)

87.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/lib/rdf/json/extensions.rb
1
module RDF::JSON
2✔
2
  ##
3
  # RDF/JSON extensions for [RDF.rb](https://github.com/ruby-rdf/rdf) core classes
4
  # and mixins.
5
  #
6
  # Classes are extended with two new instance methods:
7
  #
8
  # * `#to_rdf_json` returns the RDF/JSON representation as a `Hash` object.
9
  # * `#to_rdf_json.to_json` returns the serialized RDF/JSON representation as a string.
10
  #
11
  # @example Serializing blank nodes into RDF/JSON format
12
  #   RDF::Node.new(id).to_rdf_json.to_json
13
  #
14
  # @example Serializing URI references into RDF/JSON format
15
  #   RDF::URI.new("https://rubygems.org/gems/rdf/").to_rdf_json.to_json
16
  #
17
  # @example Serializing plain literals into RDF/JSON format
18
  #   RDF::Literal.new("Hello, world!").to_rdf_json.to_json
19
  #
20
  # @example Serializing language-tagged literals into RDF/JSON format
21
  #   RDF::Literal.new("Hello, world!", :language => 'en-US').to_rdf_json.to_json
22
  #
23
  # @example Serializing datatyped literals into RDF/JSON format
24
  #   RDF::Literal.new(3.1415).to_rdf_json.to_json
25
  #   RDF::Literal.new('true', :datatype => RDF::XSD.boolean).to_rdf_json.to_json
26
  #
27
  # @example Serializing statements into RDF/JSON format
28
  #   RDF::Statement.new(s, p, o).to_rdf_json.to_json
29
  #
30
  # @example Serializing enumerables into RDF/JSON format
31
  #   [RDF::Statement.new(s, p, o)].extend(RDF::Enumerable).to_rdf_json.to_json
32
  #
33
  module Extensions
2✔
34
    ##
35
    # @private
36
    def self.install!
2✔
37
      self.constants.each do |klass|
2✔
38
        RDF.const_get(klass).send(:include, self.const_get(klass))
16✔
39
      end
40
    end
41

42
    ##
43
    # RDF/JSON extensions for `RDF::Node`.
44
    module Node
2✔
45
      ##
46
      # Returns the RDF/JSON representation of this blank node.
47
      #
48
      # @return [Hash]
49
      def to_rdf_json
2✔
50
        {:type => :bnode, :value => to_s}
38✔
51
      end
52
    end # Node
53

54
    ##
55
    # RDF/JSON extensions for `RDF::URI`.
56
    module URI
2✔
57
      ##
58
      # Returns the RDF/JSON representation of this URI reference.
59
      #
60
      # @return [Hash]
61
      def to_rdf_json
2✔
62
        {:type => :uri, :value => to_s}
8✔
63
      end
64
    end # URI
65

66
    ##
67
    # RDF/JSON extensions for `RDF::Literal`.
68
    module Literal
2✔
69
      ##
70
      # Returns the RDF/JSON representation of this literal.
71
      #
72
      # @return [Hash]
73
      def to_rdf_json
2✔
74
        case
75
          when has_datatype?
42✔
76
            {:type => :literal, :value => value.to_s, :datatype => datatype.to_s}
6✔
77
          when has_language?
78
            {:type => :literal, :value => value.to_s, :lang => language.to_s}
10✔
79
          else
80
            {:type => :literal, :value => value.to_s}
26✔
81
        end
82
      end
83
    end # Literal
84

85
    ##
86
    # RDF/JSON extensions for `RDF::Statement`.
87
    module Statement
2✔
88
      ##
89
      # Returns the RDF/JSON representation of this statement.
90
      #
91
      # @return [Hash]
92
      def to_rdf_json
2✔
93
        {subject.to_s => {predicate.to_s => [object.to_rdf_json]}}
8✔
94
      end
95
    end # Statement
96

97
    ##
98
    # RDF/JSON extensions for `RDF::Enumerable`.
99
    module Enumerable
2✔
100
      ##
101
      # Returns the RDF/JSON representation of this object.
102
      #
103
      # @return [Hash]
104
      def to_rdf_json
2✔
105
        json = {}
10✔
106
        each_statement do |statement|
10✔
107
          s = statement.subject.to_s
10✔
108
          p = statement.predicate.to_s
10✔
109
          o = statement.object.is_a?(RDF::Value) ? statement.object : RDF::Literal.new(statement.object)
10✔
110
          json[s]    ||= {}
10✔
111
          json[s][p] ||= []
10✔
112
          json[s][p] << o.to_rdf_json
10✔
113
        end
114
        json
10✔
115
      end
116
    end # Enumerable
117

118
    ##
119
    # RDF/JSON extensions for `RDF::Graph`.
120
    module Graph
2✔
121
      include Enumerable
2✔
122
    end # Graph
123

124
    ##
125
    # RDF/JSON extensions for `RDF::Repository`.
126
    module Repository
2✔
127
      include Enumerable
2✔
128
    end # Repository
129

130
    ##
131
    # RDF/JSON extensions for `RDF::Transaction`.
132
    module Transaction
2✔
133
      ##
134
      # Returns the serialized RDF/JSON representation of this object.
135
      #
136
      # @return [Hash]
137
      def to_rdf_json
2✔
138
        json = options.dup.to_hash rescue {}
×
139
        json.merge!({
×
140
          :graph  => graph ? graph.to_uri.to_s : nil,
×
141
          :delete => deletes.to_rdf_json,
142
          :insert => inserts.to_rdf_json,
143
        })
144
      end
145
    end # Transaction
146
  end # Extensions
147

148
  Extensions.install!
2✔
149
end # RDF::JSON
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc