ardb 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,11 +20,19 @@ module Ardb
20
20
 
21
21
  attr_reader :associations, :callbacks, :validations
22
22
 
23
- [ :belongs_to, :has_many, :has_one ].each do |method_name|
23
+ [ :belongs_to, :has_one, :has_many ].each do |method_name|
24
24
 
25
- define_method(method_name) do |*args|
25
+ define_method(method_name) do |assoc_name, *args|
26
26
  @associations ||= []
27
- @associations << Association.new(method_name, *args)
27
+
28
+ define_method(assoc_name) do
29
+ instance_variable_get("@#{assoc_name}") || (method_name == :has_many ? [] : nil)
30
+ end
31
+ define_method("#{assoc_name}=") do |value|
32
+ instance_variable_set("@#{assoc_name}", value)
33
+ end
34
+
35
+ @associations << Association.new(method_name, assoc_name, *args)
28
36
  end
29
37
 
30
38
  end
@@ -83,6 +91,8 @@ module Ardb
83
91
 
84
92
  module InstanceMethods
85
93
 
94
+ attr_accessor :id
95
+
86
96
  def update_column(col, value)
87
97
  self.send("#{col}=", value)
88
98
  end
@@ -92,10 +102,10 @@ module Ardb
92
102
  class Association
93
103
  attr_reader :type, :name, :options
94
104
 
95
- def initialize(type, name, options)
105
+ def initialize(type, name, options = nil)
96
106
  @type = type.to_sym
97
107
  @name = name
98
- @options = options
108
+ @options = options || {}
99
109
  end
100
110
  end
101
111
 
data/lib/ardb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -147,6 +147,7 @@ module Ardb::RecordSpy
147
147
  class InstanceTests < UnitTests
148
148
  subject{ @instance }
149
149
 
150
+ should have_accessors :id
150
151
  should have_imeths :update_column
151
152
 
152
153
  should "allow spying the update_column method by just writing the value" do
@@ -156,11 +157,29 @@ module Ardb::RecordSpy
156
157
  assert_equal 'updated', subject.name
157
158
  end
158
159
 
160
+ should "have accessors for each association defined" do
161
+ assert_nil subject.bt_thing
162
+ subject.bt_thing = 'something'
163
+ assert_equal 'something', subject.bt_thing
164
+
165
+ assert_nil subject.ho_thing
166
+ subject.ho_thing = 'other thing'
167
+ assert_equal 'other thing', subject.ho_thing
168
+
169
+ assert_empty subject.hm_things
170
+ subject.hm_things = [1,2,3]
171
+ assert_equal [1,2,3], subject.hm_things
172
+ end
173
+
159
174
  end
160
175
 
161
176
  class MyRecord
162
177
  include Ardb::RecordSpy
163
178
  attr_accessor :name
179
+
180
+ belongs_to :bt_thing
181
+ has_one :ho_thing
182
+ has_many :hm_things
164
183
  end
165
184
 
166
185
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ardb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 47
4
+ hash: 43
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 12
8
+ - 13
9
9
  - 0
10
- version: 0.12.0
10
+ version: 0.13.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-09-26 00:00:00 Z
19
+ date: 2013-09-27 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: assert