Method: Gash::Helpers#normalize

Defined in:
lib/gash.rb

#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