Module: Mead::Validations::ClassMethods

Defined in:
lib/mead/validations.rb

Instance Method Summary collapse

Instance Method Details

#add_error(instance, attribute, message) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/mead/validations.rb', line 45

def add_error(instance, attribute, message)
  if instance.errors[attribute]
    instance.errors[attribute] << message
  else
    instance.errors[attribute] = [message]
  end
end

#format_regexpObject

FIXME: This should be a more generic regular expression. Overriding this method is the way to use a different regular expression during format validation.



56
57
58
59
60
61
# File 'lib/mead/validations.rb', line 56

def format_regexp
  container_codes = Mead::CONTAINER_MAPPING.keys.join('|')
  #/^(ua\d{3}|mc\d{5})(_\d{3})?-\d{3}-(#{container_codes})\d{4}-\d{3}([A-Z])?-\d{3}(_\d{4})?$/ # NCSU specific
  # eadid        series box/container            folder       sequence   page
  /^([a-z0-9_]*)-\d{3}-(#{container_codes})\d{4}-\d{3}([A-Z])?-\d{3}(_\d{4})?$/
end

#format_validationsObject



41
42
43
# File 'lib/mead/validations.rb', line 41

def format_validations
  @format_validations ||= []
end

#validates(&proc) ⇒ Object



113
114
115
116
117
# File 'lib/mead/validations.rb', line 113

def validates(&proc)
  validations << Proc.new{|instance|
    proc.call(instance)
  }
end

#validates_attributes(*attributes, &proc) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/mead/validations.rb', line 105

def validates_attributes(*attributes, &proc)
  validations << Proc.new { |instance|
    attributes.each {|attribute|
      proc.call(instance, attribute, instance.__send__(attribute))
    }
  }
end

#validates_format(&proc) ⇒ Object



119
120
121
122
123
# File 'lib/mead/validations.rb', line 119

def validates_format(&proc)
  format_validations << Proc.new{|instance|
    proc.call(instance)
  }
end

#validates_format_of_meadObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/mead/validations.rb', line 63

def validates_format_of_mead
  validates_format do |instance|
    add_error(instance, :mead, "Can't be blank.") if instance.mead.nil? or instance.mead.empty?
    # check the format of the whole thing
    # ua023_031-001-cb0003-005-001
    unless instance.mead.to_s =~ format_regexp
      add_error(instance, :mead, 'Does not match regular expression.')
    end
  end
end

#validates_numericality_of_mead(*attributes) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/mead/validations.rb', line 94

def validates_numericality_of_mead(*attributes)
  validates_attributes(*attributes) do |instance, attribute, value, options|
    if value
      unless value.to_s =~ /\A[+-]?\d+\Z/
        add_error(instance, attribute, "Not a number.")
        next
      end
    end
  end
end

#validates_presence_of_meadObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mead/validations.rb', line 74

def validates_presence_of_mead
  validates do |instance|
    begin
    if instance.
      result = instance.
    else
      result = Mead::Extractor.new(instance).extract
    end
    if result.nil? or result.empty?
      add_error(instance, :mead, 'No matching container.')
    # even if the instance.series is 1 there may not be any series in the EAD XML yet
  elsif result.last[:series_sequence] and instance.series != result.last[:series_sequence].to_s
      add_error(instance, :mead, 'Bad series.') 
    end
    rescue => e
      add_error(instance, :mead, e)
    end
  end
end

#validationsObject



37
38
39
# File 'lib/mead/validations.rb', line 37

def validations
  @validations ||= []
end