lib/ezid/identifier.rb in ezid-client-0.9.1 vs lib/ezid/identifier.rb in ezid-client-0.10.0

- old
+ new

@@ -1,22 +1,18 @@ -require "forwardable" - module Ezid # # Represents an EZID identifier as a resource. # + # Ezid::Identifier delegates access to registered metadata elements through #method_missing. + # # @api public # class Identifier - extend Forwardable attr_reader :id, :client attr_accessor :shoulder, :metadata - def_delegators :metadata, *(Metadata.elements.readers) - def_delegators :metadata, *(Metadata.elements.writers) - # Attributes to display on inspect INSPECT_ATTRS = %w( id status target created ) # EZID status terms PUBLIC = "public" @@ -124,14 +120,15 @@ clear_metadata self end # Deletes the identifier from EZID + # @see http://ezid.cdlib.org/doc/apidoc.html#operation-delete-identifier # @return [Ezid::Identifier] the identifier # @raise [Ezid::Error] def delete - raise Error, "Status must be \"reserved\" to delete (status: \"#{status}\")." unless reserved? + raise Error, "Only persisted, reserved identifiers may be deleted: #{inspect}." unless deletable? client.delete_identifier(id) @deleted = true reset end @@ -148,42 +145,73 @@ end # Is the identifier unavailable? # @return [Boolean] def unavailable? - status == UNAVAILABLE + status =~ /^#{UNAVAILABLE}/ end - private - - def refresh_metadata - response = client.get_identifier_metadata(id) - @metadata = Metadata.new(response.metadata) + # Is the identifier deletable? + # @return [Boolean] + def deletable? + persisted? && reserved? end - def clear_metadata - @metadata.clear + # Mark the identifier as unavailable + # @param reason [String] an optional reason + # @return [String] the new status + def unavailable!(reason = nil) + raise Error, "Cannot make a reserved identifier unavailable." if persisted? && reserved? + value = UNAVAILABLE + value << " | #{reason}" if reason + self.status = value end - def modify - client.modify_identifier(id, metadata) + # Mark the identifier as public + # @return [String] the new status + def public! + self.status = PUBLIC end - def create_or_mint - id ? create : mint - end + protected - def mint - response = client.mint_identifier(shoulder, metadata) - @id = response.id - end + def method_missing(method, *args) + metadata.send(method, *args) + rescue NoMethodError + super + end - def create - client.create_identifier(id, metadata) - end + private - def init_metadata(args) - @metadata = Metadata.new(args.delete(:metadata)) - update_metadata(self.class.defaults.merge(args)) - end + def refresh_metadata + response = client.get_identifier_metadata(id) + @metadata = Metadata.new(response.metadata) + end + + def clear_metadata + @metadata.clear + end + + def modify + client.modify_identifier(id, metadata) + end + + def create_or_mint + id ? create : mint + end + + def mint + response = client.mint_identifier(shoulder, metadata) + @id = response.id + end + + def create + client.create_identifier(id, metadata) + end + + def init_metadata(args) + @metadata = Metadata.new(args.delete(:metadata)) + update_metadata(self.class.defaults.merge(args)) + end + end end