Class: Ezid::Identifier

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

Class Method Summary collapse

Instance Method Summary collapse

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

.defaultsObject



18
19
20
# File 'lib/ezid/identifier.rb', line 18

def defaults
  @defaults
end

Instance Attribute Details

#clientObject (readonly)



9
10
11
# File 'lib/ezid/identifier.rb', line 9

def client
  @client
end

#idObject



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

def id
  @id
end

#metadata(load = true) ⇒ Ezid::Metadata

Returns the identifier metadata

Parameters:

  • load (Boolean) (defaults to: true)
    • flag to load the metadata from EZID if stale (default: ‘true`)

Returns:



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

def 
  @metadata
end

#shoulderObject



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)

Returns:

Raises:

See Also:



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

Returns:

Raises:

  • (Ezid::Error)

    if the identifier does not exist in EZID



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?

Returns:

  • (Boolean)


166
167
168
# File 'lib/ezid/identifier.rb', line 166

def deletable?
  persisted? && reserved?
end

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

Returns:

  • (Boolean)


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

def deleted?
  state == :deleted
end

#inspectObject



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_metadataEzid::Identifier

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

Returns:

Raises:



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?

Returns:

  • (Boolean)


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

def persisted?
  state == :persisted
end

#public!String

Mark the identifier as public

Returns:

  • (String)

    the new status



189
190
191
# File 'lib/ezid/identifier.rb', line 189

def public!
  self.status = Status::PUBLIC
end

#public?Boolean

Is the identifier public?

Returns:

  • (Boolean)


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

def public?
  status == Status::PUBLIC
end

#reloadObject

Deprecated.

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?

Returns:

  • (Boolean)


148
149
150
# File 'lib/ezid/identifier.rb', line 148

def reserved?
  status == Status::RESERVED
end

#resetEzid::Identifier

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

Returns:



130
131
132
133
# File 'lib/ezid/identifier.rb', line 130

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.



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_sObject



58
59
60
# File 'lib/ezid/identifier.rb', line 58

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



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?

Returns:

  • (Boolean)


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

Parameters:

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

    a hash of metadata

Returns:

Raises:



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

Parameters:

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

    the metadata

Returns:



86
87
88
89
# File 'lib/ezid/identifier.rb', line 86

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