Class: Ezid::Identifier
- Inherits:
-
Object
- Object
- Ezid::Identifier
- Defined in:
- lib/ezid/identifier.rb
Overview
Represents an EZID identifier as a resource.
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary collapse
-
.create(*args) ⇒ Ezid::Identifier
Creates or mints an identifier (depending on arguments).
-
.find(id) ⇒ Ezid::Identifier
Retrieves an identifier.
-
.load(id, metadata = nil) ⇒ Ezid::Identifier
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.
-
.mint(*args) ⇒ Ezid::Identifier
Mints a new identifier.
-
.modify(id, metadata) ⇒ Ezid::Identifier
Modifies the metadata of an existing identifier.
Instance Method Summary collapse
- #client ⇒ Object
-
#deletable? ⇒ Boolean
Is the identifier deletable?.
-
#delete ⇒ Ezid::Identifier
Deletes the identifier from EZID.
-
#deleted? ⇒ Boolean
Has the identifier been deleted?.
-
#initialize(*args) {|_self| ... } ⇒ Identifier
constructor
A new instance of Identifier.
- #inspect ⇒ Object
-
#load_metadata ⇒ Ezid::Identifier
Loads the metadata from EZID and marks the identifier as persisted.
-
#load_metadata!(metadata) ⇒ Ezid::Identifier
Loads provided metadata and marks the identifier as persisted.
- #local_metadata ⇒ Object
-
#metadata(_ = nil) ⇒ Ezid::Metadata
Returns the identifier metadata.
-
#modify! ⇒ Ezid::Identifier
Force a modification of the EZID identifier – i.e., assumes previously persisted without confirmation.
-
#persisted? ⇒ Boolean
Is the identifier persisted?.
-
#public! ⇒ String
Mark the identifier as public.
-
#public? ⇒ Boolean
Is the identifier public?.
-
#reload ⇒ Object
deprecated
Deprecated.
Use #load_metadata instead.
- #remote_metadata ⇒ Object
-
#reserved? ⇒ Boolean
Is the identifier reserved?.
-
#reset ⇒ Ezid::Identifier
Empties the (local) metadata (changes will be lost!).
- #reset_metadata ⇒ Object
-
#save ⇒ Ezid::Identifier
Persist the identifer and/or metadata to EZID.
- #to_s ⇒ Object
-
#unavailable!(reason = nil) ⇒ String
Mark the identifier as unavailable.
-
#unavailable? ⇒ Boolean
Is the identifier unavailable?.
-
#update(data = {}) ⇒ Ezid::Identifier
Updates the metadata and saves the identifier.
-
#update_metadata(attrs = {}) ⇒ Ezid::Identifier
Updates the metadata.
Constructor Details
#initialize(*args) {|_self| ... } ⇒ Identifier
Returns a new instance of Identifier.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ezid/identifier.rb', line 103 def initialize(*args) raise ArgumentError, "`new` receives 0-2 arguments." if args.size > 2 = args.last.is_a?(Hash) ? args.pop : nil @id = args.first if if id = .delete(:id) warn "[DEPRECATION] The `:id` hash option is deprecated and will raise an exception in 2.0. The id should be passed as the first argument to `new` or set explicitly using the attribute writer. (called by #{caller.first})" if @id raise ArgumentError, "`id' specified in both positional argument and (deprecated) hash option." end @id = id end if shoulder = .delete(:shoulder) warn "[DEPRECATION] The `:shoulder` hash option is deprecated and will raise an exception in 2.0. Use `Ezid::Identifier.mint(shoulder, metadata)` to mint an identifier. (called by #{caller.first})" @shoulder = shoulder end if client = .delete(:client) warn "[DEPRECATION] The `:client` hash option is deprecated and ignored. It will raise an exception in 2.0. See the README for details on configuring `Ezid::Client`." end if anvl = .delete(:metadata) (anvl) end () end yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object (protected)
329 330 331 332 333 |
# File 'lib/ezid/identifier.rb', line 329 def method_missing(*args) (*args) rescue NoMethodError super end |
Class Attribute Details
.defaults ⇒ Object
13 14 15 |
# File 'lib/ezid/identifier.rb', line 13 def defaults @defaults end |
Instance Attribute Details
#id ⇒ Object
9 10 11 |
# File 'lib/ezid/identifier.rb', line 9 def id @id end |
#shoulder ⇒ Object
9 10 11 |
# File 'lib/ezid/identifier.rb', line 9 def shoulder @shoulder end |
Class Method Details
.create(id, metadata = nil) ⇒ Ezid::Identifier .create(metadata = nil) ⇒ Ezid::Identifier
Creates or mints an identifier (depending on arguments)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ezid/identifier.rb', line 27 def create(*args) raise ArgumentError, "`mint` receives 0-2 arguments." if args.size > 2 if args.first.is_a?(Hash) warn "[DEPRECATION] Sending a hash as the first argument to `create` is deprecated and will raise an exception in 2.0. Use `create(id, metadata)` or `mint(metadata)` instead. (called from #{caller.first})" = args.first id = .delete(:id) else id, = args end if id.nil? warn "[DEPRECATION] Calling `create` without an id will raise an exception in 2.0. Use `mint` instead. (called from #{caller.first})" shoulder = ? .delete(:shoulder) : nil mint(shoulder, ) else new(id, ) { |i| i.save } end end |
.find(id) ⇒ Ezid::Identifier
Retrieves an identifier
93 94 95 96 97 98 |
# File 'lib/ezid/identifier.rb', line 93 def find(id) allocate.tap do |i| i.id = id i. end end |
.load(id, metadata = nil) ⇒ Ezid::Identifier
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.
82 83 84 85 86 87 |
# File 'lib/ezid/identifier.rb', line 82 def load(id, = nil) allocate.tap do |i| i.id = id i.() end end |
.mint(shoulder, metadata = nil) ⇒ Ezid::Identifier .mint(metadata = nil) ⇒ Ezid::Identifier
Mints a new identifier
53 54 55 56 57 58 59 60 |
# File 'lib/ezid/identifier.rb', line 53 def mint(*args) raise ArgumentError, "`mint` receives 0-2 arguments." if args.size > 2 = args.last.is_a?(Hash) ? args.pop : nil new() do |i| i.shoulder = args.first i.save end end |
.modify(id, metadata) ⇒ Ezid::Identifier
Modifies the metadata of an existing identifier.
67 68 69 70 71 72 73 |
# File 'lib/ezid/identifier.rb', line 67 def modify(id, ) allocate.tap do |i| i.id = id i.() i.modify! end end |
Instance Method Details
#client ⇒ Object
318 319 320 |
# File 'lib/ezid/identifier.rb', line 318 def client @client ||= Client.new end |
#deletable? ⇒ Boolean
Is the identifier deletable?
291 292 293 |
# File 'lib/ezid/identifier.rb', line 291 def deletable? persisted? && reserved? end |
#delete ⇒ Ezid::Identifier
Deletes the identifier from EZID
262 263 264 265 266 267 268 269 |
# File 'lib/ezid/identifier.rb', line 262 def delete raise Error, "Only persisted, reserved identifiers may be deleted: #{inspect}." unless deletable? client.delete_identifier(id) self.deleted = true self.persisted = false self end |
#deleted? ⇒ Boolean
Has the identifier been deleted?
208 209 210 |
# File 'lib/ezid/identifier.rb', line 208 def deleted? !!deleted end |
#inspect ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ezid/identifier.rb', line 132 def inspect id_val = if id.nil? "NEW" elsif deleted? "#{id} [DELETED]" else id end "#<#{self.class.name} id=#{id_val}>" end |
#load_metadata ⇒ Ezid::Identifier
Loads the metadata from EZID and marks the identifier as persisted.
230 231 232 233 234 235 |
# File 'lib/ezid/identifier.rb', line 230 def response = client.(id) (response.) persists! self end |
#load_metadata!(metadata) ⇒ Ezid::Identifier
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.
244 245 246 247 248 |
# File 'lib/ezid/identifier.rb', line 244 def () () persists! self end |
#local_metadata ⇒ Object
157 158 159 |
# File 'lib/ezid/identifier.rb', line 157 def @local_metadata ||= Metadata.new end |
#metadata(_ = nil) ⇒ Ezid::Metadata
Returns the identifier metadata
149 150 151 152 153 154 155 |
# File 'lib/ezid/identifier.rb', line 149 def (_=nil) if !_.nil? warn "[DEPRECATION] The parameter of `metadata` is ignored and will be removed in 2.0. " \ "(called from #{caller.first})" end .merge().freeze end |
#modify! ⇒ Ezid::Identifier
Force a modification of the EZID identifier – i.e.,
assumes previously persisted without confirmation.
184 185 186 187 188 189 190 |
# File 'lib/ezid/identifier.rb', line 184 def modify! raise Error, "Cannot modify an identifier without and id." if id.nil? modify persists! self end |
#persisted? ⇒ Boolean
Is the identifier persisted?
202 203 204 |
# File 'lib/ezid/identifier.rb', line 202 def persisted? !!persisted end |
#public! ⇒ String
Mark the identifier as public
314 315 316 |
# File 'lib/ezid/identifier.rb', line 314 def public! self.status = Status::PUBLIC end |
#public? ⇒ Boolean
Is the identifier public?
279 280 281 |
# File 'lib/ezid/identifier.rb', line 279 def public? status == Status::PUBLIC end |
#reload ⇒ Object
Use #load_metadata instead.
222 223 224 225 |
# File 'lib/ezid/identifier.rb', line 222 def reload warn "[DEPRECATION] `reload` is deprecated and will be removed in version 2.0. Use `load_metadata` instead. (called from #{caller.first})" end |
#remote_metadata ⇒ Object
161 162 163 |
# File 'lib/ezid/identifier.rb', line 161 def @remote_metadata ||= Metadata.new end |
#reserved? ⇒ Boolean
Is the identifier reserved?
273 274 275 |
# File 'lib/ezid/identifier.rb', line 273 def reserved? status == Status::RESERVED end |
#reset ⇒ Ezid::Identifier
Empties the (local) metadata (changes will be lost!)
252 253 254 255 256 |
# File 'lib/ezid/identifier.rb', line 252 def reset warn "[DEPRECATION] `reset` is deprecated and will be removed in 2.0. Use `reset_metadata` instead. (called from #{caller.first})" self end |
#reset_metadata ⇒ Object
322 323 324 325 |
# File 'lib/ezid/identifier.rb', line 322 def .clear .clear end |
#save ⇒ Ezid::Identifier
Persist the identifer and/or metadata to EZID.
If the identifier is already persisted, this is an update operation;
Otherwise, create (if it has an id) or mint (if it has a shoulder)
the identifier.
172 173 174 175 176 177 |
# File 'lib/ezid/identifier.rb', line 172 def save raise Error, "Cannot save a deleted identifier." if deleted? persist self end |
#to_s ⇒ Object
143 144 145 |
# File 'lib/ezid/identifier.rb', line 143 def to_s id end |
#unavailable!(reason = nil) ⇒ String
Mark the identifier as unavailable
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/ezid/identifier.rb', line 298 def unavailable!(reason = nil) if persisted? && reserved? raise Error, "Cannot make a reserved identifier unavailable." end if unavailable? and reason.nil? return end value = Status::UNAVAILABLE if reason value += " | #{reason}" end self.status = value end |
#unavailable? ⇒ Boolean
Is the identifier unavailable?
285 286 287 |
# File 'lib/ezid/identifier.rb', line 285 def unavailable? status.to_s.start_with? Status::UNAVAILABLE end |
#update(data = {}) ⇒ Ezid::Identifier
Updates the metadata and saves the identifier
216 217 218 219 |
# File 'lib/ezid/identifier.rb', line 216 def update(data={}) (data) save end |
#update_metadata(attrs = {}) ⇒ Ezid::Identifier
Updates the metadata
195 196 197 198 |
# File 'lib/ezid/identifier.rb', line 195 def (attrs={}) .update(attrs) self end |