Module: GitModel::Persistable

Defined in:
lib/gitmodel/persistable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitmodel/persistable.rb', line 4

def self.included(base)
  base.class_eval do
     
    extend ActiveModel::Callbacks
    extend ActiveModel::Naming
    include ActiveModel::Validations
    include ActiveModel::Dirty
    include ActiveModel::Observing
    include ActiveModel::Translation

    define_model_callbacks :initialize, :find, :touch, :only => :after
    define_model_callbacks :save, :create, :update, :destroy

    cattr_accessor :index, true
    self.index = GitModel::Index.new(self)
  end

  base.extend(ClassMethods)
end

Instance Method Details

#attributesObject



57
58
59
# File 'lib/gitmodel/persistable.rb', line 57

def attributes
  @attributes
end

#attributes=(new_attributes, guard_protected_attributes = true) ⇒ Object



61
62
63
64
65
66
# File 'lib/gitmodel/persistable.rb', line 61

def attributes=(new_attributes, guard_protected_attributes = true)
  @attributes = HashWithIndifferentAccess.new
  if new_attributes
    new_attributes.each {|k,v| @attributes[k] = v}
  end
end

#blobsObject



68
69
70
# File 'lib/gitmodel/persistable.rb', line 68

def blobs
  @blobs
end

#blobs=(new_blobs) ⇒ Object



72
73
74
75
76
77
# File 'lib/gitmodel/persistable.rb', line 72

def blobs=(new_blobs)
  @blobs = HashWithIndifferentAccess.new
  if new_blobs
    new_blobs.each {|k,v| @blobs[k] = v}
  end
end

#delete(options = {}) ⇒ Object



123
124
125
126
# File 'lib/gitmodel/persistable.rb', line 123

def delete(options = {})
  freeze
  self.class.delete(id, options)
end

#idObject



48
49
50
# File 'lib/gitmodel/persistable.rb', line 48

def id
  @id
end

#id=(string) ⇒ Object



52
53
54
55
# File 'lib/gitmodel/persistable.rb', line 52

def id=(string)
  # TODO ensure is valid as a filename
  @id = string
end

#initialize(args = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/gitmodel/persistable.rb', line 25

def initialize(args = {})
  _run_initialize_callbacks do
    @new_record = true 
    self.attributes = {}
    self.blobs = {}
    args.each do |k,v|
      self.send("#{k}=".to_sym, v)
    end
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gitmodel/persistable.rb', line 79

def new_record?
  @new_record || false
end

#save(options = {}) ⇒ Object

Valid options are:

:transaction
OR:
:branch
:commit_message

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gitmodel/persistable.rb', line 89

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

    result
  end
end

#save!(options = {}) ⇒ Object

Same as #save but raises an exception on error



119
120
121
# File 'lib/gitmodel/persistable.rb', line 119

def save!(options = {})
  save(options) || raise(GitModel::RecordNotSaved)
end

#to_keyObject



40
41
42
# File 'lib/gitmodel/persistable.rb', line 40

def to_key
  id ? [id] : nil
end

#to_modelObject



36
37
38
# File 'lib/gitmodel/persistable.rb', line 36

def to_model
  self
end

#to_paramObject



44
45
46
# File 'lib/gitmodel/persistable.rb', line 44

def to_param
  id && id.to_s
end

#to_sObject



128
129
130
# File 'lib/gitmodel/persistable.rb', line 128

def to_s
  "#<#{self.class.name}:#{__id__} id=#{id}, attributes=#{attributes.inspect}, blobs.keys=#{blobs.keys.inspect}>"
end