lib/hexapdf/dictionary.rb in hexapdf-0.10.0 vs lib/hexapdf/dictionary.rb in hexapdf-0.11.0
- old
+ new
@@ -68,11 +68,11 @@
# Integer:: Integer
# Real:: Float
# String:: String (for text strings), PDFByteString (for binary strings)
# Date:: PDFDate
# Name:: Symbol
- # Array:: Array
+ # Array:: PDFArray or Array
# Dictionary:: Dictionary (or any subclass) or Hash
# Stream:: Stream (or any subclass)
# Null:: NilClass
#
# If an array of classes is provided, the value can be an instance of any of these
@@ -91,15 +91,18 @@
#
# indirect:: Specifies whether the value (or the values in the array value) of this field has
# to be an indirect object (+true+), a direct object (+false+) or if it doesn't
# matter (unspecified or +nil+).
#
+ # allowed_values:: An array of allowed values for this field.
+ #
# version:: Specifies the minimum version of the PDF specification needed for this value.
def self.define_field(name, type:, required: false, default: nil, indirect: nil,
- version: '1.2')
+ allowed_values: nil, version: '1.2')
@fields ||= {}
- @fields[name] = Field.new(type, required, default, indirect, version)
+ @fields[name] = Field.new(type, required: required, default: default, indirect: indirect,
+ allowed_values: allowed_values, version: version)
end
# Returns the field entry for the given field name.
#
# The ancestor classes are also searched for such a field entry if none is found for the
@@ -292,9 +295,14 @@
yield(msg, true)
self[name] = obj.intern
else
yield(msg, false)
end
+ end
+
+ # Check the value of the field against the allowed values.
+ if field.allowed_values && !field.allowed_values.include?(obj)
+ yield("Field #{name} does not contain an allowed value")
end
# Check if field value needs to be (in)direct
unless field.indirect.nil?
obj = value[name] # we need the unwrapped object!