Class: EntityStorage::Entity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/entity_storage.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_value(search_key, default_value) ⇒ Object

Gets value based on specific key. If not found, will initialize according to defaults set in DEFAULT_KEYS global constant.



89
90
91
92
93
94
95
# File 'lib/entity_storage.rb', line 89

def self.get_value(search_key,default_value)
  e = Entity.find_by_key(search_key.to_s)
  if e.nil? || e.value.nil?
e = initialize_value(search_key,default_value)
  end
  e.value rescue nil
end

.remove_item(search_key) ⇒ Object

Deletes a record from key store.



115
116
117
118
# File 'lib/entity_storage.rb', line 115

def self.remove_item(search_key)
  e = Entity.find_by_key(search_key.to_s)
  e.destroy rescue 0
end

.reset_value(search_key, default_value) ⇒ Object

Resets a key contained in DEFAULT_KEYS global constant to it’s default value



109
110
111
112
# File 'lib/entity_storage.rb', line 109

def self.reset_value(search_key,default_value)
  Entity.remove_item(search_key)
  initialize_value(search_key,default_value).value   
end

.set_value(search_key, new_value) ⇒ Object

Sets value for a specific key. If key doesn’t exist, creates with value.



98
99
100
101
102
103
104
105
106
# File 'lib/entity_storage.rb', line 98

def self.set_value(search_key, new_value)
  e = Entity.find_by_key(search_key.to_s)
  if e.nil?
e = new
  end
  e.key = search_key
  e.value = new_value
  e.save
end

Instance Method Details

#key=(newkey) ⇒ Object



128
129
130
# File 'lib/entity_storage.rb', line 128

def key=(newkey)
  write_attribute(:key,newkey.to_s)
end

#valueObject



124
125
126
# File 'lib/entity_storage.rb', line 124

def value
  Marshal.load(read_attribute(:value))
end

#value=(data) ⇒ Object



120
121
122
# File 'lib/entity_storage.rb', line 120

def value=(data)
  write_attribute(:value,Marshal.dump(data))
end