Module: Persistence

Included in:
BlocRecord::Base
Defined in:
lib/bloc_record/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/bloc_record/persistence.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#destroyObject



113
114
115
# File 'lib/bloc_record/persistence.rb', line 113

def destroy
  self.class.destroy(self.id)
end

#saveObject



101
102
103
# File 'lib/bloc_record/persistence.rb', line 101

def save
  self.save! rescue false
end

#save!Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bloc_record/persistence.rb', line 83

def save!
  unless self.id
    self.id = self.class.create(BlocRecord::Utility.instance_variables_to_hash(self)).id
    BlocRecord::Utility.reload_obj(self)
    return true
  end

  fields = self.class.attributes.map { |col| "#{col}=#{BlocRecord::Utility.sql_strings(self.instance_variable_get("@#{col}"))}" }.join(",")

  self.class.connection.execute <<-SQL
    UPDATE #{self.class.table}
    SET #{fields}
    WHERE id = #{self.id};
  SQL

  true
end

#update_attribute(attribute, value) ⇒ Object



105
106
107
# File 'lib/bloc_record/persistence.rb', line 105

def update_attribute(attribute, value)
  self.class.update(self.id, { attribute => value })
end

#update_attributes(updates) ⇒ Object



109
110
111
# File 'lib/bloc_record/persistence.rb', line 109

def update_attributes(updates)
  self.class.update(self.id, updates)
end