Class: Ezid::Identifier
- Inherits:
-
Object
- Object
- Ezid::Identifier
- Defined in:
- lib/ezid/identifier.rb
Overview
Represents an EZID identifier as a resource.
Constant Summary collapse
- INSPECT_ATTRS =
Attributes to display on inspect
%w( id status target created ).freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
- #client ⇒ Object readonly
- #id ⇒ Object readonly
-
#metadata(load = true) ⇒ Ezid::Metadata
Returns the identifier metadata.
- #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
-
#load_metadata ⇒ Ezid::Identifier
Loads the metadata from EZID (local changes will be lost!).
-
#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.
-
#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.
40 41 42 43 44 45 46 47 |
# File 'lib/ezid/identifier.rb', line 40 def initialize(args={}) @client = args.delete(:client) || Client.new @id = args.delete(:id) @shoulder = args.delete(:shoulder) @state = :new self. = Metadata.new args.delete(:metadata) self.class.defaults.merge(args) # deprecate? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
195 196 197 198 199 |
# File 'lib/ezid/identifier.rb', line 195 def method_missing(method, *args) .send(method, *args) rescue NoMethodError super end |
Class Attribute Details
.defaults ⇒ Object
18 19 20 |
# File 'lib/ezid/identifier.rb', line 18 def defaults @defaults end |
Instance Attribute Details
#client ⇒ Object (readonly)
9 10 11 |
# File 'lib/ezid/identifier.rb', line 9 def client @client end |
#id ⇒ Object
10 11 12 |
# File 'lib/ezid/identifier.rb', line 10 def id @id end |
#metadata(load = true) ⇒ Ezid::Metadata
Returns the identifier metadata
65 66 67 |
# File 'lib/ezid/identifier.rb', line 65 def @metadata end |
#shoulder ⇒ Object
10 11 12 |
# File 'lib/ezid/identifier.rb', line 10 def shoulder @shoulder end |
Class Method Details
.create(attrs = {}) ⇒ Ezid::Identifier
Creates or mints an identifier (depending on arguments)
24 25 26 27 |
# File 'lib/ezid/identifier.rb', line 24 def create(attrs = {}) identifier = new(attrs) identifier.save end |
.find(id) ⇒ Ezid::Identifier
Retrieves an identifier
32 33 34 35 |
# File 'lib/ezid/identifier.rb', line 32 def find(id) identifier = new(id: id) identifier. end |
Instance Method Details
#deletable? ⇒ Boolean
Is the identifier deletable?
166 167 168 |
# File 'lib/ezid/identifier.rb', line 166 def deletable? persisted? && reserved? end |
#delete ⇒ Ezid::Identifier
Deletes the identifier from EZID
139 140 141 142 143 144 |
# File 'lib/ezid/identifier.rb', line 139 def delete raise Error, "Only persisted, reserved identifiers may be deleted: #{inspect}." unless deletable? client.delete_identifier(id) self.state = :deleted reset end |
#deleted? ⇒ Boolean
Has the identifier been deleted?
99 100 101 |
# File 'lib/ezid/identifier.rb', line 99 def deleted? state == :deleted end |
#inspect ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/ezid/identifier.rb', line 49 def inspect attrs = if deleted? "id=\"#{id}\" DELETED" else INSPECT_ATTRS.map { |attr| "#{attr}=#{send(attr).inspect}" }.join(", ") end "#<#{self.class.name} #{attrs}>" end |
#load_metadata ⇒ Ezid::Identifier
Loads the metadata from EZID (local changes will be lost!)
121 122 123 124 125 126 |
# File 'lib/ezid/identifier.rb', line 121 def response = client.(id) self. = Metadata.new(response.) self.state = :persisted self end |
#persisted? ⇒ Boolean
Is the identifier persisted?
93 94 95 |
# File 'lib/ezid/identifier.rb', line 93 def persisted? state == :persisted end |
#public! ⇒ String
Mark the identifier as public
189 190 191 |
# File 'lib/ezid/identifier.rb', line 189 def public! self.status = Status::PUBLIC end |
#public? ⇒ Boolean
Is the identifier public?
154 155 156 |
# File 'lib/ezid/identifier.rb', line 154 def public? status == Status::PUBLIC end |
#reload ⇒ Object
Use #load_metadata instead.
113 114 115 116 |
# File 'lib/ezid/identifier.rb', line 113 def reload warn "[DEPRECATION] `reload` is deprecated and will be removed in version 2.0. Use `load_metadata` instead." end |
#reserved? ⇒ Boolean
Is the identifier reserved?
148 149 150 |
# File 'lib/ezid/identifier.rb', line 148 def reserved? status == Status::RESERVED end |
#reset ⇒ Ezid::Identifier
Empties the (local) metadata (changes will be lost!)
130 131 132 133 |
# File 'lib/ezid/identifier.rb', line 130 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.
77 78 79 80 81 |
# File 'lib/ezid/identifier.rb', line 77 def save raise Error, "Cannot save a deleted identifier." if deleted? persist reset end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/ezid/identifier.rb', line 58 def to_s id end |
#unavailable!(reason = nil) ⇒ String
Mark the identifier as unavailable
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ezid/identifier.rb', line 173 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?
160 161 162 |
# File 'lib/ezid/identifier.rb', line 160 def unavailable? status.to_s.start_with? Status::UNAVAILABLE end |
#update(data = {}) ⇒ Ezid::Identifier
Updates the metadata and saves the identifier
107 108 109 110 |
# File 'lib/ezid/identifier.rb', line 107 def update(data={}) (data) save end |
#update_metadata(attrs = {}) ⇒ Ezid::Identifier
Updates the metadata
86 87 88 89 |
# File 'lib/ezid/identifier.rb', line 86 def (attrs={}) attrs.each { |k, v| send("#{k}=", v) } self end |