Class: Ezid::Identifier

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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 )
PUBLIC =

EZID status terms

"public"
RESERVED =
"reserved"
UNAVAILABLE =
"unavailable"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Identifier

Returns a new instance of Identifier.



49
50
51
52
53
54
55
# File 'lib/ezid/identifier.rb', line 49

def initialize(args={})
  @client = args.delete(:client) || Client.new
  @id = args.delete(:id)
  @shoulder = args.delete(:shoulder)
  @deleted = false
  (args)
end

Class Attribute Details

.defaultsObject



27
28
29
# File 'lib/ezid/identifier.rb', line 27

def defaults
  @defaults
end

Instance Attribute Details

#clientObject (readonly)



12
13
14
# File 'lib/ezid/identifier.rb', line 12

def client
  @client
end

#idObject (readonly)



12
13
14
# File 'lib/ezid/identifier.rb', line 12

def id
  @id
end

#metadataObject



13
14
15
# File 'lib/ezid/identifier.rb', line 13

def 
  
end

#shoulderObject



13
14
15
# File 'lib/ezid/identifier.rb', line 13

def shoulder
  @shoulder
end

Class Method Details

.create(attrs = {}) ⇒ Ezid::Identifier

Creates or mints an identifier (depending on arguments)

Raises:

See Also:



33
34
35
36
# File 'lib/ezid/identifier.rb', line 33

def create(attrs = {})
  identifier = new(attrs)
  identifier.save
end

.find(id) ⇒ Ezid::Identifier

Retrieves an identifier

Raises:

  • (Ezid::Error)

    if the identifier does not exist in EZID



41
42
43
44
# File 'lib/ezid/identifier.rb', line 41

def find(id)
  identifier = new(id: id)
  identifier.reload
end

Instance Method Details

#deleteEzid::Identifier

Deletes the identifier from EZID

Raises:



131
132
133
134
135
136
# File 'lib/ezid/identifier.rb', line 131

def delete
  raise Error, "Status must be \"reserved\" to delete (status: \"#{status}\")." unless reserved?
  client.delete_identifier(id)
  @deleted = true
  reset
end

#deleted?Boolean

Has the identifier been deleted?



100
101
102
# File 'lib/ezid/identifier.rb', line 100

def deleted?
  @deleted
end

#inspectObject



57
58
59
60
61
62
63
64
# File 'lib/ezid/identifier.rb', line 57

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?



93
94
95
96
# File 'lib/ezid/identifier.rb', line 93

def persisted?
  return false if deleted?
  !!(id && created)
end

#public?Boolean

Is the identifier public?



146
147
148
# File 'lib/ezid/identifier.rb', line 146

def public?
  status == PUBLIC
end

#reloadEzid::Identifier

Reloads the metadata from EZID (local changes will be lost!)

Raises:



116
117
118
119
# File 'lib/ezid/identifier.rb', line 116

def reload
  
  self
end

#reserved?Boolean

Is the identifier reserved?



140
141
142
# File 'lib/ezid/identifier.rb', line 140

def reserved?
  status == RESERVED
end

#resetEzid::Identifier

Empties the (local) metadata (changes will be lost!)



123
124
125
126
# File 'lib/ezid/identifier.rb', line 123

def reset
  
  self
end

#saveEzid::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.

Raises:

  • (Ezid::Error)

    if the identifier is deleted, or the host responds with an error status.



77
78
79
80
81
# File 'lib/ezid/identifier.rb', line 77

def save
  raise Error, "Cannot save a deleted identifier." if deleted?
  persisted? ? modify : create_or_mint
  reload
end

#to_sObject



66
67
68
# File 'lib/ezid/identifier.rb', line 66

def to_s
  id
end

#unavailable?Boolean

Is the identifier unavailable?



152
153
154
# File 'lib/ezid/identifier.rb', line 152

def unavailable?
  status == UNAVAILABLE
end

#update(data = {}) ⇒ Ezid::Identifier

Updates the metadata and saves the identifier

Raises:



108
109
110
111
# File 'lib/ezid/identifier.rb', line 108

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