Skip to content

Commit 8551341

Browse files
committed
Implement "patch" cli command.
1 parent a3f420d commit 8551341

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

lib/ld/patch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module LD
99
#
1010
# @author [Gregg Kellogg](http://greggkellogg.net/)
1111
module Patch
12+
require 'ld/patch/format'
1213
autoload :Algebra, 'ld/patch/algebra'
1314
autoload :Meta, 'ld/patch/meta'
1415
autoload :Parser, 'ld/patch/parser'

lib/ld/patch/format.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module LD::Patch
2+
##
3+
# LD::Patch format specification. Note that this format does not define any readers or writers.
4+
#
5+
# @example Obtaining an LD Patch format class
6+
# RDF::Format.for(:ldp) #=> LD::Patch::Format
7+
# RDF::Format.for("etc/foaf.ldp")
8+
# RDF::Format.for(:file_name => "etc/foaf.ldp")
9+
# RDF::Format.for(file_extension: "ldp")
10+
# RDF::Format.for(:content_type => "text/ldpatch")
11+
#
12+
# @see http://www.w3.org/TR/ldpatch/
13+
class Format < RDF::Format
14+
content_type 'text/ldpatch', extension: :ldp
15+
content_encoding 'utf-8'
16+
17+
##
18+
# Hash of CLI commands appropriate for this format
19+
# @return [Hash{Symbol => Lambda(Array, Hash)}]
20+
def self.cli_commands
21+
{
22+
patch: {
23+
description: "Patch the current graph using a URI Encoded patch file, or a referenced path file/URI",
24+
help: "patch [--patch 'patch'] [--patch-file file]",
25+
parse: true,
26+
lambda: -> (argv, opts) do
27+
opts[:patch] ||= RDF::Util::File.open_file(opts[:patch_file]) {|f| f.read}
28+
raise ArgumentError, "Patching requires a URI encoded patch or reference to patch resource" unless opts[:patch]
29+
$stdout.puts "Patch"
30+
patch = LD::Patch.parse(opts[:patch], base_uri: opts.fetch(:patch_file, "http://rubygems.org/gems/ld-patch"))
31+
RDF::CLI.repository.query(patch)
32+
end,
33+
options: [
34+
RDF::CLI::Option.new(
35+
symbol: :patch,
36+
datatype: String,
37+
on: ["--patch STRING"],
38+
description: "Patch in URI encoded format"
39+
) {|v| URI.decode(v)},
40+
RDF::CLI::Option.new(
41+
symbol: :patch_file,
42+
datatype: String,
43+
on: ["--patch-file URI"],
44+
description: "URI of patch file"
45+
) {|v| RDF::URI(v)},
46+
]
47+
}
48+
}
49+
end
50+
51+
def self.to_sym; :ldpatch; end
52+
end
53+
end

spec/format_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding: utf-8
2+
$:.unshift "."
3+
require 'spec_helper'
4+
require 'rdf/spec/format'
5+
6+
describe LD::Patch::Format do
7+
it_behaves_like 'an RDF::Format' do
8+
let(:format_class) {JSON::LD::Format}
9+
end
10+
11+
describe ".for" do
12+
formats = [
13+
:ldpatch,
14+
"etc/doap.ldp",
15+
{file_name: 'etc/doap.ldp'},
16+
{file_extension: 'ldp'},
17+
{content_type: 'text/ldpatch'},
18+
].each do |arg|
19+
it "discovers with #{arg.inspect}" do
20+
expect(RDF::Format.for(arg)).to eq described_class
21+
end
22+
end
23+
end
24+
25+
describe "#to_sym" do
26+
specify {expect(described_class.to_sym).to eq :ldpatch}
27+
end
28+
29+
describe ".cli_commands" do
30+
require 'rdf/cli'
31+
let(:nt) {File.expand_path("../test-files/1triple.nt", __FILE__)}
32+
let(:patch) {File.expand_path("../test-files/add-1triple.ldpatch", __FILE__)}
33+
let(:patch_enc) {File.read(patch)} # Not encoded, since decode done in option parsing
34+
35+
describe "#patch" do
36+
it "patches from file" do
37+
expect {RDF::CLI.exec(["patch", "serialize", nt], patch_file: patch)}.to write.to(:output)
38+
end
39+
it "patches from argument" do
40+
expect {RDF::CLI.exec(["patch", "serialize", nt], patch: patch_enc)}.to write.to(:output)
41+
end
42+
end
43+
end
44+
end

spec/test-files/1triple.nt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<http://example.org/s1> <http://example.org/p1> <http://example.org/o1> .
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add { <http://example.org/s2> <http://example.org/p2> <http://example.org/o2> } .

0 commit comments

Comments
 (0)