lib/ezid/identifier.rb in ezid-client-0.5.0 vs lib/ezid/identifier.rb in ezid-client-0.6.0

- old
+ new

@@ -21,11 +21,13 @@ # EZID status terms PUBLIC = "public" RESERVED = "reserved" UNAVAILABLE = "unavailable" - class << self + class << self + attr_accessor :defaults + # Creates or mints an identifier (depending on arguments) # @see #save # @return [Ezid::Identifier] the new identifier # @raise [Ezid::Error] def create(attrs = {}) @@ -40,17 +42,18 @@ identifier = new(id: id) identifier.reload end end + self.defaults = {} + def initialize(args={}) @client = args.delete(:client) || Client.new @id = args.delete(:id) @shoulder = args.delete(:shoulder) - @metadata = Metadata.new(args.delete(:metadata)) - update_metadata(args) @deleted = false + init_metadata(args) end def inspect attrs = if deleted? "id=\"#{id}\" DELETED" @@ -71,15 +74,11 @@ # @return [Ezid::Identifier] the identifier # @raise [Ezid::Error] if the identifier is deleted, or the host responds # with an error status. def save raise Error, "Cannot save a deleted identifier." if deleted? - if persisted? - modify - else - create_or_mint - end + persisted? ? modify : create_or_mint reload end # Updates the metadata # @param attrs [Hash] the metadata @@ -152,17 +151,10 @@ # @return [Boolean] def unavailable? status == UNAVAILABLE end - protected - - def method_missing(name, *args) - return metadata.send(name, *args) if metadata.respond_to?(name) - super - end - private def refresh_metadata response = client.get_identifier_metadata(id) @metadata = Metadata.new(response.metadata) @@ -175,17 +167,11 @@ def modify client.modify_identifier(id, metadata) end def create_or_mint - if id - create - elsif shoulder - mint - else - raise Error, "Unable to create or mint identifier when neither `id' nor `shoulder' present." - end + id ? create : mint end def mint response = client.mint_identifier(shoulder, metadata) @id = response.id @@ -193,10 +179,11 @@ def create client.create_identifier(id, metadata) end - def init_metadata(args={}) + def init_metadata(args) + @metadata = Metadata.new(args.delete(:metadata)) + update_metadata(self.class.defaults.merge(args)) end - end end