Class: Ezid::Identifier
- Inherits:
-
Object
- Object
- Ezid::Identifier
- Defined in:
- lib/ezid/identifier.rb
Overview
Represents an EZID identifier as a resource.
Ezid::Identifier delegates access to registered metadata elements through #method_missing.
Constant Summary collapse
- INSPECT_ATTRS =
Attributes to display on inspect
%w( id status target created )
- PUBLIC =
EZID status terms
"public"
- RESERVED =
"reserved"
- UNAVAILABLE =
"unavailable"
Class Attribute Summary collapse
Instance Attribute Summary collapse
- #client ⇒ Object readonly
- #id ⇒ Object readonly
- #metadata ⇒ Object
- #shoulder ⇒ Object
Class Method Summary collapse
-
.create(attrs = {}) ⇒ Ezid::Identifier
Creates or mints an identifier (depending on arguments).
-
.find(id) ⇒ Ezid::Identifier
Retrieves an identifier.
Instance Method Summary collapse
-
#deletable? ⇒ Boolean
Is the identifier deletable?.
-
#delete ⇒ Ezid::Identifier
Deletes the identifier from EZID.
-
#deleted? ⇒ Boolean
Has the identifier been deleted?.
-
#initialize(args = {}) ⇒ Identifier
constructor
A new instance of Identifier.
- #inspect ⇒ Object
-
#persisted? ⇒ Boolean
Is the identifier persisted?.
-
#public! ⇒ String
Mark the identifier as public.
-
#public? ⇒ Boolean
Is the identifier public?.
-
#reload ⇒ Ezid::Identifier
Reloads the metadata from EZID (local changes will be lost!).
-
#reserved? ⇒ Boolean
Is the identifier reserved?.
-
#reset ⇒ Ezid::Identifier
Empties the (local) metadata (changes will be lost!).
-
#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 = {}) ⇒ Identifier
Returns a new instance of Identifier.
45 46 47 48 49 50 51 |
# File 'lib/ezid/identifier.rb', line 45 def initialize(args={}) @client = args.delete(:client) || Client.new @id = args.delete(:id) @shoulder = args.delete(:shoulder) @deleted = false (args) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
177 178 179 180 181 |
# File 'lib/ezid/identifier.rb', line 177 def method_missing(method, *args) .send(method, *args) rescue NoMethodError super end |
Class Attribute Details
.defaults ⇒ Object
23 24 25 |
# File 'lib/ezid/identifier.rb', line 23 def defaults @defaults end |
Instance Attribute Details
#client ⇒ Object (readonly)
11 12 13 |
# File 'lib/ezid/identifier.rb', line 11 def client @client end |
#id ⇒ Object (readonly)
11 12 13 |
# File 'lib/ezid/identifier.rb', line 11 def id @id end |
#metadata ⇒ Object
12 13 14 |
# File 'lib/ezid/identifier.rb', line 12 def @metadata end |
#shoulder ⇒ Object
12 13 14 |
# File 'lib/ezid/identifier.rb', line 12 def shoulder @shoulder end |
Class Method Details
.create(attrs = {}) ⇒ Ezid::Identifier
Creates or mints an identifier (depending on arguments)
29 30 31 32 |
# File 'lib/ezid/identifier.rb', line 29 def create(attrs = {}) identifier = new(attrs) identifier.save end |
.find(id) ⇒ Ezid::Identifier
Retrieves an identifier
37 38 39 40 |
# File 'lib/ezid/identifier.rb', line 37 def find(id) identifier = new(id: id) identifier.reload end |
Instance Method Details
#deletable? ⇒ Boolean
Is the identifier deletable?
155 156 157 |
# File 'lib/ezid/identifier.rb', line 155 def deletable? persisted? && reserved? end |
#delete ⇒ Ezid::Identifier
Deletes the identifier from EZID
128 129 130 131 132 133 |
# File 'lib/ezid/identifier.rb', line 128 def delete raise Error, "Only persisted, reserved identifiers may be deleted: #{inspect}." unless deletable? client.delete_identifier(id) @deleted = true reset end |
#deleted? ⇒ Boolean
Has the identifier been deleted?
96 97 98 |
# File 'lib/ezid/identifier.rb', line 96 def deleted? @deleted end |
#inspect ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/ezid/identifier.rb', line 53 def inspect attrs = if deleted? "id=\"#{id}\" DELETED" else INSPECT_ATTRS.map { |attr| "#{attr}=\"#{send(attr)}\"" }.join(" ") end "#<#{self.class.name} #{attrs}>" end |
#persisted? ⇒ Boolean
Is the identifier persisted?
89 90 91 92 |
# File 'lib/ezid/identifier.rb', line 89 def persisted? return false if deleted? !!(id && created) end |
#public! ⇒ String
Mark the identifier as public
171 172 173 |
# File 'lib/ezid/identifier.rb', line 171 def public! self.status = PUBLIC end |
#public? ⇒ Boolean
Is the identifier public?
143 144 145 |
# File 'lib/ezid/identifier.rb', line 143 def public? status == PUBLIC end |
#reload ⇒ Ezid::Identifier
Reloads the metadata from EZID (local changes will be lost!)
112 113 114 115 |
# File 'lib/ezid/identifier.rb', line 112 def reload self end |
#reserved? ⇒ Boolean
Is the identifier reserved?
137 138 139 |
# File 'lib/ezid/identifier.rb', line 137 def reserved? status == RESERVED end |
#reset ⇒ Ezid::Identifier
Empties the (local) metadata (changes will be lost!)
119 120 121 122 |
# File 'lib/ezid/identifier.rb', line 119 def reset self 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.
73 74 75 76 77 |
# File 'lib/ezid/identifier.rb', line 73 def save raise Error, "Cannot save a deleted identifier." if deleted? persisted? ? modify : create_or_mint reload end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/ezid/identifier.rb', line 62 def to_s id end |
#unavailable!(reason = nil) ⇒ String
Mark the identifier as unavailable
162 163 164 165 166 167 |
# File 'lib/ezid/identifier.rb', line 162 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 |
#unavailable? ⇒ Boolean
Is the identifier unavailable?
149 150 151 |
# File 'lib/ezid/identifier.rb', line 149 def unavailable? status =~ /^#{UNAVAILABLE}/ end |
#update(data = {}) ⇒ Ezid::Identifier
Updates the metadata and saves the identifier
104 105 106 107 |
# File 'lib/ezid/identifier.rb', line 104 def update(data={}) (data) save end |
#update_metadata(attrs = {}) ⇒ Ezid::Identifier
Updates the metadata
82 83 84 85 |
# File 'lib/ezid/identifier.rb', line 82 def (attrs={}) attrs.each { |k, v| send("#{k}=", v) } self end |