Module: ONIX::WorkIdentifiers

Defined in:
lib/onix/work_identifiers.rb

Constant Summary collapse

ID_TYPES =
{:proprietary_id => 1, :isbn10 => 2, :isbn13 => 15, :isbn => 15}

Instance Method Summary collapse

Instance Method Details

#find_work_identifier(options = {}) ⇒ Object

find the WorkIdentifier matching a particular type

Raises:

  • (NoMethodError)


18
19
20
21
22
23
24
25
26
# File 'lib/onix/work_identifiers.rb', line 18

def find_work_identifier(options = {})
  raise NoMethodError, "Must call initialize_work_identifiers first" unless defined?(@work_identifiers)
  
  # test find parameters
  attribute, value = set_type_options(options).first
  raise ArgumentError, "Find method passed unknown attribute '#{attribute}'" unless ONIX::WorkIdentifier.new.attributes.include?(attribute.to_s)
  
  @work_identifiers.find { |obj| obj.send(attribute) == value }
end

#initialize_work_identifiers(options = {}) ⇒ Object



7
8
9
10
11
# File 'lib/onix/work_identifiers.rb', line 7

def initialize_work_identifiers(options = {})
  self.work_identifiers = options[:work_identifiers]
  # To initialize accessor methods, class in which this module is included
  # must also include Inflector and call initialize_attributes
end

#set_work_identifier(type, value) ⇒ Object

set the value of a particular WorkIdentifier (found by type) type can be either type code or key from ID_TYPES



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/onix/work_identifiers.rb', line 30

def set_work_identifier(type, value)
  obj = find_work_identifier(type)

  # create a new work identifier object if necessary
  if obj.nil?
    obj = ONIX::WorkIdentifier.new(set_type_options(type))
    @work_identifiers << obj
  end

  obj.id_value = value
end

#work_identifiersObject



13
14
15
# File 'lib/onix/work_identifiers.rb', line 13

def work_identifiers
  @work_identifiers ||= []
end

#work_identifiers=(values) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/onix/work_identifiers.rb', line 42

def work_identifiers=(values)
  values = [values] if values.nil? || values.is_a?(ONIX::WorkIdentifier)
  if values.is_a?(Array)
    # Empty array
    @work_identifiers = []
    # Add any valid value
    values.each do |value|
      @work_identifiers << value if value.is_a?(ONIX::WorkIdentifier)
    end
  else
    raise ArgumentError, "Invalid WorkIdentifier value: #{values.inspect}"
  end
end