ninjudd-active_document 0.0.3 → 0.0.4

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/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 3
3
+ :patch: 4
4
4
  :major: 0
@@ -3,7 +3,7 @@ class ActiveDocument::Base
3
3
  if path
4
4
  @path = path
5
5
  else
6
- @path || (base_class? ? ActiveDocument::DEFAULT_PATH : super)
6
+ @path ||= (self == ActiveDocument::Base ? ActiveDocument::DEFAULT_PATH : ActiveDocument::Base.path)
7
7
  end
8
8
  end
9
9
 
@@ -12,20 +12,8 @@ class ActiveDocument::Base
12
12
  raise 'cannot modify database_name after db has been initialized' if @database_name
13
13
  @database_name = database_name
14
14
  else
15
- return nil if self == ActiveDocument::Base
16
- @database_name ||= base_class? ? name.underscore.gsub('/', '-').pluralize : super
17
- end
18
- end
19
-
20
- def self.base_class?
21
- self == base_class
22
- end
23
-
24
- def self.base_class(klass = self)
25
- if klass == ActiveDocument::Base or klass.superclass == ActiveDocument::Base
26
- klass
27
- else
28
- base_class(klass.superclass)
15
+ return if self == ActiveDocument::Base
16
+ @database_name ||= name.underscore.gsub('/', '-').pluralize
29
17
  end
30
18
  end
31
19
 
@@ -49,6 +37,8 @@ class ActiveDocument::Base
49
37
  end
50
38
 
51
39
  def self.primary_key(field_or_fields)
40
+ databases[:primary_key] = ActiveDocument::Database.new(:model_class => self, :unique => true)
41
+
52
42
  field = define_field_accessor(field_or_fields)
53
43
  define_find_methods(field, :field => :primary_key) # find_by_field1_and_field2
54
44
 
@@ -74,7 +64,7 @@ class ActiveDocument::Base
74
64
  end
75
65
 
76
66
  def self.databases
77
- @databases ||= { :primary_key => ActiveDocument::Database.new(:model_class => self, :unique => true) }
67
+ @databases ||= {}
78
68
  end
79
69
 
80
70
  def self.open_database
@@ -99,9 +89,8 @@ class ActiveDocument::Base
99
89
  open_database # Make sure the database is open.
100
90
  field ||= :primary_key
101
91
  field = field.to_sym
102
- database = databases[field]
103
- database ||= base_class.database(field) unless base_class?
104
- database
92
+ return if self == ActiveDocument::Base
93
+ databases[field] ||= super
105
94
  end
106
95
 
107
96
  def database(field = nil)
@@ -187,10 +176,12 @@ class ActiveDocument::Base
187
176
 
188
177
  def initialize(attributes = {})
189
178
  if attributes.kind_of?(String)
190
- @attributes, @saved_attributes = Marshal.load(attributes)
179
+ @attributes, @saved_attributes = Marshal.load(attributes)
191
180
  else
192
181
  @attributes = attributes
193
182
  end
183
+ @attributes = HashWithIndifferentAccess.new(@attributes) if @attributes
184
+ @saved_attributes = HashWithIndifferentAccess.new(@saved_attributes) if @saved_attributes
194
185
  end
195
186
 
196
187
  attr_reader :saved_attributes
@@ -199,6 +190,18 @@ class ActiveDocument::Base
199
190
  @attributes ||= Marshal.load(Marshal.dump(saved_attributes))
200
191
  end
201
192
 
193
+ def to_json(*fields)
194
+ if fields.empty?
195
+ attributes.to_json
196
+ else
197
+ slice = {}
198
+ fields.each do |field|
199
+ slice[field] = attributes[field]
200
+ end
201
+ slice.to_json
202
+ end
203
+ end
204
+
202
205
  def ==(other)
203
206
  return false if other.nil?
204
207
  attributes == other.attributes
@@ -246,7 +249,9 @@ class ActiveDocument::Base
246
249
  end
247
250
 
248
251
  def _dump(ignored)
249
- Marshal.dump([@attributes, @saved_attributes])
252
+ attributes = @attributes.to_hash if @attributes
253
+ saved_attributes = @saved_attributes.to_hash if @saved_attributes
254
+ Marshal.dump([attributes, saved_attributes])
250
255
  end
251
256
 
252
257
  def self._load(data)
@@ -113,7 +113,11 @@ class ActiveDocument::Database
113
113
  flags = opts[:create] ? Bdb::DB_NOOVERWRITE : 0
114
114
  db.put(transaction, key, data, flags)
115
115
  rescue Bdb::DbError => e
116
- raise ActiveDocument::DuplicatePrimaryKey, "primary key #{model.primary_key.inspect} already exists"
116
+ if e.message =~ /DB_KEYEXIST/
117
+ raise ActiveDocument::DuplicatePrimaryKey, "primary key #{model.primary_key.inspect} already exists"
118
+ else
119
+ raise e
120
+ end
117
121
  end
118
122
 
119
123
  def delete(model)
@@ -121,6 +125,11 @@ class ActiveDocument::Database
121
125
  db.del(transaction, key, 0)
122
126
  end
123
127
 
128
+ def truncate
129
+ # Delete all records in the database. Beware!
130
+ db.truncate(transaction)
131
+ end
132
+
124
133
  def open
125
134
  if @db.nil?
126
135
  @db = environment.db
@@ -7,6 +7,7 @@ end
7
7
  require 'bdb'
8
8
  require 'tuple'
9
9
  require 'active_support/inflector'
10
+ require 'active_support/core_ext/hash/indifferent_access.rb'
10
11
  require 'active_document/database'
11
12
  require 'active_document/environment'
12
13
  require 'active_document/base'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjudd-active_document
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Balthrop
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-24 00:00:00 -07:00
12
+ date: 2009-08-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,7 +33,6 @@ files:
33
33
  - test/test_helper.rb
34
34
  has_rdoc: true
35
35
  homepage: http://github.com/ninjudd/active_document
36
- licenses:
37
36
  post_install_message:
38
37
  rdoc_options:
39
38
  - --inline-source
@@ -55,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
54
  requirements: []
56
55
 
57
56
  rubyforge_project:
58
- rubygems_version: 1.3.5
57
+ rubygems_version: 1.2.0
59
58
  signing_key:
60
59
  specification_version: 2
61
60
  summary: TODO