lib/ezid/identifier.rb in ezid-client-1.6.0 vs lib/ezid/identifier.rb in ezid-client-1.7.0
- old
+ new
@@ -63,24 +63,40 @@
# @param id [String] the EZID identifier
# @param metadata [Hash] the metadata to update on the identifier
# @return [Ezid::Identifier] the identifier
# @raise [Ezid::IdentifierNotFoundError]
def modify(id, metadata)
- i = allocate
- i.id = id
- i.update_metadata(metadata)
- i.modify!
+ allocate.tap do |i|
+ i.id = id
+ i.update_metadata(metadata)
+ i.modify!
+ end
end
+ # Loads an identifier with provided remote metadata
+ # The main purpose is to provide an API in a batch processing
+ # context to instantiate Identifiers from a BatchDownload.
+ # @see #load_metadata!
+ # @param id [String] the EZID identifier
+ # @param metadata [String, Hash, Ezid::Metadata] the provided metadata
+ # @return [Ezid::Identifier] the identifier
+ def load(id, metadata = nil)
+ allocate.tap do |i|
+ i.id = id
+ i.load_metadata!(metadata)
+ end
+ end
+
# Retrieves an identifier
# @param id [String] the EZID identifier to find
# @return [Ezid::Identifier] the identifier
# @raise [Ezid::IdentifierNotFoundError] if the identifier does not exist in EZID
def find(id)
- i = allocate
- i.id = id
- i.load_metadata
+ allocate.tap do |i|
+ i.id = id
+ i.load_metadata
+ end
end
end
self.defaults = {}
@@ -130,15 +146,20 @@
# Returns the identifier metadata
# @return [Ezid::Metadata] the metadata
def metadata(_=nil)
if !_.nil?
- warn "[DEPRECATION] The parameter of `metadata` is deprecated and will be removed in 2.0. (called from #{caller.first})"
+ warn "[DEPRECATION] The parameter of `metadata` is ignored and will be removed in 2.0. " \
+ "(called from #{caller.first})"
end
- @metadata ||= Metadata.new
+ remote_metadata.merge(local_metadata).freeze
end
+ def local_metadata
+ @local_metadata ||= Metadata.new
+ end
+
def remote_metadata
@remote_metadata ||= Metadata.new
end
# Persist the identifer and/or metadata to EZID.
@@ -170,11 +191,11 @@
# Updates the metadata
# @param attrs [Hash] the metadata
# @return [Ezid::Identifier] the identifier
def update_metadata(attrs={})
- metadata.update(attrs)
+ local_metadata.update(attrs)
self
end
# Is the identifier persisted?
# @return [Boolean]
@@ -201,21 +222,33 @@
def reload
warn "[DEPRECATION] `reload` is deprecated and will be removed in version 2.0. Use `load_metadata` instead. (called from #{caller.first})"
load_metadata
end
- # Loads the metadata from EZID
+ # Loads the metadata from EZID and marks the identifier as persisted.
# @return [Ezid::Identifier] the identifier
- # @raise [Ezid::Error]
+ # @raise [Ezid::Error] the identifier is not found or other error.
def load_metadata
response = client.get_identifier_metadata(id)
- # self.remote_metadata = Metadata.new(response.metadata)
- remote_metadata.replace(response.metadata)
+ load_remote_metadata(response.metadata)
persists!
self
end
+ # Loads provided metadata and marks the identifier as persisted.
+ # The main purpose is to provide an API in a batch processing
+ # context to instantiate Identifiers from a BatchDownload.
+ # @see Ezid::BatchEnumerator
+ # @see .load
+ # @param metadata [String, Hash, Ezid::Metadata] the provided metadata
+ # @return [Ezid::Identifier] the identifier
+ def load_metadata!(metadata)
+ load_remote_metadata(metadata)
+ persists!
+ self
+ end
+
# Empties the (local) metadata (changes will be lost!)
# @return [Ezid::Identifier] the identifier
def reset
warn "[DEPRECATION] `reset` is deprecated and will be removed in 2.0. Use `reset_metadata` instead. (called from #{caller.first})"
reset_metadata
@@ -285,11 +318,11 @@
def client
@client ||= Client.new
end
def reset_metadata
- metadata.clear unless metadata.empty?
+ local_metadata.clear unless local_metadata.empty?
remote_metadata.clear unless remote_metadata.empty?
end
protected
@@ -300,33 +333,33 @@
end
private
def local_or_remote_metadata(*args)
- value = metadata.send(*args)
+ value = local_metadata.send(*args)
if value.nil? && persisted?
load_metadata if remote_metadata.empty?
value = remote_metadata.send(*args)
end
value
end
def modify
- client.modify_identifier(id, metadata)
+ client.modify_identifier(id, local_metadata)
end
def create_or_mint
id ? create : mint
end
def mint
- response = client.mint_identifier(shoulder, metadata)
+ response = client.mint_identifier(shoulder, local_metadata)
self.id = response.id
end
def create
- client.create_identifier(id, metadata)
+ client.create_identifier(id, local_metadata)
end
def persist
persisted? ? modify : create_or_mint
persists!
@@ -336,9 +369,13 @@
self.persisted = true
end
def apply_default_metadata
update_metadata(self.class.defaults)
+ end
+
+ def load_remote_metadata(metadata)
+ remote_metadata.replace(metadata)
end
end
end