Class: HexaPDF::DictionaryFields::Field
- Inherits:
-
Object
- Object
- HexaPDF::DictionaryFields::Field
- Defined in:
- lib/hexapdf/dictionary_fields.rb
Overview
A field contains information about one field of a structured PDF object and this information comes directly from the PDF specification.
By incorporating this field information into HexaPDF it is possible to do many things automatically, like checking for the correct minimum PDF version to use or converting a date from its string representation to a Time object.
Instance Attribute Summary collapse
-
#allowed_values ⇒ Object
readonly
Returns an array with the allowed values for this field, or
nil
if the values are not constrained. -
#indirect ⇒ Object
readonly
Returns
true
if the value for this field needs to be an indirect object,false
if it needs to be a direct object ornil
if it can be either. -
#version ⇒ Object
readonly
Returns the PDF version that is required for this field.
Class Method Summary collapse
-
.converter_for(type) ⇒ Object
Returns the converter for the given
type
specification. -
.converters ⇒ Object
Returns the list of available converter objects.
Instance Method Summary collapse
-
#convert(data, document) ⇒ Object
Converts the data into a useful object if possible.
-
#default ⇒ Object
Returns a duplicated default value.
-
#default? ⇒ Boolean
Returns
true
if a default value is available. -
#initialize(type, required: false, default: nil, indirect: nil, allowed_values: nil, version: nil) ⇒ Field
constructor
Create a new Field object.
-
#required? ⇒ Boolean
Returns
true
if this field is required. -
#type ⇒ Object
Returns the array with valid types for this field.
-
#valid_object?(obj) ⇒ Boolean
Returns
true
if the given object is valid for this field.
Constructor Details
#initialize(type, required: false, default: nil, indirect: nil, allowed_values: nil, version: nil) ⇒ Field
Create a new Field object. See Dictionary::define_field for information on the arguments.
Depending on the type
entry an appropriate field converter object is chosen from the available converters.
130 131 132 133 134 135 136 137 |
# File 'lib/hexapdf/dictionary_fields.rb', line 130 def initialize(type, required: false, default: nil, indirect: nil, allowed_values: nil, version: nil) @type = [type].flatten @type_mapped = false @required, @default, @indirect, @version = required, default, indirect, version @allowed_values = allowed_values && [allowed_values].flatten @converters = @type.map {|t| self.class.converter_for(t) }.compact end |
Instance Attribute Details
#allowed_values ⇒ Object (readonly)
Returns an array with the allowed values for this field, or nil
if the values are not constrained.
121 122 123 |
# File 'lib/hexapdf/dictionary_fields.rb', line 121 def allowed_values @allowed_values end |
#indirect ⇒ Object (readonly)
Returns true
if the value for this field needs to be an indirect object, false
if it needs to be a direct object or nil
if it can be either.
117 118 119 |
# File 'lib/hexapdf/dictionary_fields.rb', line 117 def indirect @indirect end |
#version ⇒ Object (readonly)
Returns the PDF version that is required for this field.
124 125 126 |
# File 'lib/hexapdf/dictionary_fields.rb', line 124 def version @version end |
Class Method Details
.converter_for(type) ⇒ Object
Returns the converter for the given type
specification.
The converter list from #converters is checked for a suitable converter from the front to the back. So if two converters could potentially be used for the same type, the one that appears earlier is used.
111 112 113 |
# File 'lib/hexapdf/dictionary_fields.rb', line 111 def self.converter_for(type) @converters.find {|converter| converter.usable_for?(type) } end |
.converters ⇒ Object
Returns the list of available converter objects.
See ::converter_for for information on how this list is used.
102 103 104 |
# File 'lib/hexapdf/dictionary_fields.rb', line 102 def self.converters @converters ||= [] end |
Instance Method Details
#convert(data, document) ⇒ Object
Converts the data into a useful object if possible. Otherwise returns nil
.
177 178 179 180 181 182 183 |
# File 'lib/hexapdf/dictionary_fields.rb', line 177 def convert(data, document) @converters.each do |converter| result = converter.convert(data, type, document) return result unless result.nil? end nil end |
#default ⇒ Object
Returns a duplicated default value.
166 167 168 |
# File 'lib/hexapdf/dictionary_fields.rb', line 166 def default @default.dup end |
#default? ⇒ Boolean
Returns true
if a default value is available.
161 162 163 |
# File 'lib/hexapdf/dictionary_fields.rb', line 161 def default? !@default.nil? end |
#required? ⇒ Boolean
Returns true
if this field is required.
156 157 158 |
# File 'lib/hexapdf/dictionary_fields.rb', line 156 def required? @required end |
#type ⇒ Object
Returns the array with valid types for this field.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/hexapdf/dictionary_fields.rb', line 140 def type return @type if @type_mapped @type.concat(@converters.flat_map(&:additional_types).compact) @type.map! do |type| if type.kind_of?(Symbol) HexaPDF::GlobalConfiguration.constantize('object.type_map', type) else type end end @type.uniq! @type_mapped = true @type end |