Module: Identifiers

Included in:
Accession, Resource
Defined in:
backend/app/model/mixins/identifiers.rb

Constant Summary

MAX_LENGTH =
50

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) format(identifier)



17
18
19
# File 'backend/app/model/mixins/identifiers.rb', line 17

def self.format(identifier)
  identifier.compact.join("--")
end

+ (Object) included(base)



11
12
13
14
15
# File 'backend/app/model/mixins/identifiers.rb', line 11

def self.included(base)
  base.repo_unique_constraint(:identifier,
                              :message => "That ID is already in use",
                              :json_property => :id_0)
end

+ (Object) parse(identifier)



22
23
24
# File 'backend/app/model/mixins/identifiers.rb', line 22

def self.parse(identifier)
  ASUtils.json_parse(identifier || "[]")
end

Instance Method Details

- (Object) after_initialize



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'backend/app/model/mixins/identifiers.rb', line 27

def after_initialize
  # Split the identifier into its components and add the individual pieces as
  # variables on this instance.
  if self[:identifier]
    identifier = Identifiers.parse(self[:identifier])

    4.times do |i|
      self.instance_eval {
        @values[:"id_#{i}"] = identifier[i] if identifier[i] and !identifier[i].empty?
      }

      instance_variable_set("@id_#{i}", identifier[i])
    end
  end

  super
end

- (Object) before_validation



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'backend/app/model/mixins/identifiers.rb', line 45

def before_validation
  # Combine the identifier into a single string and remove the instance variables we added previously.
  values = (0...4).map {|i| instance_variable_get("@id_#{i}")}

  if (values.reject{|v| v.nil? || v.empty?}.empty?)
    # None of the id_* fields were set, so the whole identifier is NULL.
    self.identifier = nil
  else
    self.identifier = JSON(values)
  end

  4.times do |i|
    self.instance_eval {
      @values.delete(:"id_#{i}")
    }
  end

  super
end

- (Object) id_0=(v)



5
# File 'backend/app/model/mixins/identifiers.rb', line 5

def id_0=(v) @id_0 = v; self.modified! end

- (Object) id_1=(v)



6
# File 'backend/app/model/mixins/identifiers.rb', line 6

def id_1=(v) @id_1 = v; self.modified! end

- (Object) id_2=(v)



7
# File 'backend/app/model/mixins/identifiers.rb', line 7

def id_2=(v) @id_2 = v; self.modified! end

- (Object) id_3=(v)



8
# File 'backend/app/model/mixins/identifiers.rb', line 8

def id_3=(v) @id_3 = v; self.modified! end

- (Object) validate



66
67
68
69
70
71
72
73
74
75
# File 'backend/app/model/mixins/identifiers.rb', line 66

def validate
  return super if self.class.table_name === :resource and self.identifier.nil?

  (0...4).each do |i|
    val = instance_variable_get("@id_#{i}")
    errors.add("id_#{i}".intern, "Max length is #{MAX_LENGTH} characters") if val && val.length > MAX_LENGTH
  end

  super
end