Class: EntityStorage::Storage
- Inherits:
-
Object
- Object
- EntityStorage::Storage
- Defined in:
- lib/entity_storage.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Read a value.
-
#[]=(index, value) ⇒ Object
Write a value.
-
#default(index) ⇒ Object
Returns the default value of a key contained in DEFAULT_KEYS global constant.
-
#default!(index) ⇒ Object
Resets the default value of a key contained in DEFAULT_KEYS global constant and returns the value.
-
#delete(index) ⇒ Object
Deletes a key and associated data from store.
-
#initialize(defaults = {}) ⇒ Storage
constructor
Checks for the existence of the necessary Entities table…
-
#method_missing(*args) ⇒ Object
Allows EntityStorage to be accessed via EntityStorage.whatever.
Constructor Details
#initialize(defaults = {}) ⇒ Storage
Checks for the existence of the necessary Entities table… if not here, creates it.
14 15 16 17 18 19 20 |
# File 'lib/entity_storage.rb', line 14 def initialize(defaults={}) unless ActiveRecord::Base.connection.table_exists?('entity_storage') AddEntitiesTable.up 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.
49 50 51 52 53 54 55 56 57 |
# File 'lib/entity_storage.rb', line 49 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
#defaults ⇒ Object
Returns the value of attribute defaults.
11 12 13 |
# File 'lib/entity_storage.rb', line 11 def defaults @defaults end |
Instance Method Details
#[](index) ⇒ Object
Read a value.
23 24 25 |
# File 'lib/entity_storage.rb', line 23 def [](index) Entity.get_value(index,@defaults[index.to_s]) end |
#[]=(index, value) ⇒ Object
Write a value.
28 29 30 |
# File 'lib/entity_storage.rb', line 28 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.
39 40 41 |
# File 'lib/entity_storage.rb', line 39 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.
44 45 46 |
# File 'lib/entity_storage.rb', line 44 def default!(index) Entity.reset_value(index,@defaults[index.to_s]) end |
#delete(index) ⇒ Object
Deletes a key and associated data from store.
33 34 35 |
# File 'lib/entity_storage.rb', line 33 def delete(index) Entity.remove_item(index) end |