dm-constant-cache 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,8 @@ begin
12
12
  gem.homepage = "http://www.viget.com/extend/"
13
13
  gem.files = %w(MIT-LICENSE README.md Rakefile) + Dir.glob("{lib,test}/**/*")
14
14
 
15
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ gem.add_development_dependency "shoulda", ">= 0"
16
+ gem.add_development_dependency "jferris-mocha", ">= 0"
16
17
  end
17
18
  Jeweler::GemcutterTasks.new
18
19
  rescue LoadError
@@ -8,7 +8,7 @@ module ConstantCache
8
8
  end
9
9
 
10
10
  def self.cache!
11
- @descendants.each {|klass| klass.all.each {|instance| instance.set_instance_as_constant }}
11
+ @descendants.each {|klass| klass.cache!}
12
12
  end
13
13
 
14
14
  #
@@ -22,7 +22,7 @@ module ConstantCache
22
22
  # a simple interface to cache the data in the corresponding table as constants on the model:
23
23
  #
24
24
  # class Status
25
- # caches_constants
25
+ # include ConstantCache
26
26
  # end
27
27
  #
28
28
  # It makes certain assumptions about your schema: the constant created is based off of a <tt>name</tt>
@@ -36,7 +36,8 @@ module ConstantCache
36
36
  # class State
37
37
  # include ConstantCache
38
38
  #
39
- # caches_constants :key => :abbreviation, :limit => 2
39
+ # cache_as :abbreviation
40
+ # cache_limit 2
40
41
  # end
41
42
  #
42
43
  # This will use the <tt>abbreviation</tt> column to generate constants and will truncate constant names to 2
@@ -54,13 +55,18 @@ module ConstantCache
54
55
  end
55
56
 
56
57
  def cache_options
57
- # @cache_options = {:key => :name, :limit => CHARACTER_LIMIT}.merge(@cache_options || {})
58
58
  @cache_options ||= {:key => :name, :limit => CHARACTER_LIMIT}
59
59
  end
60
60
 
61
61
  def reset_cache_options
62
62
  @cache_options = {:key => :name, :limit => CHARACTER_LIMIT}
63
63
  end
64
+
65
+ def cache!
66
+ unless DataMapper::Repository.adapters.empty?
67
+ all.each {|instance| instance.set_instance_as_constant }
68
+ end
69
+ end
64
70
  end
65
71
 
66
72
  #
@@ -79,4 +85,4 @@ module ConstantCache
79
85
  end
80
86
  end
81
87
  private :constant_name
82
- end
88
+ end
@@ -11,5 +11,4 @@ class String
11
11
  value = self.strip.gsub(/\s+/, '_').gsub(/[^\w_]/, '').gsub(/_{2,}/, '_').upcase
12
12
  (value == '') ? nil : value
13
13
  end
14
-
15
- end
14
+ end
@@ -1,5 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
+ module DataMapper
4
+ end
5
+
3
6
  class Cached
4
7
  include ConstantCache
5
8
 
@@ -11,23 +14,25 @@ class CacheMethodsTest < Test::Unit::TestCase
11
14
  context "A class with ConstantCache mixed in" do
12
15
  should "have default options for the cache key and character limit" do
13
16
  Cached.reset_cache_options
14
- Cached.cache_options.should == {:key => :name, :limit => 64}
17
+ assert_equal({:key => :name, :limit => 64}, Cached.cache_options)
15
18
  end
16
19
 
17
20
  should "all overridden options for key and character limit" do
18
21
  Cached.cache_as :abbreviation
19
22
  Cached.cache_limit 20
20
- Cached.cache_options.should == {:key => :abbreviation, :limit => 20}
23
+ assert_equal({:key => :abbreviation, :limit => 20}, Cached.cache_options)
21
24
  end
22
25
 
23
26
  should "revert the limit on characters if less than 1" do
24
27
  Cached.cache_limit -10
25
- Cached.cache_options.should == {:key => :name, :limit => 64}
28
+ assert_equal({:key => :name, :limit => 64}, Cached.cache_options)
26
29
  end
27
30
  end
28
31
 
29
32
  context "ConstantCache" do
30
33
  should "be able to cache all instances as constants" do
34
+ DataMapper::Repository = stub(:adapters => ["an adapter"])
35
+
31
36
  c1 = Cached.new
32
37
  c1.name = 'al einstein'
33
38
  c1.expects(:set_instance_as_constant)
@@ -50,14 +55,14 @@ class CacheMethodsTest < Test::Unit::TestCase
50
55
  should "create a constant as a reference to the instance" do
51
56
  @cached.name = 'al sharpton'
52
57
  @cached.set_instance_as_constant
53
- Cached.constants.include?("AL_SHARPTON").should == true
54
- Cached::AL_SHARPTON.should == @cached
58
+ assert_equal true, Cached.constants.include?("AL_SHARPTON")
59
+ assert_equal @cached, Cached::AL_SHARPTON
55
60
  end
56
61
 
57
62
  should "not create a constant without a key value" do
58
63
  size = Cached.constants.size
59
64
  @cached.set_instance_as_constant
60
- Cached.constants.size.should == size
65
+ assert_equal size, Cached.constants.size
61
66
  end
62
67
 
63
68
  should "not raise an exception on duplicate constant" do
@@ -73,8 +78,8 @@ class CacheMethodsTest < Test::Unit::TestCase
73
78
 
74
79
  @cached.name = 'a'*65
75
80
  @cached.set_instance_as_constant
76
-
77
- Cached.constants.include?(constant_name).should == true
81
+
82
+ assert_equal true, Cached.constants.include?(constant_name)
78
83
  end
79
84
  end
80
85
  end
@@ -3,31 +3,31 @@ require 'test_helper'
3
3
  class CoreExtTest < Test::Unit::TestCase
4
4
  context "The String core extension" do
5
5
  should "upcase its characters" do
6
- 'test'.constant_name.should == 'TEST'
6
+ assert_equal 'TEST', 'test'.constant_name
7
7
  end
8
8
 
9
9
  should "replace whitespace with a single underscore" do
10
- "test this \tformat\nplease.".constant_name.should == 'TEST_THIS_FORMAT_PLEASE'
10
+ assert_equal 'TEST_THIS_FORMAT_PLEASE', "test this \tformat\nplease.".constant_name
11
11
  end
12
12
 
13
13
  should "remove leading and trailing whitespace" do
14
- ' test '.constant_name.should == 'TEST'
14
+ assert_equal 'TEST', ' test '.constant_name
15
15
  end
16
16
 
17
17
  should "remove non-word characters" do
18
- '!test?'.constant_name.should == 'TEST'
18
+ assert_equal 'TEST', '!test?'.constant_name
19
19
  end
20
20
 
21
21
  should "not singularize plural name" do
22
- 'tests'.constant_name.should == 'TESTS'
22
+ assert_equal 'TESTS', 'tests'.constant_name
23
23
  end
24
24
 
25
25
  should "return nil when all characters are removed" do
26
- '?'.constant_name.should be(nil)
26
+ assert_nil '?'.constant_name
27
27
  end
28
28
 
29
29
  should "collapse multiple underscores" do
30
- 'test__me'.constant_name.should == 'TEST_ME'
30
+ assert_equal 'TEST_ME', 'test__me'.constant_name
31
31
  end
32
32
  end
33
- end
33
+ end
@@ -3,7 +3,6 @@ $:.reject! { |e| e.include? 'TextMate' }
3
3
  require 'rubygems'
4
4
  require 'test/unit'
5
5
  require 'shoulda'
6
- require 'matchy'
7
6
  require 'mocha'
8
7
 
9
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-constant-cache
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 2
8
- - 0
9
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Tony Pitale
@@ -15,21 +16,37 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-06-21 00:00:00 -07:00
19
+ date: 2010-08-01 00:00:00 -04:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
- name: thoughtbot-shoulda
23
+ name: shoulda
23
24
  prerelease: false
24
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
25
27
  requirements:
26
28
  - - ">="
27
29
  - !ruby/object:Gem::Version
30
+ hash: 3
28
31
  segments:
29
32
  - 0
30
33
  version: "0"
31
34
  type: :development
32
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: jferris-mocha
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
33
50
  description: ""
34
51
  email: tony.pitale@viget.com
35
52
  executables: []
@@ -60,23 +77,27 @@ rdoc_options:
60
77
  require_paths:
61
78
  - lib
62
79
  required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
63
81
  requirements:
64
82
  - - ">="
65
83
  - !ruby/object:Gem::Version
84
+ hash: 3
66
85
  segments:
67
86
  - 0
68
87
  version: "0"
69
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
70
90
  requirements:
71
91
  - - ">="
72
92
  - !ruby/object:Gem::Version
93
+ hash: 3
73
94
  segments:
74
95
  - 0
75
96
  version: "0"
76
97
  requirements: []
77
98
 
78
99
  rubyforge_project:
79
- rubygems_version: 1.3.6
100
+ rubygems_version: 1.3.7
80
101
  signing_key:
81
102
  specification_version: 3
82
103
  summary: ""