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.



91
92
93
94
95
96
97
# File 'lib/entity_storage.rb', line 91

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.



117
118
119
120
# File 'lib/entity_storage.rb', line 117

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



111
112
113
114
# File 'lib/entity_storage.rb', line 111

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.



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

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



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

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

#valueObject



126
127
128
# File 'lib/entity_storage.rb', line 126

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

#value=(data) ⇒ Object



122
123
124
# File 'lib/entity_storage.rb', line 122

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