Class: Sequel::Model

Inherits:
Object show all
Defined in:
lib/extensions/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create!(attributes = {}) ⇒ Object



30
31
32
33
34
# File 'lib/extensions/model.rb', line 30

def self.create! attributes={}
  instance = self.new attributes
  instance.save!
  instance
end

.reload(model) ⇒ Object



40
41
42
# File 'lib/extensions/model.rb', line 40

def self.reload model
  model.class[model.id] rescue model.class.filter(:id => model.id).first
end

Instance Method Details

#cached_association_id_key(association) ⇒ Object



45
46
47
# File 'lib/extensions/model.rb', line 45

def cached_association_id_key association
  "#{self.class.name}_#{id}_#{association}"
end

#get_cached_association(association) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/extensions/model.rb', line 62

def get_cached_association association
  return send(association.to_sym, false) if MEMCACHE['session_enabled'] != true
  cached_association_id = CACHE.get cached_association_id_key(association)
  if cached_association_id.nil?
    cached_association_id = update_cached_association association
  end
  if -1 == cached_association_id
    nil
  else
    association.to_upper_camelcase.constantize[cached_association_id]
  end
end

#memcached?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/extensions/model.rb', line 36

def memcached?
  MEMCACHE['session_enabled'] && MEMCACHE['model_caching_enabled'] && CACHE.get(cache_key).present?
end

#save!Object



23
24
25
26
27
28
# File 'lib/extensions/model.rb', line 23

def save!
  success = save
  if !success
    raise ValidationException.new(errors), "Could not save model due to failing validation: #{errors.inspect}"
  end
end

#update_cached_association(association) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/extensions/model.rb', line 49

def update_cached_association association
  if MEMCACHE['session_enabled'] == true
    cached_association = send association.to_sym, false
    cached_association_id = cached_association.present?? cached_association.id : -1
    CACHE.set cached_association_id_key(association), cached_association_id
    cached_association_id
  end
end

#update_cached_associations(*associations) ⇒ Object



58
59
60
# File 'lib/extensions/model.rb', line 58

def update_cached_associations *associations
  associations.each{ |association | update_cached_association association }
end