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.



104
105
106
107
108
109
110
# File 'lib/entity_storage.rb', line 104

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.



134
135
136
137
# File 'lib/entity_storage.rb', line 134

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



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

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/entity_storage.rb', line 113

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

Instance Method Details

#key=(newkey) ⇒ Object



147
148
149
# File 'lib/entity_storage.rb', line 147

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

#valueObject



143
144
145
# File 'lib/entity_storage.rb', line 143

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

#value=(data) ⇒ Object



139
140
141
# File 'lib/entity_storage.rb', line 139

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