tableless_model 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -8,7 +8,12 @@ GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
10
  ansi (1.2.2)
11
+ ffi (1.0.4)
12
+ rake (>= 0.8.7)
11
13
  minitest (2.0.2)
14
+ rake (0.8.7)
15
+ sqlite3 (0.1.1)
16
+ ffi (>= 0.6.3)
12
17
  validatable (1.6.7)
13
18
 
14
19
  PLATFORMS
@@ -17,5 +22,6 @@ PLATFORMS
17
22
  DEPENDENCIES
18
23
  ansi
19
24
  minitest
25
+ sqlite3
20
26
  tableless_model!
21
27
  validatable
data/README.rdoc CHANGED
@@ -16,7 +16,7 @@ Tableless Model is available as a Rubygem:
16
16
 
17
17
  gem install tableless_model
18
18
 
19
- (current version: 0.0.4)
19
+ (current version: 0.0.5)
20
20
 
21
21
 
22
22
  == Usage
@@ -27,20 +27,15 @@ module Base
27
27
 
28
28
  # if only the column name is given, the tableless model's class is expected to have that name, classified, as class name
29
29
  class_type = column.class == Hash ? column.collect{|k,v| v}.last : column.to_s.classify.constantize
30
-
31
-
30
+
32
31
  # injecting in the parent object a getter and a setter for the
33
32
  # attribute that will store an instance of a tableless model
34
33
  class_eval do
35
-
36
- # Making sure the serialized column contains a new instance of the tableless model
37
- # if it hasn't been set yet
38
- default_value_for column_name, class_type.new
39
-
34
+
40
35
  # Telling AR that the column has to store an instance of the given tableless model in
41
36
  # YAML serialized format
42
37
  serialize column_name, ActiveRecord::TablelessModel
43
-
38
+
44
39
  # Adding getter for the serialized column,
45
40
  # making sure it always returns an instance of the specified tableless
46
41
  # model and not just a normal hash or the value of the attribute in the database,
@@ -48,7 +43,7 @@ module Base
48
43
  define_method column_name.to_s do
49
44
  class_type.new(read_attribute(column_name.to_sym) || {})
50
45
  end
51
-
46
+
52
47
  # Adding setter for the serialized column,
53
48
  # making sure it always stores in it an instance of
54
49
  # the specified tableless model (as the argument may also be a regular hash)
@@ -57,35 +52,6 @@ module Base
57
52
  end
58
53
  end
59
54
  end
60
-
61
-
62
-
63
- #
64
- #
65
- # Overriding the setter for the serialized column in the AR model,
66
- # to make sure that, if it is still nil, the column always
67
- # returns at least a new instance of the specified tableless model
68
- # having the default values, if any, declared in the tableless model itself
69
- #
70
- def default_value_for(property, default_value)
71
- unless method_defined? "after_initialize_with_default_value_for_#{property.to_s}"
72
-
73
- unless method_defined? "after_initialize"
74
- define_method "after_initialize" do |*args|
75
- end
76
- end
77
-
78
- define_method "after_initialize_with_default_value_for_#{property.to_s}" do |*args|
79
- send("after_initialize_without_default_value_for_#{property.to_s}", *args)
80
- return unless new_record?
81
- return unless self.respond_to?(property.to_sym)
82
-
83
- self.send("#{property.to_s}=".to_sym, self.send(property.to_sym) || default_value)
84
- end
85
-
86
- alias_method_chain "after_initialize", "default_value_for_#{property.to_s}"
87
- end
88
- end
89
55
 
90
56
  end
91
57
  end
@@ -1,3 +1,3 @@
1
1
  module TablelessModel
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -148,13 +148,56 @@ describe "An ActiveRecord::Base model" do
148
148
  attribute :bbb, :default => "bbb"
149
149
  end
150
150
 
151
- class Model < ActiveRecord::Base
152
- # has_tableless :options => ModelOptions
151
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
152
+ ActiveRecord::Base.connection.execute(" create table test_models (options varchar(50)) ")
153
+
154
+ class TestModel < ActiveRecord::Base
155
+ has_tableless :options => ModelOptions
153
156
  end
154
157
  end
155
158
 
156
159
  it "responds to default_value_for, has_tableless" do
157
- [:default_value_for, :has_tableless].each {|method| Model.must_respond_to(method)}
160
+ TestModel.must_respond_to(:has_tableless)
161
+ end
162
+
163
+ describe "new instance" do
164
+ before do
165
+ @instance = TestModel.new
166
+ end
167
+
168
+ it "has a getter and a setter for :options" do
169
+ %w(options options=).each{|m| @instance.must_respond_to m }
170
+ @instance.options.must_be_kind_of ModelOptions
171
+ @instance.options.wont_be_nil
172
+ @instance.options.aaa.must_equal "111"
173
+ @instance.options.bbb.must_equal "bbb"
174
+ proc { @instance.options = "test" }.must_raise NoMethodError, "should not accept other values than a hash or ModelOptions instance"
175
+ end
176
+
177
+ describe "setter" do
178
+ before do
179
+ @return_value = @instance.send("options=", { :aaa => "CCC", :bbb => "DDD" })
180
+ end
181
+
182
+ it "correctly sets the serialized column" do
183
+ @return_value.must_be_kind_of ModelOptions
184
+ %w(aaa bbb).each{|m| @return_value.must_respond_to m}
185
+ @instance.options.aaa.must_equal "CCC"
186
+ @instance.options.bbb.must_equal "DDD"
187
+ end
188
+
189
+ it "should save the serialized column correctly" do
190
+ @instance.save!
191
+ @instance = TestModel.first
192
+
193
+ @instance.options.aaa.must_equal "CCC"
194
+ @instance.options.bbb.must_equal "DDD"
195
+
196
+ # Ensuring the serialize macro is used
197
+ @instance.options.must_be_kind_of ModelOptions
198
+
199
+ end
200
+ end
158
201
  end
159
202
 
160
203
  end
data/spec/test_helper.rb CHANGED
@@ -75,7 +75,8 @@ class MiniTest::Unit
75
75
  end)
76
76
 
77
77
 
78
- @@out.print " #{test.humanize.gsub(/Test\s\d+\s(.*)/,"\\1")} "
78
+ # @@out.print " #{test.humanize.gsub(/Test\s\d+\s(.*)/,"\\1")} "
79
+ @@out.print " #{test} "
79
80
  @@out.print " (%.2fs) " % (Time.now - t)
80
81
 
81
82
  if @broken
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.add_dependency "validatable"
17
17
  s.add_development_dependency "minitest"
18
18
  s.add_development_dependency "ansi"
19
+ s.add_development_dependency "sqlite3"
19
20
 
20
21
  s.rubyforge_project = "tableless_model"
21
22
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Vito Botta
@@ -56,6 +56,19 @@ dependencies:
56
56
  version: "0"
57
57
  type: :development
58
58
  version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: sqlite3
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
59
72
  description: A serialisable and validatable table-less model with support for associations, useful to store settings, options, etc in a serialized form in a parent object
60
73
  email:
61
74
  - vito@botta.name