|
| 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 |
0 commit comments