Module: Origami::StandardObject

Overview

Mixin’ module for objects which can store their options into an inner Dictionary.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_ATTRIBUTES =

:nodoc:

{ :Type => Object, :Version => "1.1" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object

:nodoc:



111
112
113
114
# File 'lib/origami/object.rb', line 111

def self.included(receiver) #:nodoc:
  receiver.instance_variable_set(:@fields, Hash.new(DEFAULT_ATTRIBUTES))
  receiver.extend(ClassMethods)
end

Instance Method Details

#do_type_checkObject

:nodoc:



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/origami/object.rb', line 212

def do_type_check #:nodoc:
  self.class.fields.each_pair do |field, attributes|
    
    if not self[field].nil? and not attributes[:Type].nil?
      types = attributes[:Type].is_a?(::Array) ? attributes[:Type] : [ attributes[:Type] ]
      if not self[field].is_a?(Reference) and types.all? {|type| not self[field].is_a?(type)}
        puts "Warning: in object #{self.class}, field `#{field.to_s}' has unexpected type #{self[field].class}" 
      end
    end
  end
end

#has_field?(field) ⇒ Boolean

Check if an attribute is set in the current Object.

attr

The attribute name.

Returns:



173
174
175
# File 'lib/origami/object.rb', line 173

def has_field? (field)
  not self[field].nil?
end

#pdf_version_requiredObject

Returns the version and level required by the current Object.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/origami/object.rb', line 180

def pdf_version_required #:nodoc:
  max = [ 1.0, 0 ]
  
  self.each_key do |field|
    attributes = self.class.fields[field.value]

    current_version = attributes.has_key?(:Version) ? attributes[:Version].to_f : 0
    current_level = attributes[:ExtensionLevel] || 0
    current = [ current_version, current_level ]

    max = current if (current <=> max) > 0

    sub = self[field.value].pdf_version_required
    max = sub if (sub <=> max) > 0
  end
  
  max
end

#pre_buildObject

:nodoc:



161
162
163
164
165
166
167
# File 'lib/origami/object.rb', line 161

def pre_build #:nodoc:
  
  set_default_values
  do_type_check if Origami::OPTIONS[:enable_type_checking] == true
  
  super
end

#set_default_value(field) ⇒ Object

:nodoc:



199
200
201
202
203
204
# File 'lib/origami/object.rb', line 199

def set_default_value(field) #:nodoc:
  if self.class.fields[field][:Default]
    self[field] = self.class.fields[field][:Default]
    self[field].pre_build
  end
end

#set_default_valuesObject

:nodoc:



206
207
208
209
210
# File 'lib/origami/object.rb', line 206

def set_default_values #:nodoc:
  self.class.required_fields.each do |field| 
    set_default_value(field) unless has_field?(field) 
  end
end