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
- #delete(options = {}) ⇒ Object
- #id ⇒ Object
- #id=(string) ⇒ Object
- #initialize(args = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
-
#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
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 |
#blobs ⇒ Object
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
127 128 129 130 |
# File 'lib/gitmodel/persistable.rb', line 127 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
79 80 81 |
# File 'lib/gitmodel/persistable.rb', line 79 def new_record? @new_record || false end |
#persisted? ⇒ Boolean
83 84 85 |
# File 'lib/gitmodel/persistable.rb', line 83 def persisted? !new_record? end |
#save(options = {}) ⇒ Object
Valid options are:
:transaction
OR:
:branch
:commit_message
Returns false if validations failed, otherwise returns the SHA of the commit
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gitmodel/persistable.rb', line 93 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 result end end |
#save!(options = {}) ⇒ Object
Same as #save but raises an exception on error
123 124 125 |
# File 'lib/gitmodel/persistable.rb', line 123 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
132 133 134 |
# File 'lib/gitmodel/persistable.rb', line 132 def to_s "#<#{self.class.name}:#{__id__} id=#{id}, attributes=#{attributes.inspect}, blobs.keys=#{blobs.keys.inspect}>" end |