Module: Gash::Helpers

Included in:
Blob, Tree
Defined in:
lib/gash.rb

Overview

Some common methods used by both Tree and Blob.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



62
63
64
# File 'lib/gash.rb', line 62

def mode
  @mode
end

#parentObject

Returns the value of attribute parent.



62
63
64
# File 'lib/gash.rb', line 62

def parent
  @parent
end

#sha1Object

Returns the value of attribute sha1.



62
63
64
# File 'lib/gash.rb', line 62

def sha1
  @sha1
end

Instance Method Details

#blob?Boolean

Checks if this is a Blob.

Returns:

  • (Boolean)


78
# File 'lib/gash.rb', line 78

def blob?; self.class == Gash::Blob end

#changed!Object

Mark this, and all parents as changed.



84
# File 'lib/gash.rb', line 84

def changed!; @sha1 = nil;parent.changed! if parent end

#changed?Boolean

Checks if this object has been changed (since last commit).

Returns:

  • (Boolean)


82
# File 'lib/gash.rb', line 82

def changed?; !@sha1 end

#gashObject

Returns the Gash-object (top-parent).



86
# File 'lib/gash.rb', line 86

def gash; parent.gash if parent end

#initialize(opts = {}) ⇒ Object

Sets the accessors using a Hash:

tree = Gash::Tree.new(:sha1 => "some thing", :mode => "some thing",
                      :parent => "some parent")
tree.sha1 == "some thing"
tree.mode == "some thing"
tree.parent == "some parent"


71
72
73
74
75
# File 'lib/gash.rb', line 71

def initialize(opts = {})
  opts.each do |key, value|
    send("#{key}=", value)
  end
end

#normalize(value) ⇒ Object

Converts the value to a Tree or a Blob, using some rules:

If value is already a Tree or a Blob:

  • If value comes from another repo, we load it and return a deep copy.

  • If value got no parent, we simply return the same tree.

  • If value‘s parent is self, we also return the same tree.

  • If value‘s parent is something else, we return a duplicated tree.

If it’s something else:

  • If value is a Hash, we create a Tree from it.

  • If it’s not any of the former rules, we turn it into a string and create a Blob from it.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gash.rb', line 101

def normalize(value)
  case value
  when Tree, Blob, Gash
    if value.parent && value.parent != self
      if (g = value.gash) && self.gash == g
        value.dup
      else
        normalize(value.tree? ? value.to_hash : value.to_s)
      end
    else
      value
    end
  when Hash
    Tree[value]
  else
    Blob.new(:content => value.to_s)
  end
end

#tree?Boolean

Checks if this is a Tree.

Returns:

  • (Boolean)


80
# File 'lib/gash.rb', line 80

def tree?; self.class == Gash::Tree end