hashdown 0.2.2 → 0.2.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2ae3203f51852e88568edbd1e2166336e71a4c9
4
+ data.tar.gz: 73912f3ce1977885a168648f412f67248b3fce19
5
+ SHA512:
6
+ metadata.gz: ba7a79495e66c86d4d4996f928d6285e3d0f52b26e242d6dcba2a84e45f7a80ca68efa9f027980468be409b71a3054d87e73e9522554d8edbb291c266847cbc3
7
+ data.tar.gz: 8cfcbbfa8787db3a8bb645c52aef582f46d6170c58b54ea68a5d0950432c3d415e015bedd9cd2d0cfd347229b5dbcfaf25b478449ce68ee55248f27387b83ef7
data/hashdown.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = %q{super lightweight Rails plugin that adds hash-style lookups and option list (for generating dropdowns) to ActiveRecord models}
9
9
  gem.homepage = "https://github.com/rubysolo/hashdown"
10
10
 
11
- gem.add_dependency 'activerecord', '~> 3.0'
11
+ gem.add_dependency 'activerecord', '>= 3.0'
12
12
 
13
13
  gem.add_development_dependency 'pry-nav'
14
14
  gem.add_development_dependency 'rake'
@@ -1,6 +1,6 @@
1
1
  module Hashdown
2
2
  def self.cache
3
- @cache ||= rails_cache || local_cache
3
+ @cache ||= rails.cache || local_cache
4
4
  end
5
5
 
6
6
  def self.cache=(value)
@@ -8,17 +8,23 @@ module Hashdown
8
8
  end
9
9
 
10
10
  def self.force_cache_miss?
11
- Rails.env.test? if defined?(Rails)
11
+ rails.env.test?
12
12
  end
13
13
 
14
14
  def self.cache_key(source, class_name, token=nil)
15
- ['hashdown', class_name, source, token].compact.join('-')
15
+ ['hashdown', rails.env, class_name, source, token].compact.join('-')
16
16
  end
17
17
 
18
18
  def self.uncache(source, class_name, token)
19
19
  cache.delete(cache_key(source, class_name, token))
20
20
  end
21
21
 
22
+ def self.cached(cache_key)
23
+ cache.fetch(cache_key, :force => force_cache_miss?) do
24
+ yield
25
+ end
26
+ end
27
+
22
28
  class Config
23
29
  def initialize(hash={})
24
30
  @data = hash
@@ -41,8 +47,8 @@ module Hashdown
41
47
 
42
48
  private
43
49
 
44
- def self.rails_cache
45
- Rails.cache if defined?(Rails)
50
+ def self.rails
51
+ @rails ||= defined?(Rails) ? Rails : Config.new(cache: nil, env: Config.new)
46
52
  end
47
53
 
48
54
  def self.local_cache
@@ -20,14 +20,19 @@ module Hashdown
20
20
  end
21
21
 
22
22
  def [](token)
23
- Hashdown.cache.fetch(Hashdown.cache_key(:finder, self.to_s, token), :force => Hashdown.force_cache_miss?) do
24
- where({hashdown.finder.key => token.to_s}).first || (
25
- hashdown.finder.default? ?
26
- hashdown.finder.default :
27
- raise(ActiveRecord::RecordNotFound)
28
- )
23
+ cache_key = Hashdown.cache_key(:finder, self.to_s, token)
24
+
25
+ Hashdown.cached(cache_key) do
26
+ where({hashdown.finder.key => token.to_s}).first || hashdown_default_or_raise
29
27
  end
30
28
  end
29
+
30
+ private
31
+
32
+ def hashdown_default_or_raise
33
+ raise ActiveRecord::RecordNotFound unless hashdown.finder.default?
34
+ hashdown.finder.default
35
+ end
31
36
  end
32
37
 
33
38
  module InstanceMethods
@@ -24,12 +24,7 @@ module Hashdown
24
24
 
25
25
  module ClassMethods
26
26
  def select_options(*args)
27
- options = args.pop if args.last.is_a?(Hash)
28
- options ||= {}
29
-
30
- options[:label] ||= args.shift if args.any?
31
- options[:value] ||= args.shift if args.any?
32
- options.reverse_merge!(select_options_options)
27
+ options = generate_options_for_cache_key(args)
33
28
 
34
29
  scope = scoped
35
30
  options[:is_sorted] = scope.arel.orders.any?
@@ -37,7 +32,7 @@ module Hashdown
37
32
 
38
33
  cache_key = Hashdown.cache_key(:select_options, self.to_s, select_options_cache_key(options, scope))
39
34
 
40
- Hashdown.cache.fetch(cache_key, :force => Hashdown.force_cache_miss?) do
35
+ Hashdown.cached(cache_key) do
41
36
  if grouping = options[:group]
42
37
  scope.all.group_by {|record| grouping.call(record) }.map do |group, records|
43
38
  [group, map_records_to_select_options(records, options)]
@@ -50,6 +45,17 @@ module Hashdown
50
45
 
51
46
  private
52
47
 
48
+ def generate_options_for_cache_key(args)
49
+ options = args.pop if args.last.is_a?(Hash)
50
+ options ||= {}
51
+
52
+ options[:label] ||= args.shift if args.any?
53
+ options[:value] ||= args.shift if args.any?
54
+ options.reverse_merge!(select_options_options)
55
+
56
+ options
57
+ end
58
+
53
59
  def select_options_cache_key(options, scope)
54
60
  key = options.sort.each_with_object(""){|ck,(k,v)| ck << "#{ k }:#{ v };" }
55
61
  key << scope.to_sql
@@ -1,3 +1,3 @@
1
1
  module Hashdown
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -3,12 +3,16 @@ require 'spec_helper'
3
3
  describe Hashdown do
4
4
  describe 'cache' do
5
5
  describe 'in a Rails project' do
6
- before { Object.const_set('Rails', mock(cache: :rails_cache)) }
6
+ before { Object.const_set('Rails', mock(cache: :rails_cache, env: 'development')) }
7
7
  after { Object.send(:remove_const, 'Rails') }
8
8
 
9
9
  it 'delegates to Rails.cache if available' do
10
10
  Hashdown.cache.should eq Rails.cache
11
11
  end
12
+
13
+ it 'incorporates the environment in the cache key' do
14
+ Hashdown.cache_key(:finder, 'MyModel', 'some-value').should match(/development/)
15
+ end
12
16
  end
13
17
 
14
18
  it 'creates a new cache store if Rails.cache unavailable' do
@@ -32,7 +32,7 @@ describe Hashdown::SelectOptions do
32
32
  end
33
33
 
34
34
  it 'generates grouped options' do
35
- grouped_states = State.select_options(group: lambda{|state| state.name.first })
35
+ grouped_states = State.select_options(group: lambda { |state| state.name[0] })
36
36
  grouped_states.map(&:first).should eq %w( A C N T )
37
37
  grouped_states.detect{|group, states| group == 'C' }.last.length.should eq 2
38
38
  end
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,12 @@ require 'hashdown/select_options'
10
10
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
11
11
  load 'support/models.rb'
12
12
 
13
+ module Hashdown
14
+ def self.reset
15
+ @cache = @rails = nil
16
+ end
17
+ end
18
+
13
19
  RSpec.configure do |config|
14
20
  config.mock_with :rspec
15
21
 
@@ -18,10 +24,10 @@ RSpec.configure do |config|
18
24
  load 'support/schema.rb'
19
25
  load 'support/seeds.rb'
20
26
 
21
- Hashdown.cache = nil
27
+ Hashdown.reset
22
28
  end
23
29
 
24
30
  config.after(:each) do
25
- Hashdown.cache = nil
31
+ Hashdown.reset
26
32
  end
27
33
  end
metadata CHANGED
@@ -1,71 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
5
- prerelease:
4
+ version: 0.2.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Solomon White
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-15 00:00:00.000000000 Z
11
+ date: 2013-03-20 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
- requirement: &70280660899280 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ~>
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70280660899280
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: pry-nav
27
- requirement: &70280660898640 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70280660898640
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: rake
38
- requirement: &70280660897960 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *70280660897960
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: rspec
49
- requirement: &70280660889020 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *70280660889020
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: sqlite3
60
- requirement: &70280660888540 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - '>='
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *70280660888540
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Hashdown
70
84
  email:
71
85
  - rubysolo@gmail.com
@@ -93,27 +107,26 @@ files:
93
107
  - spec/support/seeds.rb
94
108
  homepage: https://github.com/rubysolo/hashdown
95
109
  licenses: []
110
+ metadata: {}
96
111
  post_install_message:
97
112
  rdoc_options: []
98
113
  require_paths:
99
114
  - lib
100
115
  required_ruby_version: !ruby/object:Gem::Requirement
101
- none: false
102
116
  requirements:
103
- - - ! '>='
117
+ - - '>='
104
118
  - !ruby/object:Gem::Version
105
119
  version: '0'
106
120
  required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
121
  requirements:
109
- - - ! '>='
122
+ - - '>='
110
123
  - !ruby/object:Gem::Version
111
124
  version: '0'
112
125
  requirements: []
113
126
  rubyforge_project:
114
- rubygems_version: 1.8.11
127
+ rubygems_version: 2.0.0
115
128
  signing_key:
116
- specification_version: 3
129
+ specification_version: 4
117
130
  summary: super lightweight Rails plugin that adds hash-style lookups and option list
118
131
  (for generating dropdowns) to ActiveRecord models
119
132
  test_files: