Module: Jason::Persistence::InstanceMethods

Defined in:
lib/jason/persistence.rb

Instance Method Summary collapse

Instance Method Details

#as_jsonObject



157
158
159
160
161
# File 'lib/jason/persistence.rb', line 157

def as_json
  jsonable = {}
  jsonable[Jason::singularize_key(self.class)] = self.to_hsh
  return jsonable
end

#attributesObject



130
131
132
# File 'lib/jason/persistence.rb', line 130

def attributes
  @attributes.merge!(:id => self.id)
end

#deleteObject



140
141
142
143
# File 'lib/jason/persistence.rb', line 140

def delete
  raise Jason::Errors::UndeletableError.new "Could not delete an unpersisted object!" if new_record?
  Encoding::PersistenceHandler.delete(self)
end

#initialize(attrs = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



123
124
125
126
127
128
# File 'lib/jason/persistence.rb', line 123

def initialize(attrs=nil)
  @attributes = attrs || {}
  @new_record = true
  process_attributes(attrs) unless attrs.nil?
  yield(self) if block_given?
end

#new_record?Boolean Also known as: persisted?

Returns:

  • (Boolean)


163
164
165
# File 'lib/jason/persistence.rb', line 163

def new_record?
  @new_record
end

#reload_attributesObject



168
169
170
171
172
173
# File 'lib/jason/persistence.rb', line 168

def reload_attributes
  self.class.defined_attributes.each do |attribute|
    called_attribute = self.send(attribute[:name])
    @attributes[attribute[:name]] = called_attribute if called_attribute
  end
end

#saveObject



134
135
136
137
138
# File 'lib/jason/persistence.rb', line 134

def save
  saved = Encoding::PersistenceHandler.persist(self, new_record? ? {} : {:update => true})
  @new_record = saved ? false : true
  return saved
end

#to_hshObject Also known as: to_hash



151
152
153
154
# File 'lib/jason/persistence.rb', line 151

def to_hsh
  #p self.attributes
  self.attributes
end

#update_attributes(attributes = {}) ⇒ Object



145
146
147
148
149
# File 'lib/jason/persistence.rb', line 145

def update_attributes(attributes={})
  process_attributes(attributes.merge(:id => self.id), :reload => true)
  updated = Encoding::PersistenceHandler.persist(self,:update => true)
  return updated
end