class Origami::Dictionary

Class representing a Dictionary Object. Dictionaries are containers associating a Name to an embedded Object.

Attributes

names_cache[R]
strings_cache[R]
xref_cache[R]

Public Class Methods

new(hash = {}, parser = nil) click to toggle source

Creates a new Dictionary.

hash

The hash representing the new Dictionary.

Calls superclass method Origami::Object::new
# File lib/origami/dictionary.rb, line 48
def initialize(hash = {}, parser = nil)
    raise TypeError, "Expected type Hash, received #{hash.class}." unless hash.is_a?(Hash)
    super()

    @strings_cache = []
    @names_cache = []
    @xref_cache = {}

    hash.each_pair do |k,v|
        next if k.nil?

        # Turns the values into Objects.
        key, value = k.to_o, v.to_o

        if Origami::OPTIONS[:enable_type_guessing]
            hint_type = guess_value_type(key, value)

            if hint_type.is_a?(Class) and hint_type < value.class
                value = value.cast_to(hint_type, parser)
            end

            if hint_type and parser and Origami::OPTIONS[:enable_type_propagation]
                if value.is_a?(Reference)
                    parser.defer_type_cast(value, hint_type)
                end
            end
        end

        # Cache keys and values for fast search.
        cache_key(key)
        cache_value(value)

        self[key] = value
    end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 164
def [](key)
    super(key.to_o)
end
[]=(key,val) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 146
def []=(key,val)
    unless key.is_a?(Symbol) or key.is_a?(Name)
        fail "Expecting a Name for a Dictionary entry, found #{key.class} instead."
    end

    key = key.to_o
    if val.nil?
        delete(key)
        return
    end

    val = val.to_o
    super(key,val)

    key.parent = self
    val.parent = self unless val.indirect? or val.parent.equal?(self)
end
cast_to(type, parser = nil) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 178
def cast_to(type, parser = nil)
    super(type)

    cast = type.new(self.copy, parser)
    cast.parent = self.parent
    cast.no, cast.generation = self.no, self.generation
    if self.indirect?
        cast.set_indirect(true)
        cast.set_document(self.document)
        cast.file_offset = self.file_offset # cast can replace self
    end

    cast
end
copy() click to toggle source
# File lib/origami/dictionary.rb, line 200
def copy
    copy = self.class.new
    self.each_pair do |k,v|
        copy[k] = v.copy
    end

    copy.parent = @parent
    copy.no, copy.generation = @no, @generation
    copy.set_indirect(true) if self.indirect?
    copy.set_document(@document) if self.indirect?

    copy
end
delete(key) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 174
def delete(key)
    super(key.to_o)
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 168
def key?(key)
    super(key.to_o)
end
Also aliased as: include?, has_key?
map!(&b) click to toggle source
# File lib/origami/dictionary.rb, line 136
def map!(&b)
    self.each_pair do |k,v|
        self[k] = b.call(v)
    end
end
merge(dict) click to toggle source
Calls superclass method
# File lib/origami/dictionary.rb, line 142
def merge(dict)
    Dictionary.new(super(dict))
end
to_h() click to toggle source
# File lib/origami/dictionary.rb, line 195
def to_h
    Hash[self.to_a.map!{|k, v| [ k.value, v.value ]}]
end
Also aliased as: value
to_obfuscated_str() click to toggle source
Calls superclass method Origami::Object#to_obfuscated_str
# File lib/origami/obfuscation.rb, line 136
def to_obfuscated_str
    content = TOKENS.first + Obfuscator.junk_spaces
    self.each_pair do |key, value|
        content << Obfuscator.junk_spaces +
          key.to_obfuscated_str + Obfuscator.junk_spaces +
          value.to_obfuscated_str + Obfuscator.junk_spaces
    end

    content << TOKENS.last
    super(content)
end
value()
Alias for: to_h

Private Instance Methods

cache_key(key) click to toggle source
# File lib/origami/dictionary.rb, line 244
def cache_key(key)
    @names_cache.push(key)
end
cache_value(value) click to toggle source
# File lib/origami/dictionary.rb, line 248
def cache_value(value)
    case value
    when String then @strings_cache.push(value)
    when Name then @names_cache.push(value)
    when Reference then
        (@xref_cache[value] ||= []).push(self)
    when Dictionary, Array
        @strings_cache.concat(value.strings_cache)
        @names_cache.concat(value.names_cache)
        @xref_cache.update(value.xref_cache) do |_ref, cache1, cache2|
            cache1.concat(cache2)
        end

        value.strings_cache.clear
        value.names_cache.clear
        value.xref_cache.clear
    end
end
guess_value_type(key, value) click to toggle source
# File lib/origami/dictionary.rb, line 267
def guess_value_type(key, value)
    hint_type = self.class.hint_type(key.value)
    if hint_type.is_a?(::Array) and not value.is_a?(Reference) # Choose best match
        hint_type = hint_type.find {|type| type < value.class }
    end

    hint_type
end