eatenbyagrue-entity_storage 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ === 1.0.4 2009-09-23
2
+ * Fixed edge case bug with defaults and accessors with underscores
3
+
4
+ === 1.0.3 2009-09-23
5
+ * updated tests and example docs
6
+
1
7
  === 1.0.2 2009-09-21
2
8
  * changed method of table discovery for auto table creation
3
9
 
@@ -4,16 +4,17 @@ http://github.com/eatenbyagrue/entity_storage
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- An easy to use, persistent Key/Value store for any Ruby on Rails project. Designed to store config values & application wide state that need to survive server restarts.
7
+ An easy to use Key/Value store for any Ruby on Rails project. Like Memcache, only persistent. Stores config values & application wide state in the database in order to survive server restarts.
8
8
 
9
- Additionally, allows users to set a list of default keys that auto-initiliazes baseline key/value pairs in the database for easy initialization.
9
+ Designed to allow you to add persistent value storage to any Rails project in about 5 minutes.
10
+
11
+ Additionally, allows users to set a list of default keys that auto-initializes baseline key/value pairs in the database for easy initialization.
10
12
 
11
13
  == SYNOPSIS:
12
14
 
13
15
  You can use the entity store like so:
14
16
 
15
- # Get key value. If it doesn't exist and is specified in default list, will be initialized and returned.
16
- # If not in default list, will return nil.
17
+ # Get key value.
17
18
  e = EntityStore["testkey"]
18
19
  e = EntityStore.testkey
19
20
  e = EntityStore[:testkey]
@@ -25,6 +26,9 @@ You can use the entity store like so:
25
26
 
26
27
  # find out it's default, even if it's been changed
27
28
  e = EntityStore.default(:testkey)
29
+
30
+ # or
31
+ e = EntityStore.defaults[:testkey]
28
32
 
29
33
  # set it back to default
30
34
  EntityStore.default!(:testkey)
@@ -32,7 +36,9 @@ You can use the entity store like so:
32
36
  # delete an item
33
37
  EntityStore.delete(key)
34
38
 
35
- All EntityStorage operations sync immediately with the database, so a server shutdown will not impact stored values.
39
+ All EntityStorage operations sync immediately with the database, so a server shutdown will not impact stored values.
40
+
41
+ If you access a key that doesn't exist, and is specified in default list, will be initialized and returned. If not in default list, will return nil.
36
42
 
37
43
  Keys can be up to 512 characters in length. Values can be practically any size, and consist of any object. Objects are marshalled back and forth between database.
38
44
 
@@ -1,8 +1,8 @@
1
1
 
2
2
  adapter: mysql
3
- database: test
3
+ database: careers
4
4
  username: root
5
- password:
5
+ password: bnjstyle
6
6
  host: localhost
7
7
 
8
8
 
@@ -5,7 +5,7 @@ require 'rubygems'
5
5
  require 'activerecord'
6
6
 
7
7
  module EntityStorage
8
- VERSION = '1.0.2'
8
+ VERSION = '1.0.4'
9
9
 
10
10
  class Storage
11
11
  attr_accessor :defaults
@@ -21,7 +21,7 @@ module EntityStorage
21
21
 
22
22
  # Read a value.
23
23
  def [](index)
24
- Entity.get_value(index,defaults[index])
24
+ Entity.get_value(index,@defaults[index.to_s])
25
25
  end
26
26
 
27
27
  # Write a value.
@@ -37,23 +37,23 @@ module EntityStorage
37
37
  # Returns the default value of a key contained in DEFAULT_KEYS global constant.
38
38
  # Does not change the stored value. Use default! to reset the value.
39
39
  def default(index)
40
- self.defaults[:index]
40
+ @defaults[index.to_s]
41
41
  end
42
42
 
43
43
  # Resets the default value of a key contained in DEFAULT_KEYS global constant and returns the value.
44
44
  def default!(index)
45
- Entity.reset_value(index,defaults[index])
45
+ Entity.reset_value(index,@defaults[index.to_s])
46
46
  end
47
47
 
48
48
  # Allows EntityStorage[:whatever] to be accessed via EntityStorage.whatever.
49
49
  def method_missing(*args)
50
50
  if args.length == 1
51
- self[args[0]]
52
- elsif args.length == 2 and args[0].to_s =~ /^(.*)=$/
53
- self[$1.intern] = args[1]
54
- else
55
- super
56
- end
51
+ self[args[0]]
52
+ elsif args.length == 2 and args[0].to_s =~ /^(.*)=$/
53
+ self[$1.intern] = args[1]
54
+ else
55
+ super
56
+ end
57
57
  end
58
58
  end
59
59
 
@@ -88,8 +88,8 @@ module EntityStorage
88
88
  # If not found, will initialize according to defaults set in DEFAULT_KEYS global constant.
89
89
  def self.get_value(search_key,default_value)
90
90
  e = Entity.find_by_key(search_key.to_s)
91
- if e.nil?
92
- e = initialize_value(search_key,default_value)
91
+ if e.nil? || e.value.nil?
92
+ e = initialize_value(search_key,default_value)
93
93
  end
94
94
  e.value rescue nil
95
95
  end
@@ -98,7 +98,7 @@ module EntityStorage
98
98
  def self.set_value(search_key, new_value)
99
99
  e = Entity.find_by_key(search_key.to_s)
100
100
  if e.nil?
101
- e = new
101
+ e = new
102
102
  end
103
103
  e.key = search_key
104
104
  e.value = new_value
@@ -1,89 +1,106 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
+ ActiveRecord::Base.establish_connection(YAML::load(File.read(File.dirname(__FILE__) + '/../config/database.yml')))
2
3
 
3
4
  class TestEntityStorage < Test::Unit::TestCase
4
- DEFAULT_KEYS = { "test" => DateTime.parse("1-1-900"), "also test" => 2, "long ass key that I probably wouldn't use" => false }
5
- attr_accessor :entity_storage
5
+ DEFAULT_KEYS = { "test" => DateTime.parse("1-1-900"), "also test" => 2, "long ass key that I probably wouldn't use" => false,
6
+ # last time the hiring manager notifications have been run
7
+ "new_applicant_last_run" => DateTime.parse("1-1-1900"),
8
+ # last time the new job notifications have been run
9
+ "new_jobs_last_run" => DateTime.parse("1-1-1900"),
10
+ "email_test_address" => "joshuas@bnj.com" }
6
11
 
12
+ attr_accessor :EntityStore
13
+ EntityStore = EntityStorage::Storage.new(DEFAULT_KEYS)
14
+
7
15
  def setup
8
- ActiveRecord::Base.establish_connection(YAML::load(File.read(File.dirname(__FILE__) + '/../config/database.yml'))) unless ActiveRecord::Base.connected?
9
- self.entity_storage ||= EntityStorage::Storage.new(DEFAULT_KEYS)
16
+ ActiveRecord::Base.connection.execute("delete from entity_storage")
17
+ end
18
+
19
+ def test_instantiation
20
+ # test nasty weird bug with underscore characters
21
+ entityStore2 = EntityStorage::Storage.new(DEFAULT_KEYS)
22
+ assert_equal entityStore2.email_test_address, 'joshuas@bnj.com'
10
23
  end
11
24
 
12
25
  # tests value setting and getting functionality, along with default creation
13
26
  def test_defaultkeys
14
27
  DEFAULT_KEYS.each { |key,value|
15
- self.entity_storage.delete(key)
28
+ EntityStore.delete(key)
16
29
 
17
30
  # set key when it doesn't exist, should go to default
18
- e = self.entity_storage[key]
31
+ e = EntityStore[key]
19
32
  assert_equal e, value
20
33
 
34
+ EntityStore.delete(key)
35
+
21
36
  # set and try method missing access
22
37
  if key[/\w/] == key
23
- eval("e = self.entity_storage."+key)
24
- assert_equal e, value
25
- end
38
+ eval("e = EntityStore."+key)
39
+ assert_equal e, value
40
+ end
26
41
 
27
42
  # change it to something else
28
- self.entity_storage[key] = Time.now.to_s
29
- e = self.entity_storage[key]
43
+ EntityStore[key] = Time.now.to_s
44
+ e = EntityStore[key]
30
45
  assert_not_equal e, value
31
46
 
32
47
  # find out it's default
33
- e = self.entity_storage.default(key)
34
- assert_not_equal e, value
48
+ e = EntityStore.default(key)
49
+ assert_equal e, value
50
+
51
+ e = EntityStore.defaults[key]
52
+ assert_equal e, value
35
53
 
36
54
  # set it back to default
37
- self.entity_storage.default!(key)
38
- e = self.entity_storage[key]
55
+ EntityStore.default!(key)
56
+ e = EntityStore[key]
39
57
  assert_equal e, value
40
58
 
41
59
  # set it to something else using method missing
42
60
  if key[/\w/] == key
43
- eval("e = self.entity_storage."+key +" = Time.now.to_s")
44
- eval("e = self.entity_storage."+key)
45
- assert_equal e, value
61
+ eval("e = EntityStore."+key +" = Time.now.to_s")
62
+ assert_equal e, value
46
63
  end
47
64
  }
48
65
  end
49
66
 
50
67
  # tests setting values when no DEFAULT_KEY exists
51
68
  def test_settingvalues
52
- self.entity_storage.delete('test')
69
+ EntityStore.delete('test')
53
70
 
54
71
  # key that can't be accessed via method missing
55
72
  # set value and check it
56
- self.entity_storage['big long key that cannot be accessed via method missing'] = 'what up'
57
- e = self.entity_storage['big long key that cannot be accessed via method missing']
73
+ EntityStore['big long key that cannot be accessed via method missing'] = 'what up'
74
+ e = EntityStore['big long key that cannot be accessed via method missing']
58
75
  assert_equal e, 'what up'
59
76
 
60
77
  # set another value and check it
61
- self.entity_storage['big long key that cannot be accessed via method missing'] = 'another value'
62
- e = self.entity_storage['big long key that cannot be accessed via method missing']
78
+ EntityStore['big long key that cannot be accessed via method missing'] = 'another value'
79
+ e = EntityStore['big long key that cannot be accessed via method missing']
63
80
  assert_equal e, 'another value'
64
81
 
65
- self.entity_storage['test'] = 'what up'
66
- e = self.entity_storage['test']
82
+ EntityStore['test'] = 'what up'
83
+ e = EntityStore['test']
67
84
  assert_equal e, 'what up'
68
85
 
69
- self.entity_storage['test'] = 'another value'
70
- e = self.entity_storage['test']
86
+ EntityStore['test'] = 'another value'
87
+ e = EntityStore['test']
71
88
  assert_equal e, 'another value'
72
89
 
73
90
  # try the method missing version of the tests
74
- e = self.entity_storage.test = 'what up'
75
- e = self.entity_storage.test
91
+ e = EntityStore.test = 'what up'
92
+ e = EntityStore.test
76
93
  assert_equal e, 'what up'
77
94
 
78
- e = self.entity_storage.test = 'another value'
79
- e = self.entity_storage.test
95
+ e = EntityStore.test = 'another value'
96
+ e = EntityStore.test
80
97
  assert_equal e, 'another value'
81
98
 
82
99
  end
83
100
 
84
101
  def test_getunknownvalue
85
- self.entity_storage.delete('does not exist')
86
- e = self.entity_storage['does not exist']
102
+ EntityStore.delete('does not exist')
103
+ e = EntityStore['does not exist']
87
104
  assert_nil e
88
105
  end
89
106
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eatenbyagrue-entity_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Siler
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-21 00:00:00 -07:00
12
+ date: 2009-09-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.3.3
34
34
  version:
35
- description: An easy to use, persistent Key/Value store for any Ruby on Rails project. Designed to store config values & application wide state that need to survive server restarts. Additionally, allows users to set a list of default keys that auto-initiliazes baseline key/value pairs in the database for easy initialization.
35
+ description: An easy to use Key/Value store for any Ruby on Rails project. Like Memcache, only persistent. Stores config values &amp; application wide state in the database in order to survive server restarts. Designed to allow you to add persistent value storage to any Rails project in about 5 minutes. Additionally, allows users to set a list of default keys that auto-initializes baseline key/value pairs in the database for easy initialization.
36
36
  email:
37
37
  - joshua.siler@gmail.com
38
38
  executables: []
@@ -81,7 +81,7 @@ rubyforge_project: entity_storage
81
81
  rubygems_version: 1.3.5
82
82
  signing_key:
83
83
  specification_version: 3
84
- summary: An easy to use, persistent Key/Value store for any Ruby on Rails project
84
+ summary: An easy to use Key/Value store for any Ruby on Rails project
85
85
  test_files:
86
86
  - test/test_entity_storage.rb
87
87
  - test/test_helper.rb