Class: EntityStorage::Storage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}) ⇒ Storage

Checks for the existence of the necessary Entities table… if not here, creates it.



15
16
17
18
19
20
21
# File 'lib/entity_storage.rb', line 15

def initialize(defaults={})
  unless ActiveRecord::Base.connection.table_exists?('entity_storage')
AddEntitiesTable.create
  end

  self.defaults = defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

Allows EntityStorage to be accessed via EntityStorage.whatever.



50
51
52
53
54
55
56
57
58
# File 'lib/entity_storage.rb', line 50

def method_missing(*args)
  if args.length == 1
self[args[0]]
      elsif args.length == 2 and args[0].to_s =~ /^(.*)=$/
self[$1.intern] = args[1]
      else
super
      end
end

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



12
13
14
# File 'lib/entity_storage.rb', line 12

def defaults
  @defaults
end

Instance Method Details

#[](index) ⇒ Object

Read a value.



24
25
26
# File 'lib/entity_storage.rb', line 24

def [](index)
  Entity.get_value(index,@defaults[index.to_s])
end

#[]=(index, value) ⇒ Object

Write a value.



29
30
31
# File 'lib/entity_storage.rb', line 29

def []=(index,value)
  Entity.set_value(index,value)
end

#default(index) ⇒ Object

Returns the default value of a key contained in DEFAULT_KEYS global constant. Does not change the stored value. Use default! to reset the value.



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

def default(index)
  @defaults[index.to_s]
end

#default!(index) ⇒ Object

Resets the default value of a key contained in DEFAULT_KEYS global constant and returns the value.



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

def default!(index)
  Entity.reset_value(index,@defaults[index.to_s])
end

#delete(index) ⇒ Object

Deletes a key and associated data from store.



34
35
36
# File 'lib/entity_storage.rb', line 34

def delete(index)
  Entity.remove_item(index)
end