passiverecord 0.0.2 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.1. Different subclasses of PassiveRecord can now use the same keys safely.
2
+
1
3
  v0.0.2. Switching to Echoe for gem packaging and removing the end-user dependency for Hoe
2
4
 
3
5
  v0.0.1. Initial release
data/Manifest CHANGED
@@ -8,3 +8,7 @@ lib/passive_record/schema.rb
8
8
  test/test_associations.rb
9
9
  test/test_base.rb
10
10
  test/test_helper.rb
11
+ test/fixtures/continents.rb
12
+ test/fixtures/doors.yml
13
+ test/fixtures/furniture.rb
14
+ test/fixtures/furniture.yml
data/README CHANGED
@@ -5,7 +5,7 @@ by Jason L Perry (http://paint.itred.org)
5
5
 
6
6
  A replacement for ActiveRecord when you just need to model
7
7
  a few unchanging objects, and don't necessarily need a full blown
8
- ActiveRecord class and table in the database.
8
+ ActiveRecord class and table in the database. Use with reservation.
9
9
 
10
10
  == FEATURES:
11
11
 
@@ -34,10 +34,10 @@ module PassiveRecord
34
34
 
35
35
  def create(*args)
36
36
  attributes = extract_options!(args)
37
- key = args.first || @@instances.size+1
37
+ key = args.first || find_every.size+1
38
38
  instance = self.new(attributes)
39
39
  instance.key = key
40
- @@instances[key] = instance
40
+ @@instances[base_key(key)] = instance
41
41
  return key
42
42
  end
43
43
 
@@ -51,6 +51,10 @@ module PassiveRecord
51
51
  def count
52
52
  find_every.size
53
53
  end
54
+
55
+ def base_class
56
+ class_of_passive_record_descendant(self)
57
+ end
54
58
 
55
59
  protected
56
60
 
@@ -79,7 +83,7 @@ module PassiveRecord
79
83
  end
80
84
 
81
85
  def find_one(_key)
82
- values = @@instances.select { |key, value| _key == key && value.is_a?(self) }.map {|its| its.last}
86
+ values = @@instances.select { |key, value| base_key(_key) == key && value.is_a?(self) }.map {|its| its.last}
83
87
  unless values.empty?
84
88
  return values.first
85
89
  else
@@ -88,7 +92,8 @@ module PassiveRecord
88
92
  end
89
93
 
90
94
  def find_some(keys)
91
- values = @@instances.select { |key, value| (keys).include?(key) && value.is_a?(self) }.map {|its| its.last}
95
+ base_keys = keys.map { |key| base_key(key) }
96
+ values = @@instances.select { |key, value| (base_keys).include?(key) && value.is_a?(self) }.map {|its| its.last }
92
97
  end
93
98
 
94
99
  def extract_options!(args) #:nodoc:
@@ -103,7 +108,7 @@ module PassiveRecord
103
108
  # super unless all_attributes_exists?(attribute_names)
104
109
  attributes = {}
105
110
  attribute_names.each_with_index { |name, idx| attributes[name] = arguments[idx] }
106
- expressions = []
111
+ expressions = ["value.is_a?(self)"]
107
112
  attributes.each_pair { |key, value| expressions << attribute_expression("value.#{key}", value) }
108
113
  results = @@instances.select { |key, value| eval expressions.join(" && ") }.map {|its| its.last}
109
114
  return expects_array ? results : results.pop
@@ -121,6 +126,28 @@ module PassiveRecord
121
126
  end
122
127
  end
123
128
 
129
+ # Returns the class descending directly from PassiveRecord in the inheritance hierarchy.
130
+ def base_class(klass = self)
131
+ if klass.superclass == Base
132
+ klass
133
+ elsif klass.superclass.nil?
134
+ raise PassiveRecordError, "#{name} doesn't belong in a hierarchy descending from PassiveRecord"
135
+ else
136
+ base_class(klass.superclass)
137
+ end
138
+ end
139
+
140
+ # Returns the real hash key used to store instances.
141
+ # Keys are name spaced with their class name, ie "Continent:1"
142
+ def base_key(key)
143
+ "#{base_class.name}:#{key}"
144
+ end
145
+
146
+ # Returns the key portion of a +base_key+
147
+ def key_base(base_key)
148
+ base_key.split(":").last
149
+ end
150
+
124
151
  end
125
152
  end
126
153
  end
@@ -1,11 +1,11 @@
1
1
 
2
- # Gem::Specification for Passiverecord-0.0.2
2
+ # Gem::Specification for Passiverecord-0.1
3
3
  # Originally generated by Echoe
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{passiverecord}
7
- s.version = "0.0.2"
8
- s.date = %q{2007-09-28}
7
+ s.version = "0.1"
8
+ s.date = %q{2007-10-03}
9
9
  s.summary = %q{Pacifying overactive records}
10
10
  s.email = %q{jasper@ambethia.com}
11
11
  s.homepage = %q{http://code.itred.org/projects/passive-record}
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.description = %q{Pacifying overactive records}
14
14
  s.has_rdoc = true
15
15
  s.authors = ["Jason L Perry"]
16
- s.files = ["CHANGELOG", "Manifest", "README", "lib/passive_record.rb", "lib/passive_record/associations.rb", "lib/passive_record/base.rb", "lib/passive_record/schema.rb", "test/test_associations.rb", "test/test_base.rb", "test/test_helper.rb", "passiverecord.gemspec"]
16
+ s.files = ["CHANGELOG", "Manifest", "README", "lib/passive_record.rb", "lib/passive_record/associations.rb", "lib/passive_record/base.rb", "lib/passive_record/schema.rb", "test/test_associations.rb", "test/test_base.rb", "test/test_helper.rb", "test/fixtures/continents.rb", "test/fixtures/doors.yml", "test/fixtures/furniture.rb", "test/fixtures/furniture.yml", "passiverecord.gemspec"]
17
17
  s.test_files = ["test/test_associations.rb", "test/test_base.rb", "test/test_helper.rb"]
18
18
  s.add_dependency(%q<activerecord>, [">= 0", "= 1.15.3"])
19
19
  end
@@ -0,0 +1,10 @@
1
+ class Continent < PassiveRecord::Base
2
+ schema :name => String, :size => Integer, :population => Integer
3
+
4
+ create :name => "Africa", :size => 30370000, :population => 890000000
5
+ create :name => "Antarctica", :size => 13720000, :population => 1000
6
+ create :name => "Australia", :size => 7600000, :population => 20000000
7
+ create :name => "Eurasia", :size => 53990000, :population => 4510000000
8
+ create :name => "North America", :size => 24490000, :population => 515000000
9
+ create :name => "South America", :size => 17840000, :population => 371000000
10
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ office:
3
+ id: 1
4
+ inside_id: 5
5
+ outside_id: 1
6
+ restroom:
7
+ id: 2
8
+ inside_id: 4
9
+ outside_id: 1
@@ -0,0 +1,41 @@
1
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
2
+
3
+ ActiveRecord::Base.connection.create_table "furniture", :force => true do |t|
4
+ t.column :name, :string
5
+ t.column :room_id, :integer
6
+ end
7
+
8
+ ActiveRecord::Base.connection.create_table "doors", :force => true do |t|
9
+ t.column :inside_id, :integer
10
+ t.column :outside_id, :integer
11
+ end
12
+
13
+ Inflector.inflections { |inflect| inflect.uncountable %w( furniture )}
14
+
15
+ class Furniture < ActiveRecord::Base
16
+ belongs_to :room
17
+ end
18
+
19
+ class Door < ActiveRecord::Base
20
+ belongs_to :inside, :class_name => "Room", :foreign_key => "inside_id"
21
+ belongs_to :outside, :class_name => "Room", :foreign_key => "outside_id"
22
+ end
23
+
24
+ class Room < PassiveRecord::Base
25
+ has_many :furniture, :order => :name
26
+ has_one :light_fixture, :class_name => "Furniture"
27
+
28
+ has_many :ins, :class_name => "Door", :foreign_key => "outside_id"
29
+ has_many :outs, :class_name => "Door", :foreign_key => "inside_id"
30
+
31
+ has_many :exits, :through => :outs
32
+ has_many :entrances, :through => :ins
33
+
34
+ schema :name => String
35
+
36
+ create 1, :name => "Family Room"
37
+ create 2, :name => "Kitchen"
38
+ create 3, :name => "Bedroom"
39
+ create 4, :name => "Restroom"
40
+ create 5, :name => "Office"
41
+ end
@@ -0,0 +1,13 @@
1
+ ---
2
+ couch:
3
+ id: 1
4
+ name: Couch
5
+ room_id: 1
6
+ ottoman:
7
+ id: 2
8
+ name: Ottoman
9
+ room_id: 1
10
+ ottlite:
11
+ id: 3
12
+ name: Ott-lite
13
+ room_id: 5
@@ -1,59 +1,9 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class Room < PassiveRecord::Base
4
- has_many :furniture, :order => :name
5
- has_one :light_fixture, :class_name => "Furniture"
6
-
7
- has_many :ins, :class_name => "Door", :foreign_key => "outside_id"
8
- has_many :outs, :class_name => "Door", :foreign_key => "inside_id"
9
-
10
- has_many :exits, :through => :outs
11
- has_many :entrances, :through => :ins
12
-
13
- schema :name => String
14
-
15
- create :name => "Family Room"
16
- create :name => "Kitchen"
17
- create :name => "Bedroom"
18
- create :name => "Restroom"
19
- create :name => "Office"
20
- end
21
-
22
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
23
- ActiveRecord::Base.connection.create_table "furniture", :force => true do |t|
24
- t.column :name, :string
25
- t.column :room_id, :integer
26
- end
27
-
28
- ActiveRecord::Base.connection.create_table "doors", :force => true do |t|
29
- t.column :inside_id, :integer
30
- t.column :outside_id, :integer
31
- end
32
-
33
- Inflector.inflections { |inflect| inflect.uncountable %w( furniture )}
34
-
35
- class Furniture < ActiveRecord::Base
36
- belongs_to :room
37
- end
38
-
39
- class Door < ActiveRecord::Base
40
- belongs_to :inside, :class_name => "Room", :foreign_key => "inside_id"
41
- belongs_to :outside, :class_name => "Room", :foreign_key => "outside_id"
42
- end
43
-
44
3
  class PassiveRecord::AssociationsTest < Test::Unit::TestCase
45
-
46
- # some "fixtures"
4
+
47
5
  def setup
48
- Furniture.create :name => "Couch", :room_id => Room.find_by_name("Family Room").id
49
- Furniture.create :name => "Ottoman", :room_id => Room.find_by_name("Family Room").id
50
- Furniture.create :name => "Ott-lite", :room_id => Room.find_by_name("Office").id
51
-
52
- Door.create :inside_id => Room.find_by_name("Office").id,
53
- :outside_id => Room.find_by_name("Family Room").id
54
-
55
- Door.create :inside_id => Room.find_by_name("Restroom").id,
56
- :outside_id => Room.find_by_name("Family Room").id
6
+ create_fixtures :doors, :furniture
57
7
  end
58
8
 
59
9
  def test_should_have_many
@@ -87,10 +37,5 @@ class PassiveRecord::AssociationsTest < Test::Unit::TestCase
87
37
  room = Room.find_by_name("Office")
88
38
 
89
39
  assert_equal room, lamp.room
90
- end
91
-
92
- def teardown
93
- Furniture.delete_all
94
- end
95
-
40
+ end
96
41
  end
@@ -1,28 +1,16 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class Continent < PassiveRecord::Base
4
- schema :name => String, :size => Integer, :population => Integer
5
- end
6
-
7
3
  class PassiveRecord::BaseTest < Test::Unit::TestCase
8
4
 
9
- def setup
10
- # Add the geographic 6 continents
11
- Continent.send :create, :name => "Africa", :size => 30370000, :population => 890000000
12
- Continent.send :create, :name => "Antarctica", :size => 13720000, :population => 1000
13
- Continent.send :create, :name => "Australia", :size => 7600000, :population => 20000000
14
- Continent.send :create, :name => "Eurasia", :size => 53990000, :population => 4510000000
15
- Continent.send :create, :name => "North America", :size => 24490000, :population => 515000000
16
- Continent.send :create, :name => "South America", :size => 17840000, :population => 371000000
17
- end
18
-
19
5
  def test_should_create_instance
20
6
  assert_equal 7, Continent.send(:create, :name => "Atlantis", :size => 0, :population => 0)
7
+ PassiveRecord::Base.send(:class_variable_get, "@@instances").delete("Continent:7")
21
8
  end
22
9
 
23
10
  def test_should_create_instance_with_manual_key
24
11
  assert_equal "ATL", Continent.send(:create, "ATL", :name => "Atlantis", :size => 0, :population => 0)
25
12
  assert Continent.find("ATL")
13
+ PassiveRecord::Base.send(:class_variable_get, "@@instances").delete("Continent:ATL")
26
14
  end
27
15
 
28
16
  def test_should_count_instances
@@ -77,11 +65,4 @@ class PassiveRecord::BaseTest < Test::Unit::TestCase
77
65
  assert_equal Continent.find(6), Continent.find_by_name_and_size(/America/, 17840000)
78
66
  end
79
67
 
80
- def test_should_puts_some_stuff
81
- end
82
-
83
- def teardown
84
- Continent.send :class_variable_set, "@@instances", {}
85
- end
86
-
87
68
  end
@@ -1,2 +1,31 @@
1
1
  require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'active_record/fixtures'
5
+ require 'active_support'
6
+ require 'active_support/breakpoint'
7
+
2
8
  require File.dirname(__FILE__) + '/../lib/passive_record'
9
+
10
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
11
+
12
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
13
+
14
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
15
+
16
+ require 'continents'
17
+ require 'furniture'
18
+
19
+ class Test::Unit::TestCase #:nodoc:
20
+
21
+ def create_fixtures(*table_names)
22
+ if block_given?
23
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
24
+ else
25
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
26
+ end
27
+ end
28
+
29
+ self.use_transactional_fixtures = true
30
+ self.use_instantiated_fixtures = false
31
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: passiverecord
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-09-28 00:00:00 -04:00
6
+ version: "0.1"
7
+ date: 2007-10-03 00:00:00 -04:00
8
8
  summary: Pacifying overactive records
9
9
  require_paths:
10
10
  - lib
@@ -39,6 +39,10 @@ files:
39
39
  - test/test_associations.rb
40
40
  - test/test_base.rb
41
41
  - test/test_helper.rb
42
+ - test/fixtures/continents.rb
43
+ - test/fixtures/doors.yml
44
+ - test/fixtures/furniture.rb
45
+ - test/fixtures/furniture.yml
42
46
  - passiverecord.gemspec
43
47
  test_files:
44
48
  - test/test_associations.rb