dataset 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *1.3.2 [Bugfix]
2
+
3
+ * Fixed bug where sti finder names were not always generated correctly [aiwilliams]
4
+
1
5
  *1.3.0 [Rails 2.3.2] (??)
2
6
 
3
7
  * Supporting Rails 2.3.2 [mhawkins, underlog]
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 1
4
4
  :minor: 3
@@ -13,6 +13,7 @@ require 'dataset/load'
13
13
  require 'dataset/resolver'
14
14
  require 'dataset/session'
15
15
  require 'dataset/session_binding'
16
+ require 'dataset/record/heirarchy'
16
17
  require 'dataset/record/meta'
17
18
  require 'dataset/record/fixture'
18
19
  require 'dataset/record/model'
@@ -17,14 +17,27 @@ module Dataset
17
17
  end
18
18
  end
19
19
 
20
+ def record_heirarchy(record_class)
21
+ base_class = record_class.base_class
22
+ record_heirarchies[base_class] ||= Dataset::Record::Heirarchy.new(base_class)
23
+ end
24
+
20
25
  def record_meta(record_class)
21
- record_metas[record_class] ||= Dataset::Record::Meta.new(record_class)
26
+ record_metas[record_class] ||= begin
27
+ heirarchy = record_heirarchy(record_class)
28
+ heirarchy.update(record_class)
29
+ Dataset::Record::Meta.new(heirarchy, record_class)
30
+ end
22
31
  end
23
32
 
24
33
  protected
25
34
  def record_metas
26
35
  @record_metas ||= Hash.new
27
36
  end
37
+
38
+ def record_heirarchies
39
+ @record_heirarchies ||= Hash.new
40
+ end
28
41
  end
29
42
  end
30
43
  end
@@ -0,0 +1,65 @@
1
+ module Dataset
2
+ module Record # :nodoc:
3
+
4
+ class Heirarchy # :nodoc:
5
+ attr_reader :base_class, :class_name, :columns, :table_name
6
+
7
+ delegate :inheritance_column, :to => :base_class
8
+
9
+ def initialize(base_class)
10
+ @base_class = base_class
11
+ @class_name = base_class.name
12
+ @table_name = base_class.table_name
13
+ @columns = base_class.columns
14
+ end
15
+
16
+ def id_cache_key
17
+ @id_cache_key ||= table_name
18
+ end
19
+
20
+ def id_finder_names
21
+ @id_finder_names ||= [id_finder_name(base_class)]
22
+ end
23
+
24
+ def model_finder_names
25
+ @model_finder_names ||= [model_finder_name(base_class)]
26
+ end
27
+
28
+ def to_s
29
+ "#<Heirarchy: #{table_name}>"
30
+ end
31
+
32
+ def update(record_class)
33
+ record_class.ancestors.each do |c|
34
+ finder_name = model_finder_name(c)
35
+ unless model_finder_names.include?(finder_name)
36
+ model_finder_names << finder_name
37
+ id_finder_names << id_finder_name(c)
38
+ end
39
+ end
40
+ end
41
+
42
+ def finder_name(klass)
43
+ klass.name.underscore.gsub('/', '_').sub(/^(\w)_/, '\1').gsub(/_(\w)_/, '_\1')
44
+ end
45
+
46
+ def id_finder_name(klass)
47
+ "#{finder_name(klass)}_id".to_sym
48
+ end
49
+
50
+ def model_finder_name(klass)
51
+ finder_name(klass).pluralize.to_sym
52
+ end
53
+
54
+ def timestamp_columns
55
+ @timestamp_columns ||= begin
56
+ timestamps = %w(created_at created_on updated_at updated_on)
57
+ columns.select do |column|
58
+ timestamps.include?(column.name)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -4,62 +4,26 @@ module Dataset
4
4
  # A mechanism to cache information about an ActiveRecord class to speed
5
5
  # things up a bit for insertions, finds, and method generation.
6
6
  class Meta # :nodoc:
7
- attr_reader :class_name, :columns, :record_class, :table_name
7
+ attr_reader :heirarchy, :class_name, :record_class
8
8
 
9
9
  # Provides information necessary to insert STI classes correctly for
10
10
  # later reading.
11
- delegate :name, :inheritance_column, :sti_name, :to => :record_class
11
+ delegate :name, :sti_name, :to => :record_class
12
+ delegate :inheritance_column, :table_name, :timestamp_columns, :to => :heirarchy
12
13
 
13
- def initialize(record_class)
14
- @record_class = record_class
15
- @class_name = record_class.name
16
- @table_name = record_class.table_name
17
- @columns = record_class.columns
18
- end
19
-
20
- def id_cache_key
21
- @id_cache_key ||= table_name
14
+ def initialize(heirarchy, record_class)
15
+ @heirarchy = heirarchy
16
+ @record_class = record_class
17
+ @class_name = record_class.name
22
18
  end
23
19
 
24
20
  def inheriting_record?
25
21
  !record_class.descends_from_active_record?
26
22
  end
27
23
 
28
- def timestamp_columns
29
- @timestamp_columns ||= begin
30
- timestamps = %w(created_at created_on updated_at updated_on)
31
- columns.select do |column|
32
- timestamps.include?(column.name)
33
- end
34
- end
35
- end
36
-
37
- def id_finder_names
38
- @id_finder_names ||= begin
39
- names = descendants.collect {|c| finder_name c}
40
- names.uniq.collect {|n| "#{n}_id".to_sym}
41
- end
42
- end
43
-
44
- def model_finder_names
45
- @record_finder_names ||= descendants.collect {|c| finder_name(c).pluralize.to_sym}.uniq
46
- end
47
-
48
24
  def to_s
49
25
  "#<RecordMeta: #{table_name}>"
50
26
  end
51
-
52
- def descendants
53
- if record_class.respond_to?(:self_and_descendents_from_active_record)
54
- record_class.self_and_descendents_from_active_record
55
- else
56
- record_class.self_and_descendants_from_active_record
57
- end
58
- end
59
-
60
- def finder_name(klass)
61
- klass.name.underscore.gsub('/', '_').sub(/^(\w)_/, '\1').gsub(/_(\w)_/, '_\1')
62
- end
63
27
  end
64
28
 
65
29
  end
@@ -5,8 +5,8 @@ module Dataset
5
5
  # raised.
6
6
  #
7
7
  class RecordNotFound < StandardError
8
- def initialize(record_type, symbolic_name)
9
- super "There is no '#{record_type.name}' found for the symbolic name ':#{symbolic_name}'."
8
+ def initialize(record_heirarchy, symbolic_name)
9
+ super "There is no '#{record_heirarchy.base_class.name}' found for the symbolic name ':#{symbolic_name}'."
10
10
  end
11
11
  end
12
12
 
@@ -41,12 +41,12 @@ module Dataset
41
41
  # people(:bobby) OR users(:bobby)
42
42
  #
43
43
  module ModelFinders
44
- def create_finder(record_meta) # :nodoc:
44
+ def create_finders(record_meta) # :nodoc:
45
45
  @finders_generated ||= []
46
+ heirarchy_finders_hash = record_meta.heirarchy.model_finder_names.join('').hash
47
+ return if @finders_generated.include?(heirarchy_finders_hash)
46
48
 
47
- return if @finders_generated.include?(record_meta)
48
-
49
- record_meta.model_finder_names.each do |finder_name|
49
+ record_meta.heirarchy.model_finder_names.each do |finder_name|
50
50
  unless instance_methods.include?(finder_name)
51
51
  define_method finder_name do |*symbolic_names|
52
52
  names = Array(symbolic_names)
@@ -58,7 +58,7 @@ module Dataset
58
58
  end
59
59
  end
60
60
 
61
- record_meta.id_finder_names.each do |finder_name|
61
+ record_meta.heirarchy.id_finder_names.each do |finder_name|
62
62
  unless instance_methods.include?(finder_name)
63
63
  define_method finder_name do |*symbolic_names|
64
64
  names = Array(symbolic_names)
@@ -70,7 +70,7 @@ module Dataset
70
70
  end
71
71
  end
72
72
 
73
- @finders_generated << record_meta
73
+ @finders_generated << heirarchy_finders_hash
74
74
  end
75
75
  end
76
76
 
@@ -207,23 +207,25 @@ module Dataset
207
207
 
208
208
  def find_id(record_type_or_meta, symbolic_name)
209
209
  record_meta = record_meta_for_type(record_type_or_meta)
210
- if local_id = @id_cache[record_meta.id_cache_key][symbolic_name]
210
+ heirarchy = record_meta.heirarchy
211
+ if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
211
212
  local_id
212
213
  elsif !parent_binding.nil?
213
214
  parent_binding.find_id record_meta, symbolic_name
214
215
  else
215
- raise RecordNotFound.new(record_meta, symbolic_name)
216
+ raise RecordNotFound.new(heirarchy, symbolic_name)
216
217
  end
217
218
  end
218
219
 
219
220
  def find_model(record_type_or_meta, symbolic_name)
220
221
  record_meta = record_meta_for_type(record_type_or_meta)
221
- if local_id = @id_cache[record_meta.id_cache_key][symbolic_name]
222
- record_meta.record_class.find local_id
222
+ heirarchy = record_meta.heirarchy
223
+ if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
224
+ heirarchy.base_class.find local_id
223
225
  elsif !parent_binding.nil?
224
226
  parent_binding.find_model record_meta, symbolic_name
225
227
  else
226
- raise RecordNotFound.new(record_meta, symbolic_name)
228
+ raise RecordNotFound.new(heirarchy, symbolic_name)
227
229
  end
228
230
  end
229
231
 
@@ -235,8 +237,8 @@ module Dataset
235
237
 
236
238
  def name_model(record, symbolic_name)
237
239
  record_meta = database.record_meta(record.class)
238
- @model_finders.create_finder(record_meta)
239
- @id_cache[record_meta.id_cache_key][symbolic_name] = record.id
240
+ @model_finders.create_finders(record_meta)
241
+ @id_cache[record_meta.heirarchy.id_cache_key][symbolic_name] = record.id
240
242
  record
241
243
  end
242
244
 
@@ -258,10 +260,10 @@ module Dataset
258
260
  record = dataset_record_class.new(record_meta, attributes, symbolic_name, self)
259
261
  return_value = nil
260
262
 
261
- @model_finders.create_finder(record_meta)
263
+ @model_finders.create_finders(record_meta)
262
264
  ActiveRecord::Base.silence do
263
265
  return_value = record.create
264
- @id_cache[record_meta.id_cache_key][symbolic_name] = record.id
266
+ @id_cache[record_meta.heirarchy.id_cache_key][symbolic_name] = record.id
265
267
  end
266
268
  return_value
267
269
  end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ class MThingy
4
+ class NThingy
5
+ end
6
+ end
7
+
8
+ describe Dataset::Record::Heirarchy, 'finder name' do
9
+ it 'should collapse single character followed by underscore to just the single character' do
10
+ @heirarchy = Dataset::Record::Heirarchy.new(Place)
11
+ @heirarchy.finder_name(MThingy).should == 'mthingy'
12
+ @heirarchy.finder_name(MThingy::NThingy).should == 'mthingy_nthingy'
13
+ end
14
+ end
@@ -142,18 +142,23 @@ describe Dataset::SessionBinding do
142
142
 
143
143
  describe 'name_model' do
144
144
  before do
145
+ @place = Place.create!
146
+ @binding.name_model(@place, :myplace)
145
147
  @state = State.create!(:name => 'NC')
146
148
  @binding.name_model(@state, :mystate)
147
149
  end
148
150
 
149
151
  it 'should allow assigning a name to a model for later lookup' do
152
+ @binding.find_model(Place, :myplace).should == @place
150
153
  @binding.find_model(State, :mystate).should == @state
151
154
  end
152
155
 
153
156
  it 'should allow finding STI' do
154
157
  @context = Object.new
155
158
  @context.extend @binding.model_finders
159
+ @context.places(:myplace).should == @place
156
160
  @context.places(:mystate).should == @state
161
+ @context.states(:mystate).should == @state
157
162
  end
158
163
  end
159
164
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dataset
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Williams
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-22 00:00:00 -05:00
12
+ date: 2009-12-29 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -62,6 +62,7 @@ files:
62
62
  - lib/dataset/instance_methods.rb
63
63
  - lib/dataset/load.rb
64
64
  - lib/dataset/record/fixture.rb
65
+ - lib/dataset/record/heirarchy.rb
65
66
  - lib/dataset/record/meta.rb
66
67
  - lib/dataset/record/model.rb
67
68
  - lib/dataset/resolver.rb
@@ -102,7 +103,7 @@ summary: A simple API for creating and finding sets of data in your database, bu
102
103
  test_files:
103
104
  - spec/dataset/cucumber_spec.rb
104
105
  - spec/dataset/database/base_spec.rb
105
- - spec/dataset/record/meta_spec.rb
106
+ - spec/dataset/record/heirarchy_spec.rb
106
107
  - spec/dataset/resolver_spec.rb
107
108
  - spec/dataset/rspec_spec.rb
108
109
  - spec/dataset/session_binding_spec.rb
@@ -1,14 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- class MThingy
4
- class NThingy
5
- end
6
- end
7
-
8
- describe Dataset::Record::Meta, 'finder name' do
9
- it 'should collapse single character followed by underscore to just the single character' do
10
- @meta = Dataset::Record::Meta.new(Place)
11
- @meta.finder_name(MThingy).should == 'mthingy'
12
- @meta.finder_name(MThingy::NThingy).should == 'mthingy_nthingy'
13
- end
14
- end