Class: ActiveRecord::Has::SparseAttributes::TableStorage

Inherits:
Storage
  • Object
show all
Defined in:
lib/active_record/has/sparse_attributes/table_storage.rb

Instance Attribute Summary collapse

Attributes inherited from Storage

#config, #record

Instance Method Summary collapse

Methods inherited from Storage

#before_save, #before_update, #initialize

Constructor Details

This class inherits a constructor from ActiveRecord::Has::SparseAttributes::Storage

Instance Attribute Details

#sparse_attribute_valuesObject

Returns the value of attribute sparse_attribute_values.



34
35
36
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 34

def sparse_attribute_values
  @sparse_attribute_values
end

#sparse_attributesObject

Returns the value of attribute sparse_attributes.



33
34
35
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 33

def sparse_attributes
  @sparse_attributes
end

#updated_sparse_attributesObject

Returns the value of attribute updated_sparse_attributes.



35
36
37
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 35

def updated_sparse_attributes
  @updated_sparse_attributes
end

Instance Method Details

#after_save(*args) ⇒ Object



37
38
39
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 37

def after_save(*args)
  save()
end

#get(name) ⇒ Object



82
83
84
85
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 82

def get(name)
  load() if @sparse_attributes.nil?
  return @sparse_attribute_values[name.to_s]
end

#loadObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 41

def load()
  @sparse_attributes = {}
  @sparse_attribute_values = {}
  @updated_sparse_attributes = []
  
  # The item has not been saved - nothing to load

  if @record.id.nil?
    return
  end
  
  unserialize = @config.serialize_values
  attributes = @config.attribute_model.where(@config.id_column => @record.id)
  attributes.each do |attr|
    @sparse_attributes[attr.name] = attr
    @sparse_attribute_values[attr.name] = unserialize ? YAML::load(attr.value) : attr.value
  end
end

#saveObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 59

def save()
  return 0 if @updated_sparse_attributes.nil?
  num_updates = 0
  klass = @config.attribute_model
  klass_id_column = @config.id_column
  serialize = @config.serialize_values
  @updated_sparse_attributes.each do |name|
    value = @sparse_attribute_values[name]
    have_attribute = @sparse_attributes.has_key?(name)
    
    # If the value is nil we will delete the attribute row

    if value.nil?
      num_updates += delete_row(name)
    else
      value = value.to_yaml if serialize
      num_updates += 1 if set_row(name, value)
    end
  end

  @updated_sparse_attributes = []
  return num_updates
end

#set(name, value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_record/has/sparse_attributes/table_storage.rb', line 87

def set(name, value)
  load() if @sparse_attributes.nil?

  name = name.to_s
  if value.nil?
    @sparse_attribute_values[name] = nil
  else
    value = value.to_s unless @config.serialize_values
    @sparse_attribute_values[name] = value
  end
  @updated_sparse_attributes << name
end