Class: Gash::Blob

Inherits:
Delegator
  • Object
show all
Includes:
Comparable, Helpers
Defined in:
lib/gash.rb

Overview

A Blob represent a string:

blob = Gash::Blob.new(:content => "Some content")
blob # => "Some content"

Using SHA1

However, if you provide a SHA1 (and have a parent which is connected to a Gash-object) it will then load the content from the repo when needed:

blob = Gash::Blob.new(:sha1 => "1234" * 10, :parent => gash_OR_tree_connected_to_gash)
blob        # => #<Blob:1234123412341234123412341234123412341234>
blob.upcase # It's loaded when needed
#blob.load! # or forced with #load!
blob        # => "Content of the blob"

Tree#[]= automatically sets the parent to itself, so you don’t need to provide it then:

tree["FILE"] = Gash::Blob.new(:sha1 => a_sha1)

See also: Helpers, Tree

Instance Attribute Summary collapse

Attributes included from Helpers

#mode, #parent, #sha1

Instance Method Summary collapse

Methods included from Helpers

#blob?, #changed!, #changed?, #gash, #initialize, #normalize, #tree?

Instance Attribute Details

#contentObject

Returns the value of attribute content.



322
323
324
# File 'lib/gash.rb', line 322

def content
  @content
end

Instance Method Details

#<=>(other) ⇒ Object

:nodoc:



333
334
335
336
337
338
339
# File 'lib/gash.rb', line 333

def <=>(other) #:nodoc:
  if other.is_a?(Blob) && sha1 && other.sha1
    sha1 <=> other.sha1
  else
    __getobj__ <=> other
  end
end

#__getobj__Object Also known as: to_s

:nodoc:



341
342
343
# File 'lib/gash.rb', line 341

def __getobj__ #:nodoc:
  @content ||= @sha1 ? load! : ''
end

#inspectObject

:nodoc:



329
330
331
# File 'lib/gash.rb', line 329

def inspect #:nodoc:
  @content ? @content.inspect : (@sha1 ? "#<Blob:#{@sha1}>" : to_s.inspect) 
end

#load!Object

Loads the file from Git, unless it’s already been loaded.



325
326
327
# File 'lib/gash.rb', line 325

def load!
  @content ||= gash.send(:cat_file, @sha1)
end