Class: HexaPDF::DictionaryFields::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/dictionary_fields.rb

Overview

A dictionary 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, required = false, default = nil, indirect = 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.



112
113
114
115
116
117
# File 'lib/hexapdf/dictionary_fields.rb', line 112

def initialize(type, required = false, default = nil, indirect = nil, version = nil)
  @type = [type].flatten
  @type_mapped = false
  @required, @default, @indirect, @version = required, default, indirect, version
  @converter = self.class.converter_for(type)
end

Instance Attribute Details

#indirectObject (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.



103
104
105
# File 'lib/hexapdf/dictionary_fields.rb', line 103

def indirect
  @indirect
end

#versionObject (readonly)

Returns the PDF version that is required for this field.



106
107
108
# File 'lib/hexapdf/dictionary_fields.rb', line 106

def version
  @version
end

Class Method Details

.converter_for(type) ⇒ Object

Returns the converter for the given type specification.

The converter list 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.



96
97
98
# File 'lib/hexapdf/dictionary_fields.rb', line 96

def self.converter_for(type)
  @converters.find {|converter| converter.usable_for?(type)}
end

.convertersObject

Returns the list of available converter objects.

See ::converter_for for information on how this list is used.



87
88
89
# File 'lib/hexapdf/dictionary_fields.rb', line 87

def self.converters
  @converters ||= []
end

Instance Method Details

#convert(data, document) ⇒ Object

If a converter was defined, it is used for converting the data. Otherwise this is a Noop - it just returns the data.

See: #convert?



176
177
178
# File 'lib/hexapdf/dictionary_fields.rb', line 176

def convert(data, document)
  @converter.convert(data, type, document)
end

#convert?(data) ⇒ Boolean

If a converter was defined, it is used. Otherwise false is returned.

See: #convert

Returns:



168
169
170
# File 'lib/hexapdf/dictionary_fields.rb', line 168

def convert?(data)
  @converter.convert?(data, type)
end

#defaultObject

Returns a duplicated default value, automatically taking unduplicatable classes into account.



147
148
149
# File 'lib/hexapdf/dictionary_fields.rb', line 147

def default
  duplicatable_default? ? @default.dup : @default
end

#default?Boolean

Returns true if a default value is available.

Returns:



141
142
143
# File 'lib/hexapdf/dictionary_fields.rb', line 141

def default?
  !@default.nil?
end

#required?Boolean

Returns true if this field is required.

Returns:



136
137
138
# File 'lib/hexapdf/dictionary_fields.rb', line 136

def required?
  @required
end

#typeObject

Returns the array with valid types for this field.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/hexapdf/dictionary_fields.rb', line 120

def type
  return @type if @type_mapped
  @type_mapped = true
  @type.concat(Array(@converter.additional_types))
  @type.map! do |type|
    if type.kind_of?(Symbol)
      HexaPDF::GlobalConfiguration.constantize('object.type_map'.freeze, type)
    else
      type
    end
  end
  @type.uniq!
  @type
end

#valid_object?(obj) ⇒ Boolean

Returns true if the given object is valid for this field.

Returns:



160
161
162
163
# File 'lib/hexapdf/dictionary_fields.rb', line 160

def valid_object?(obj)
  type.any? {|t| obj.kind_of?(t)} ||
    (obj.kind_of?(HexaPDF::Object) && type.any? {|t| obj.value.kind_of?(t)})
end