Class: Ezid::Identifier

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

.defaultsObject



23
24
25
# File 'lib/ezid/identifier.rb', line 23

def defaults
  @defaults
end

Instance Attribute Details

#clientObject (readonly)



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

def client
  @client
end

#idObject (readonly)



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

def id
  @id
end

#metadataObject



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

def 
  @metadata
end

#shoulderObject



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)

Returns:

Raises:

See Also:



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

Returns:

Raises:

  • (Ezid::Error)

    if the identifier does not exist in EZID



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?

Returns:

  • (Boolean)


155
156
157
# File 'lib/ezid/identifier.rb', line 155

def deletable?
  persisted? && reserved?
end

#deleteEzid::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?

Returns:

  • (Boolean)


96
97
98
# File 'lib/ezid/identifier.rb', line 96

def deleted?
  @deleted
end

#inspectObject



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?

Returns:

  • (Boolean)


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

Returns:

  • (String)

    the new status



171
172
173
# File 'lib/ezid/identifier.rb', line 171

def public!
  self.status = PUBLIC
end

#public?Boolean

Is the identifier public?

Returns:

  • (Boolean)


143
144
145
# File 'lib/ezid/identifier.rb', line 143

def public?
  status == PUBLIC
end

#reloadEzid::Identifier

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

Returns:

Raises:



112
113
114
115
# File 'lib/ezid/identifier.rb', line 112

def reload
  
  self
end

#reserved?Boolean

Is the identifier reserved?

Returns:

  • (Boolean)


137
138
139
# File 'lib/ezid/identifier.rb', line 137

def reserved?
  status == RESERVED
end

#resetEzid::Identifier

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

Returns:



119
120
121
122
# File 'lib/ezid/identifier.rb', line 119

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.

Returns:

Raises:

  • (Ezid::Error)

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



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_sObject



62
63
64
# File 'lib/ezid/identifier.rb', line 62

def to_s
  id
end

#unavailable!(reason = nil) ⇒ String

Mark the identifier as unavailable

Parameters:

  • reason (String) (defaults to: nil)

    an optional reason

Returns:

  • (String)

    the new status

Raises:



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?

Returns:

  • (Boolean)


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

Parameters:

  • data (Hash) (defaults to: {})

    a hash of metadata

Returns:

Raises:



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

Parameters:

  • attrs (Hash) (defaults to: {})

    the metadata

Returns:



82
83
84
85
# File 'lib/ezid/identifier.rb', line 82

def (attrs={})
  attrs.each { |k, v| send("#{k}=", v) }
  self
end