constant_cache 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.0.2 2008-04-15
2
+
3
+ * Enhancements
4
+ * Better documentation
5
+ * Modify String class directly instead of using a mix-in
6
+
1
7
  == 0.0.1 2008-04-08
2
8
 
3
9
  * 1 major enhancement:
data/README CHANGED
@@ -1,5 +1,4 @@
1
- ConstantCache
2
- ===============
1
+ = Constant Cache
3
2
 
4
3
  When your database has tables that store lookup data, there is a tendency
5
4
  to provide those values as constants in the model. If you have an
@@ -21,21 +20,19 @@ The constants are stored as integer values and need to match up exactly
21
20
  with the data that's in the table (not necessarily a bad thing), but this
22
21
  solution forces you to write code like this:
23
22
 
24
- Account.new(:username => 'preagan', :status => AccountStatus.find(AccountStatus::PENDING))
23
+ Account.new(:username => 'preagan', :status => AccountStatus.find(AccountStatus::PENDING))
25
24
 
26
25
  This requires multiple calls to find and obfuscates the code a bit. Since classes
27
26
  in Ruby are executable code, we can cache the objects from the database at runtime
28
27
  and use them in your application.
29
28
 
30
- Installation
31
- ============
29
+ == Installation
32
30
 
33
31
  This code is packaged as a gem, so simply use the `gem` command to install:
34
32
 
35
33
  gem install constant_cache
36
34
 
37
- Example
38
- =======
35
+ == Example
39
36
 
40
37
  "Out of the box," the constant_cache gem assumes that you want to use the 'name' column to generate
41
38
  constants from a column called 'name' in your database table. Assuming this schema:
@@ -72,4 +69,9 @@ well:
72
69
  caches_constants :limit => 16
73
70
  end
74
71
 
75
- Copyright (c) 2007 Patrick Reagan of Viget Labs (patrick.reagan@viget.com), released under the MIT license
72
+ == Acknowlegements
73
+
74
+ Thanks to Dave Thomas for inspiring me to write this during his Metaprogramming talk at a Rails Edge
75
+ conference in early 2007.
76
+
77
+ Copyright (c) 2007 Patrick Reagan of Viget Labs (mailto:patrick.reagan@viget.com), released under the MIT license
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ GEM = "constant_cache"
8
8
  AUTHOR = "Patrick Reagan"
9
9
  EMAIL = "patrick.reagan@viget.com"
10
10
  HOMEPAGE = "http://www.viget.com/extend/"
11
- SUMMARY = "Patches active record to add a caches_constants class method that will cache lookup data for your application."
11
+ SUMMARY = "Patches ActiveRecord::Base to add a caches_constants class method that will cache lookup data for your application."
12
12
 
13
13
  spec = Gem::Specification.new do |s|
14
14
  s.name = GEM
@@ -26,8 +26,7 @@ spec = Gem::Specification.new do |s|
26
26
  s.add_dependency('activesupport', '>= 2.0.2')
27
27
 
28
28
  s.require_path = 'lib'
29
- s.autorequire = GEM
30
- s.files = %w(MIT-LICENSE README Rakefile HISTORY) + Dir.glob("{lib,spec}/**/*")
29
+ s.files = %w(MIT-LICENSE README Rakefile HISTORY) + Dir.glob("{lib,spec}/**/*")
31
30
  end
32
31
 
33
32
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -1,6 +1,5 @@
1
- require 'constant_cache/format'
1
+ require 'constant_cache/core_ext'
2
2
  require 'constant_cache/cache_methods'
3
3
 
4
- String.send(:include, ConstantCache::Format)
5
4
  ActiveRecord::Base.send(:extend, ConstantCache::CacheMethods::ClassMethods)
6
5
  ActiveRecord::Base.send(:include, ConstantCache::CacheMethods::InstanceMethods)
@@ -1,15 +1,48 @@
1
1
  module ConstantCache
2
+
3
+ CHARACTER_LIMIT = 64
2
4
 
5
+ #
6
+ # Generated constant conflicts with an existing constant
7
+ #
3
8
  class DuplicateConstantError < StandardError; end
4
- class InvalidLimitError < StandardError; end
9
+ class InvalidLimitError < StandardError #:nodoc:
10
+ end
5
11
 
6
- module CacheMethods
12
+ module CacheMethods #:nodoc:
7
13
 
8
14
  module ClassMethods
15
+
16
+ #
17
+ # The caches_constants method is the core of the functionality behind this mix-in. It provides
18
+ # a simple interface to cache the data in the corresponding table as constants on the model:
19
+ #
20
+ # class Status
21
+ # caches_constants
22
+ # end
23
+ #
24
+ # It makes certain assumptions about your schema: the constant created is based off of a <tt>name</tt>
25
+ # column in the database and long names are truncated to ConstantCache::CHARACTER_LIMIT characters before
26
+ # being set as constants. If there is a 'Pending' status in the database, you will now have a
27
+ # Status::PENDING constant that points to an instance of ActiveRecord.
28
+ #
29
+ # Beyond the basics, some configuration is allowed. You can change both the column that is used to generate
30
+ # the constant and the truncation length:
31
+ #
32
+ # class State
33
+ # caches_constants :key => :abbreviation, :limit => 2
34
+ # end
35
+ #
36
+ # This will use the <tt>abbreviation</tt> column to generate constants and will truncate constant names to 2
37
+ # characters at the maximum.
38
+ #
39
+ # Note: In the event that a generated constant conflicts with an existing constant, a
40
+ # ConstantCache::DuplicateConstantError is raised.
41
+ #
9
42
  def caches_constants(additional_options = {})
10
43
  cattr_accessor :cache_options
11
44
 
12
- self.cache_options = {:key => :name, :limit => 64}.merge(additional_options)
45
+ self.cache_options = {:key => :name, :limit => ConstantCache::CHARACTER_LIMIT}.merge(additional_options)
13
46
 
14
47
  raise ConstantCache::InvalidLimitError, "Limit of #{self.cache_options[:limit]} is invalid" if self.cache_options[:limit] < 1
15
48
  find(:all).each {|model| model.set_instance_as_constant }
@@ -18,16 +51,23 @@ module ConstantCache
18
51
 
19
52
  module InstanceMethods
20
53
 
21
- def constant_name
54
+ def constant_name #:nodoc:
22
55
  constant_name = self.send(self.class.cache_options[:key].to_sym).constant_name
23
56
  constant_name = constant_name[0, self.class.cache_options[:limit]] unless constant_name.blank?
24
57
  constant_name
25
58
  end
59
+ private :constant_name
26
60
 
61
+ #
62
+ # Create a constant on the class that pointing to an instance
63
+ #
27
64
  def set_instance_as_constant
28
65
  const = constant_name
29
66
  if !const.blank?
30
- raise ConstantCache::DuplicateConstantError, "Constant #{self.class.to_s}::#{const} has already been defined" if self.class.const_defined?(const)
67
+ if self.class.const_defined?(const)
68
+ message = "Constant #{self.class.to_s}::#{const} has already been defined"
69
+ raise ConstantCache::DuplicateConstantError, message
70
+ end
31
71
  self.class.const_set(const, self) if !const.blank?
32
72
  end
33
73
  end
@@ -0,0 +1,16 @@
1
+ class String
2
+
3
+ #
4
+ # A method to create a constant name from the existing string. This method condenses
5
+ # multiple non-word characters into a single underscore:
6
+ #
7
+ # 'abc'.constant_name => 'ABC'
8
+ # 'Restaurants & Bars'.constant_name => 'RESTAURANTS_BARS'
9
+ #
10
+ def constant_name
11
+ value = self.strip.gsub(/\s+/, '_').gsub(/[^\w_]/, '').gsub(/_{2,}/, '_').upcase
12
+ value = nil if value.blank?
13
+ value
14
+ end
15
+
16
+ end
@@ -1,9 +1,9 @@
1
1
  module ConstantCache
2
2
 
3
- module VERSION
3
+ module VERSION #:nodoc:
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
- TINY = 1
6
+ TINY = 2
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
@@ -1,8 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
- describe String, "with constant_name method" do
4
-
5
- before { String.send(:include, ConstantCache::Format) }
3
+ describe String do
6
4
 
7
5
  it "should upcase its characters" do
8
6
  'test'.constant_name.should == 'TEST'
data/spec/spec_helper.rb CHANGED
@@ -5,8 +5,6 @@ require 'mocha'
5
5
  require 'activesupport'
6
6
  require 'activerecord'
7
7
 
8
-
9
-
10
8
  lib_dir = File.join(File.dirname(__FILE__), %w(.. lib constant_cache))
11
9
 
12
10
  Dir.glob("#{lib_dir}/*.rb").each {|file| require file }
@@ -27,7 +25,6 @@ end
27
25
  Spec::Runner.configuration.mock_with :mocha
28
26
  Spec::Runner.configuration.include ConstantCache::SpecHelper
29
27
 
30
- String.send(:include, ConstantCache::Format)
31
28
  ActiveRecord::Base.send(:include, ConstantCache::CacheMethods::InstanceMethods)
32
29
  ActiveRecord::Base.send(:extend, ConstantCache::CacheMethods::ClassMethods)
33
30
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constant_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Reagan
8
- autorequire: constant_cache
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-08 00:00:00 -04:00
12
+ date: 2008-04-15 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  - !ruby/object:Gem::Version
31
31
  version: 2.0.2
32
32
  version:
33
- description: Patches active record to add a caches_constants class method that will cache lookup data for your application.
33
+ description: Patches ActiveRecord::Base to add a caches_constants class method that will cache lookup data for your application.
34
34
  email: patrick.reagan@viget.com
35
35
  executables: []
36
36
 
@@ -47,13 +47,13 @@ files:
47
47
  - HISTORY
48
48
  - lib/constant_cache
49
49
  - lib/constant_cache/cache_methods.rb
50
- - lib/constant_cache/format.rb
50
+ - lib/constant_cache/core_ext.rb
51
51
  - lib/constant_cache/version.rb
52
52
  - lib/constant_cache.rb
53
53
  - spec/examples
54
54
  - spec/examples/constant_cache
55
55
  - spec/examples/constant_cache/cache_methods_spec.rb
56
- - spec/examples/constant_cache/format_spec.rb
56
+ - spec/examples/constant_cache/core_ext_spec.rb
57
57
  - spec/spec_helper.rb
58
58
  has_rdoc: true
59
59
  homepage: http://www.viget.com/extend/
@@ -77,9 +77,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  requirements: []
78
78
 
79
79
  rubyforge_project:
80
- rubygems_version: 1.0.0
80
+ rubygems_version: 1.1.0
81
81
  signing_key:
82
82
  specification_version: 2
83
- summary: Patches active record to add a caches_constants class method that will cache lookup data for your application.
83
+ summary: Patches ActiveRecord::Base to add a caches_constants class method that will cache lookup data for your application.
84
84
  test_files: []
85
85
 
@@ -1,9 +0,0 @@
1
- module ConstantCache
2
- module Format
3
- def constant_name
4
- value = self.strip.gsub(/\s+/, '_').gsub(/[^\w_]/, '').gsub(/_{2,}/, '_').upcase
5
- value = nil if value.blank?
6
- value
7
- end
8
- end
9
- end