Module: Identifiers
Constant Summary collapse
- MAX_LENGTH =
50
Class Method Summary collapse
Instance Method Summary collapse
-
#after_initialize ⇒ Object
-
#before_validation ⇒ Object
-
#format_multipart_identifier ⇒ Object
take a string that looks like this: “["3422","345FD","3423ASDA",null]” and convert it into this: “3422-345FD-3423ASDA”.
-
#id_0=(v) ⇒ Object
-
#id_1=(v) ⇒ Object
-
#id_2=(v) ⇒ Object
-
#id_3=(v) ⇒ Object
-
#validate ⇒ Object
Class Method Details
.format(identifier) ⇒ Object
20 21 22 |
# File 'backend/app/model/mixins/identifiers.rb', line 20 def self.format(identifier) identifier.compact.join("--") end |
.included(base) ⇒ Object
14 15 16 17 18 |
# File 'backend/app/model/mixins/identifiers.rb', line 14 def self.included(base) base.repo_unique_constraint(:identifier, :message => "That ID is already in use", :json_property => :id_0) end |
.parse(identifier) ⇒ Object
25 26 27 |
# File 'backend/app/model/mixins/identifiers.rb', line 25 def self.parse(identifier) ASUtils.json_parse(identifier || "[]") end |
Instance Method Details
#after_initialize ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'backend/app/model/mixins/identifiers.rb', line 30 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 |
#before_validation ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'backend/app/model/mixins/identifiers.rb', line 48 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 |
#format_multipart_identifier ⇒ Object
take a string that looks like this: “["3422","345FD","3423ASDA",null]” and convert it into this: “3422-345FD-3423ASDA”
82 83 84 85 86 87 88 89 |
# File 'backend/app/model/mixins/identifiers.rb', line 82 def format_multipart_identifier self[:identifier].gsub("null", '') .gsub!(/[\[\]]/, '') .gsub(",", '') .split('"') .select {|s| !s.empty?} .join("-") end |
#id_0=(v) ⇒ Object
5 |
# File 'backend/app/model/mixins/identifiers.rb', line 5 def id_0=(v) @id_0 = v; self.modified! end |
#id_1=(v) ⇒ Object
7 |
# File 'backend/app/model/mixins/identifiers.rb', line 7 def id_1=(v) @id_1 = v; self.modified! end |
#id_2=(v) ⇒ Object
9 |
# File 'backend/app/model/mixins/identifiers.rb', line 9 def id_2=(v) @id_2 = v; self.modified! end |
#id_3=(v) ⇒ Object
11 |
# File 'backend/app/model/mixins/identifiers.rb', line 11 def id_3=(v) @id_3 = v; self.modified! end |
#validate ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'backend/app/model/mixins/identifiers.rb', line 69 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 |