lib/ezid/metadata.rb in ezid-client-1.3.0 vs lib/ezid/metadata.rb in ezid-client-1.4.0
- old
+ new
@@ -1,16 +1,14 @@
require "hashie"
-require_relative "reserved_metadata"
module Ezid
#
# EZID metadata collection for an identifier.
#
# @api private
#
class Metadata < Hashie::Mash
- include ReservedMetadata
# EZID metadata field/value separator
ANVL_SEPARATOR = ": "
# EZID metadata field value separator
ELEMENT_VALUE_SEPARATOR = " | "
@@ -28,17 +26,39 @@
# A line continuation
LINE_CONTINUATION_RE = /\r?\n\s+/
# A line ending
LINE_ENDING_RE = /\r?\n/
# @api private
- RESERVED_ALIASES = %w(
- coowners datacenter export owner ownergroup
- profile shadowedby shadows status target
- ).freeze
- def initialize(data={})
- super coerce(data)
+ #
+ # EZID reserved metadata elements
+ #
+ # @see http://ezid.cdlib.org/doc/apidoc.html#internal-metadata
+ #
+ COOWNERS = "_coowners".freeze
+ CREATED = "_created".freeze
+ DATACENTER = "_datacenter".freeze
+ EXPORT = "_export".freeze
+ OWNER = "_owner".freeze
+ OWNERGROUP = "_ownergroup".freeze
+ PROFILE = "_profile".freeze
+ SHADOWEDBY = "_shadowedby".freeze
+ SHADOWS = "_shadows".freeze
+ STATUS = "_status".freeze
+ TARGET = "_target".freeze
+ UPDATED = "_updated".freeze
+ RESERVED = [
+ COOWNERS, CREATED, DATACENTER, EXPORT, OWNER, OWNERGROUP,
+ PROFILE, SHADOWEDBY, SHADOWS, STATUS, TARGET, UPDATED
+ ].freeze
+ READONLY = [
+ CREATED, DATACENTER, OWNER, OWNERGROUP, SHADOWEDBY, SHADOWS, UPDATED
+ ].freeze
+
+ def initialize(data=nil)
+ super()
+ update(data) if data
end
def elements
warn "[DEPRECATION] `elements` is deprecated and will be removed in ezid-client 2.0." \
" Use the Ezid::Metadata instance itself instead."
@@ -51,10 +71,18 @@
def updated
to_time(_updated)
end
+ def update(data)
+ super coerce(data)
+ end
+
+ def replace(data)
+ super coerce(data)
+ end
+
# Output metadata in EZID ANVL format
# @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies
# @return [String] the ANVL output
def to_anvl(include_readonly = true)
hsh = to_h
@@ -72,17 +100,17 @@
protected
# Overrides Hashie::Mash
def convert_key(key)
- k = super
- if RESERVED_ALIASES.include?(k)
- "_#{k}"
- elsif k =~ /\A(dc|datacite|erc)_/
- k.sub(/_/, ".")
+ converted = super
+ if RESERVED.include?("_#{converted}")
+ "_#{converted}"
+ elsif converted =~ /\A(dc|datacite|erc)_/
+ converted.sub(/_/, ".")
else
- k
+ converted
end
end
private
@@ -91,12 +119,10 @@
(time == 0) ? nil : Time.at(time).utc
end
# Coerce data into a Hash of elements
def coerce(data)
- data.to_h
- rescue NoMethodError
- coerce_string(data)
+ data.respond_to?(:to_h) ? data.to_h : coerce_string(data)
end
# Escape string for sending to EZID host
def escape(regexp, value)
value.gsub(regexp) { |m| URI.encode_www_form_component(m.force_encoding(Encoding::UTF_8)) }