Module: GitModel::Persistable
- Defined in:
- lib/gitmodel/persistable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(new_attributes, guard_protected_attributes = true) ⇒ Object
- #blobs ⇒ Object
- #blobs=(new_blobs) ⇒ Object
-
#branch ⇒ Object
Get the branch that the record was last loaded from or was last saved on.
- #delete(options = {}) ⇒ Object
- #id ⇒ Object
- #id=(string) ⇒ Object
- #initialize(args = {}) ⇒ Object
- #new_record? ⇒ Boolean
-
#path ⇒ Object
Get the location of the record relative to the repository’s root.
- #persisted? ⇒ Boolean
-
#reload ⇒ Object
Reloads a record and returns the model instance.
-
#save(options = {}) ⇒ Object
Valid options are: :transaction OR: :branch :commit_message Returns false if validations failed, otherwise returns the SHA of the commit.
-
#save!(options = {}) ⇒ Object
Same as #save but raises an exception on error.
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
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
#attributes ⇒ Object
74 75 76 |
# File 'lib/gitmodel/persistable.rb', line 74 def attributes @attributes end |
#attributes=(new_attributes, guard_protected_attributes = true) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/gitmodel/persistable.rb', line 78 def attributes=(new_attributes, guard_protected_attributes = true) @attributes = HashWithIndifferentAccess.new if new_attributes new_attributes.each {|k,v| @attributes[k] = v} end end |
#blobs ⇒ Object
85 86 87 |
# File 'lib/gitmodel/persistable.rb', line 85 def blobs @blobs end |
#blobs=(new_blobs) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/gitmodel/persistable.rb', line 89 def blobs=(new_blobs) @blobs = HashWithIndifferentAccess.new if new_blobs new_blobs.each {|k,v| @blobs[k] = v} end end |
#branch ⇒ Object
Get the branch that the record was last loaded from or was last saved on.
The branch specified in the GitModel
config is used by default. Typically, the branch is ‘master’.
70 71 72 |
# File 'lib/gitmodel/persistable.rb', line 70 def branch @branch ||= GitModel.default_branch end |
#delete(options = {}) ⇒ Object
159 160 161 162 |
# File 'lib/gitmodel/persistable.rb', line 159 def delete( = {}) freeze self.class.delete(id, ) end |
#id ⇒ Object
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
96 97 98 |
# File 'lib/gitmodel/persistable.rb', line 96 def new_record? @new_record || false end |
#path ⇒ Object
Get the location of the record relative to the repository’s root.
It is determined by appending the name of the directory containing the record with the record’s id
.
61 62 63 |
# File 'lib/gitmodel/persistable.rb', line 61 def path @path ||= File.join(self.class.db_subdir, self.id) end |
#persisted? ⇒ Boolean
100 101 102 |
# File 'lib/gitmodel/persistable.rb', line 100 def persisted? !new_record? end |
#reload ⇒ Object
Reloads a record and returns the model instance.
The record is reloaded from the branch that the record was last loaded from or last saved to.
154 155 156 157 |
# File 'lib/gitmodel/persistable.rb', line 154 def reload load(path, branch) self end |
#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( = {}) _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 = .delete(:transaction) || GitModel::Transaction.new() 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 |
#save!(options = {}) ⇒ Object
Same as #save but raises an exception on error
146 147 148 |
# File 'lib/gitmodel/persistable.rb', line 146 def save!( = {}) save() || raise(GitModel::RecordNotSaved) end |
#to_key ⇒ Object
40 41 42 |
# File 'lib/gitmodel/persistable.rb', line 40 def to_key id ? [id] : nil end |
#to_model ⇒ Object
36 37 38 |
# File 'lib/gitmodel/persistable.rb', line 36 def to_model self end |
#to_param ⇒ Object
44 45 46 |
# File 'lib/gitmodel/persistable.rb', line 44 def to_param id && id.to_s end |
#to_s ⇒ Object
164 165 166 |
# File 'lib/gitmodel/persistable.rb', line 164 def to_s "#<#{self.class.name}:#{__id__} id=#{id}, attributes=#{attributes.inspect}, blobs.keys=#{blobs.keys.inspect}>" end |