cached_names 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +20 -0
- data/README.md +3 -0
- data/Rakefile +19 -0
- data/cached_names.gemspec +24 -0
- data/lib/cached_names/base.rb +113 -0
- data/lib/cached_names/version.rb +3 -0
- data/lib/cached_names.rb +2 -0
- data/lib/rails/cached_names.rb +7 -0
- data/spec/spec_helper.rb +11 -0
- metadata +93 -0
data/.gemtest
ADDED
File without changes
|
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@gems
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bbcoder (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rr (1.0.2)
|
11
|
+
rspec (2.4.0)
|
12
|
+
rspec-core (~> 2.4.0)
|
13
|
+
rspec-expectations (~> 2.4.0)
|
14
|
+
rspec-mocks (~> 2.4.0)
|
15
|
+
rspec-core (2.4.0)
|
16
|
+
rspec-expectations (2.4.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.4.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
bbcoder!
|
25
|
+
rr
|
26
|
+
rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 John "asceth" Long, Jason Dew
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require "rspec"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
Rspec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
gemspec = eval(File.read(File.join(Dir.pwd, "cached_names.gemspec")))
|
10
|
+
|
11
|
+
task :build => "#{gemspec.full_name}.gem"
|
12
|
+
|
13
|
+
task :test => :spec
|
14
|
+
|
15
|
+
file "#{gemspec.full_name}.gem" => gemspec.files + ["cached_names.gemspec"] do
|
16
|
+
system "gem build cached_names.gemspec"
|
17
|
+
system "gem install cached_names-#{CachedNames::VERSION}.gem"
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "cached_names/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "cached_names"
|
6
|
+
s.version = CachedNames::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["John 'asceth' Long", "Jason Dew"]
|
9
|
+
s.email = ["jasondew@gmail.com", "machinist@asceth.com"]
|
10
|
+
s.homepage = "http://github.com/asceth/cached_names"
|
11
|
+
s.summary = "Cached names of static data for use in selects"
|
12
|
+
s.description = "A gem for caching static data for use in selects"
|
13
|
+
|
14
|
+
s.rubyforge_project = "cached_names"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
s.add_development_dependency 'rr'
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
class CachedNames
|
2
|
+
|
3
|
+
def has_cached_names name_method = "value", options = {}
|
4
|
+
@cached_names_name_method = name_method
|
5
|
+
@cached_names_sort_field = options[:sorted_by] || @cached_names_name_method
|
6
|
+
@cached_names_group_method = options[:grouped_by]
|
7
|
+
@cached_names_cache_key_prefix = "cached_names_#{self}"
|
8
|
+
@cached_names_loaded = false
|
9
|
+
|
10
|
+
after_save { self.load_cached_names }
|
11
|
+
after_destroy { self.load_cached_names }
|
12
|
+
|
13
|
+
load_cached_names
|
14
|
+
end
|
15
|
+
|
16
|
+
def names
|
17
|
+
load_cached_names unless @cached_names_loaded
|
18
|
+
|
19
|
+
begin
|
20
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names"
|
21
|
+
rescue
|
22
|
+
load_cached_names
|
23
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def names_with_deleted
|
28
|
+
load_cached_names unless @cached_names_loaded
|
29
|
+
|
30
|
+
begin
|
31
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_with_deleted"
|
32
|
+
rescue
|
33
|
+
load_cached_names
|
34
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_with_deleted"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def names_group key
|
39
|
+
load_cached_names unless @cached_names_loaded
|
40
|
+
|
41
|
+
begin
|
42
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}"
|
43
|
+
rescue
|
44
|
+
load_cached_names
|
45
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def names_group_with_deleted key
|
50
|
+
load_cached_names unless @cached_names_loaded
|
51
|
+
|
52
|
+
begin
|
53
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted"
|
54
|
+
rescue
|
55
|
+
load_cached_names
|
56
|
+
Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def load_cached_names
|
61
|
+
begin
|
62
|
+
@cached_names_instances = all(:with_deleted => true, :order => @cached_names_sort_field)
|
63
|
+
|
64
|
+
load_names
|
65
|
+
load_grouped_names if @cached_names_group_method
|
66
|
+
|
67
|
+
@cached_names_loaded = true
|
68
|
+
rescue Exception => e
|
69
|
+
Rails.logger.error e.message
|
70
|
+
Rails.logger.error e.backtrace
|
71
|
+
@cached_names_loaded = false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def load_names
|
78
|
+
names = @cached_names_instances.map {|i| [i.deleted?, [i.send(@cached_names_name_method), i.id]] }
|
79
|
+
all_names, non_deleted_names = partition_by_deleted names
|
80
|
+
|
81
|
+
Rails.cache.write "#{@cached_names_cache_key_prefix}_names_with_deleted", all_names
|
82
|
+
Rails.cache.write "#{@cached_names_cache_key_prefix}_names", non_deleted_names
|
83
|
+
end
|
84
|
+
|
85
|
+
def load_grouped_names
|
86
|
+
groups = @cached_names_instances.inject({}) do |groups, instance|
|
87
|
+
value = instance.send(@cached_names_group_method)
|
88
|
+
|
89
|
+
groups[value] ||= []
|
90
|
+
groups[value] << [instance.deleted?, [instance.send(@cached_names_name_method), instance.id]]
|
91
|
+
|
92
|
+
groups
|
93
|
+
end
|
94
|
+
|
95
|
+
# when the value is nil, this means it is universal to all groups
|
96
|
+
universal_names = groups[nil] || []
|
97
|
+
|
98
|
+
groups.each do |(key, names)|
|
99
|
+
all_names, non_deleted_names = partition_by_deleted(names + universal_names)
|
100
|
+
|
101
|
+
Rails.cache.write "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted", all_names
|
102
|
+
Rails.cache.write "#{@cached_names_cache_key_prefix}_names_group_#{key}", non_deleted_names
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def partition_by_deleted names
|
107
|
+
all_names = names.map {|(deleted, name_id_pair)| name_id_pair }
|
108
|
+
non_deleted_names = names.map {|(deleted, name_id_pair)| name_id_pair unless deleted }.compact
|
109
|
+
|
110
|
+
[all_names, non_deleted_names]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
data/lib/cached_names.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cached_names
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John 'asceth' Long
|
9
|
+
- Jason Dew
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-02-23 00:00:00 -05:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: rspec
|
19
|
+
prerelease: false
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: "0"
|
26
|
+
type: :development
|
27
|
+
version_requirements: *id001
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rr
|
30
|
+
prerelease: false
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: "0"
|
37
|
+
type: :development
|
38
|
+
version_requirements: *id002
|
39
|
+
description: A gem for caching static data for use in selects
|
40
|
+
email:
|
41
|
+
- jasondew@gmail.com
|
42
|
+
- machinist@asceth.com
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
extra_rdoc_files: []
|
48
|
+
|
49
|
+
files:
|
50
|
+
- .gemtest
|
51
|
+
- .rspec
|
52
|
+
- .rvmrc
|
53
|
+
- Gemfile
|
54
|
+
- Gemfile.lock
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- cached_names.gemspec
|
59
|
+
- lib/cached_names.rb
|
60
|
+
- lib/cached_names/base.rb
|
61
|
+
- lib/cached_names/version.rb
|
62
|
+
- lib/rails/cached_names.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/asceth/cached_names
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: cached_names
|
88
|
+
rubygems_version: 1.5.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Cached names of static data for use in selects
|
92
|
+
test_files: []
|
93
|
+
|