class Rubyfocus::Fetcher

The Fetcher class is a class that fetches data from a place - generally either a file location or a web address. The Fetcher is initialized with (by default) no options, although subclasses of Fetcher may change this.

Each Fetcher has two important methods:

You may also use the Fetcher#each_patch to iterate through all updates.

To patch a document, simply call +Fetcher#update_full()+, passing it the document to be patched. +Document#update+ will automatically send the fetcher the document.

Public Instance Methods

base() click to toggle source

Returns the content of the base file

# File lib/rubyfocus/fetchers/fetcher.rb, line 22
def base
        raise RuntimeError, "Method Fetcher#base called for abstract class Fetcher."
end
base_id() click to toggle source

Returns the id of the base

# File lib/rubyfocus/fetchers/fetcher.rb, line 27
def base_id
        raise RuntimeError, "Method Fetcher#base_id called for abstract class Fetcher."
end
can_patch?(document) click to toggle source

Can we patch this document? You should call this before update_onceing

# File lib/rubyfocus/fetchers/fetcher.rb, line 83
def can_patch?(document)
        !next_patch(document).nil?
end
can_reach_head_from?(id) click to toggle source

Can you reach head from the given ID?

# File lib/rubyfocus/fetchers/fetcher.rb, line 47
def can_reach_head_from?(id)
        patch_array = patches.select{ |p| p.from_ids.include?(id) }
        until patch_array.empty?
                p = patch_array.first
                return true if p.to_id == self.head

                next_patches = patches.select{ |np| np.from_ids.include? p.to_id }
                patch_array = patch_array[1..-1] + next_patches
        end
        return false
end
encode_with(coder) click to toggle source
# File lib/rubyfocus/fetchers/fetcher.rb, line 97
def encode_with(coder)
        raise RuntimeError, "Fetcher#encode_with called on abstract class."
end
encrypted?() click to toggle source

Is this fetcher encrypted?

# File lib/rubyfocus/fetchers/fetcher.rb, line 60
def encrypted?
        raise RuntimeError, "Method Fetcher#encrypted? called for abstract class Fetcher."
end
head() click to toggle source

Returns the ID of head - the latest patch committed to the database.

# File lib/rubyfocus/fetchers/fetcher.rb, line 42
def head
        @head ||= (patches.empty? ? base_id : patches.sort.last.to_id)
end
init_with(coder) click to toggle source

This method is called when loading a fetcher from YAML

# File lib/rubyfocus/fetchers/fetcher.rb, line 17
def init_with(coder)
        raise RuntimeError, "Method Fetcher#init_with called for abstract class Fetcher"
end
next_patch(document) click to toggle source

Collect the next patch to the document. If more than one patch can be applied, apply the latest one.

# File lib/rubyfocus/fetchers/fetcher.rb, line 89
def next_patch(document)
        all_possible_patches = self.patches.select{ |patch| patch.can_patch?(document) }
        return all_possible_patches.sort.last
end
patch(filename) click to toggle source

Returns the contents of a given patch

# File lib/rubyfocus/fetchers/fetcher.rb, line 37
def patch(filename)
        raise RuntimeError, "Method Fetcher#patch called for abstract class Fetcher."
end
patches() click to toggle source

Returns an array of patch paths - either files or urls

# File lib/rubyfocus/fetchers/fetcher.rb, line 32
def patches
        raise RuntimeError, "Method Fetcher#patches called for abstract class Fetcher."
end
reset() click to toggle source

Remove all cached information

# File lib/rubyfocus/fetchers/fetcher.rb, line 102
def reset
        @patches = nil
        @base = nil
end
update_full(document) click to toggle source

Update the document as far as we can

# File lib/rubyfocus/fetchers/fetcher.rb, line 68
def update_full(document)
        update_once(document) while can_patch?(document)
end
update_once(document) click to toggle source

Update the document one step. Raises an error if the document cannot be updated.

# File lib/rubyfocus/fetchers/fetcher.rb, line 73
def update_once(document)
        np = self.next_patch(document)
        if np
                np.apply_to(document)
        else
                raise RuntimeError, "Patcher#update_once called, but I can't find a patch to apply!"
        end
end