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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/entity_storage.rb', line 18

def initialize(defaults={})
  self.defaults = defaults
  if !ActiveRecord::Base.connection.table_exists?('entity_storage')
puts "Creating entity table..."
AddEntitiesTable.create
    self['ENTITY_STORAGE_MASTER_VERSION']='2.1.2'
			elsif self['ENTITY_STORAGE_MASTER_VERSION'].nil? || self['ENTITY_STORAGE_MASTER_VERSION']!='2.1.2'
    self['ENTITY_STORAGE_MASTER_VERSION']='2.1.2'
    ActiveRecord::Base.connection.execute("show columns from entity_storage").each {|p|
      if p[0] == "value" && p[1] != "blob"
        puts "Migrating to new 2.1.x binary format..."
        ActiveRecord::Base.connection.execute("alter table entity_storage modify value blob")
        break
      end
    }
  end

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.



64
65
66
67
68
69
70
71
72
# File 'lib/entity_storage.rb', line 64

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.



15
16
17
# File 'lib/entity_storage.rb', line 15

def defaults
  @defaults
end

Instance Method Details

#[](index) ⇒ Object

Read a value.



38
39
40
# File 'lib/entity_storage.rb', line 38

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

#[]=(index, value) ⇒ Object

Write a value.



43
44
45
# File 'lib/entity_storage.rb', line 43

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.



54
55
56
# File 'lib/entity_storage.rb', line 54

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.



59
60
61
# File 'lib/entity_storage.rb', line 59

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

#delete(index) ⇒ Object

Deletes a key and associated data from store.



48
49
50
# File 'lib/entity_storage.rb', line 48

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