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.



302
303
304
# File 'lib/gash.rb', line 302

def content
  @content
end

Instance Method Details

#<=>(other) ⇒ Object

:nodoc:



313
314
315
316
317
318
319
# File 'lib/gash.rb', line 313

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:



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

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

#inspectObject

:nodoc:



309
310
311
# File 'lib/gash.rb', line 309

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.



305
306
307
# File 'lib/gash.rb', line 305

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