Sha256: 6f1c43848047889760eef56c73331f3474625d132dcb54c941dff26a8b3656dd

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Gitrb
  class Object
    attr_accessor :repository, :id
    alias sha id

    def initialize(options = {})
      @repository = options[:repository]
      @id = options[:id] || options[:sha]
    end

    CLASS = {}

    def object
      self
    end

    def self.inherited(subclass)
      CLASS[subclass.name[7..-1].downcase] = subclass
    end

    def self.factory(type, *args)
      klass = CLASS[type] or raise NotImplementedError, "Object type not supported: #{type}"
      klass.new(*args)
    end
  end

  class Reference
    def initialize(properties = {})
      @properties = properties
      @object = nil
    end

    def method_missing(name, *args, &block)
      if @object
        instance_eval "def self.#{name}(*args, &block); @object.#{name}(*args, &block); end"
        @object.send(name, *args, &block)
      elsif name == :type && (mode = @properties['mode'] || @properties[:mode])
        mode = mode.to_i(8)
        return (mode & 0x4000 == 0x4000) ? 'tree' : 'blob'
      elsif @properties.include?(name)
        return @properties[name]
      elsif @properties.include?(name.to_s)
        return @properties[name.to_s]
      elsif object
        method_missing(name, *args, &block)
      else
        super
      end
    end

    def object
      @object ||= repository.get(id)
    end

    def resolved?
      @object != nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gitrb-0.0.1 lib/gitrb/object.rb