Method: GitModel::Persistable#save

Defined in:
lib/gitmodel/persistable.rb

#save(options = {}) ⇒ Object

Valid options are:

:transaction
OR:
:branch
:commit_message

Returns false if validations failed, otherwise returns the SHA of the commit



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gitmodel/persistable.rb', line 110

def save(options = {})
  _run_save_callbacks do 
    raise GitModel::NullId unless self.id

    if new_record?
      raise GitModel::RecordExists if self.class.exists?(self.id)
    else
      raise GitModel::RecordDoesntExist unless self.class.exists?(self.id)
    end

    GitModel.logger.debug "Saving #{self.class.name} with id: #{id}"

    dir = File.join(self.class.db_subdir, self.id)

    transaction = options.delete(:transaction) || GitModel::Transaction.new(options) 
    result = transaction.execute do |t|
      # Write the attributes to the attributes file
      t.index.add(File.join(dir, 'attributes.json'), Yajl::Encoder.encode(attributes, nil, :pretty => true))

      # Write the blob files
      blobs.each do |name, data|
        t.index.add(File.join(dir, name), data)
      end
    end

    if result
      @path = dir
      @branch = transaction.branch
      @new_record = false
    end

    result
  end
end