Parent

Included Modules

Mizuho::IdMap

Constants

BANNER
MATCHER
URANDOM

Attributes

associations[R]
entries[R]

Public Class Methods

new() click to toggle source
# File lib/mizuho/id_map.rb, line 51
def initialize
        @entries = {}
        @associations = {}
        #@namespace = slug(File.basename(filename, File.extname(filename)))
end

Public Instance Methods

add(title, id, *options) click to toggle source
# File lib/mizuho/id_map.rb, line 185
def add(title, id, *options)
        return @entries[title] = Entry.new(title, id || create_unique_id(title), *options)
end
generate_associations(titles) click to toggle source
# File lib/mizuho/id_map.rb, line 105
def generate_associations(titles)
        @associations = {}

        # Associate exact matches.
        titles = titles.reject do |title|
                if (entry = @entries[title]) && !entry.associated?
                        entry.associated = true
                        @associations[title] = entry.id
                        true
                else
                        false
                end
        end

        # For the remaining titles, associate with moved or similar-looking entry.
        titles.reject! do |title|
                if entry = find_moved(title)
                        @entries.delete(entry.title)
                        @entries[title] = entry
                        entry.title = title
                        entry.associated = true
                        entry.fuzzy = false
                        @associations[title] = entry.id
                        true
                else
                        false
                end
        end

        # For the remaining titles, associate with similar-looking entry.
        titles.reject! do |title|
                if entry = find_similar(title)
                        @entries.delete(entry.title)
                        @entries[title] = entry
                        entry.title = title
                        entry.associated = true
                        entry.fuzzy = true
                        @associations[title] = entry.id
                        true
                else
                        false
                end
        end

        # For the remaining titles, create new entries.
        titles.each do |title|
                id = create_unique_id(title)
                add(title, id, false, true)
                @associations[title] = id
        end
end
load(filename_or_io) click to toggle source
# File lib/mizuho/id_map.rb, line 57
def load(filename_or_io)
        @entries.clear
        open_io(filename_or_io, :read) do |io|
                fuzzy = false
                while true
                        begin
                                line = io.readline.strip
                                if line.empty?
                                        fuzzy = false
                                elsif line == "# fuzzy"
                                        fuzzy = true
                                elsif line !~ /\A#/
                                        title, id = line.split("\t=>\t", 2)
                                        add(title, id, fuzzy, false)
                                        fuzzy = false
                                end
                        rescue EOFError
                                break
                        end
                end
        end
        return self
end
save(filename_or_io) click to toggle source
# File lib/mizuho/id_map.rb, line 81
def save(filename_or_io)
        normal, orphaned = group_and_sort_entries
        output = ""
        output << BANNER
        normal.each do |entry|
                output << "# fuzzy\n" if entry.fuzzy?
                output << "#{entry.title}    => #{entry.id}\n"
                output << "\n"
        end
        if !orphaned.empty?
                output << "\n"
                output << "### These sections appear to have been removed. Please check.\n"
                output << "\n"
                orphaned.each do |entry|
                        output << "# fuzzy\n" if entry.fuzzy?
                        output << "#{entry.title}   =>        #{entry.id}\n"
                        output << "\n"
                end
        end
        open_io(filename_or_io, :write) do |f|
                f.write(output)
        end
end
stats() click to toggle source
# File lib/mizuho/id_map.rb, line 189
def stats
        fuzzy = 0
        orphaned = 0
        @entries.each_value do |entry|
                fuzzy += 1 if entry.fuzzy?
                orphaned += 1 if !entry.associated?
        end
        return { :fuzzy => fuzzy, :orphaned => orphaned }
end
xassociate(title) click to toggle source
# File lib/mizuho/id_map.rb, line 157
def xassociate(title)
        if entry = @entries[title]
                if entry.associated?
                        raise AlreadyAssociatedError, "Cannot associate an already associated title (#{title.inspect})"
                else
                        entry.associated = true
                        id = entry.id
                end
        elsif (moved_entry = find_moved(title)) || (similar_entry = find_similar(title))
                if moved_entry
                        puts "moved entry: #{title.inspect} -> #{moved_entry.title.inspect}"
                elsif similar_entry
                        puts "similar entry: #{title.inspect} -> #{similar_entry.title.inspect}"
                end
                entry = (moved_entry || similar_entry)
                @entries.delete(entry.title)
                @entries[title] = entry
                entry.title = title
                entry.associated = true
                entry.fuzzy = true if similar_entry
                id = entry.id
        else
                id = create_unique_id(title)
                add(title, id, false, true)
        end
        return id
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.