static_model 0.1.0

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/test/books.yml ADDED
@@ -0,0 +1,18 @@
1
+ ---
2
+ - id: 1
3
+ title: The Omnivore's Dilemma
4
+ author: Michael Pollan
5
+ genre: Non-Fiction
6
+ - id: 2
7
+ title: In Defense of Food
8
+ author: Michael Pollan
9
+ genre: Non-Fiction
10
+ - id: 3
11
+ title: Omnivore (Of Man and Manta)
12
+ author: Piers Anthony
13
+ genre: Fantasy
14
+ - id: 4
15
+ title: Choke
16
+ author: Chuck Palahniuk
17
+ genre: Humor
18
+
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'shoulda'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/static_model'
6
+
7
+ class Book < StaticModel::Base
8
+ set_data_file File.join(File.dirname(__FILE__), 'books.yml')
9
+ end
@@ -0,0 +1,214 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestStaticModel < Test::Unit::TestCase
4
+
5
+ context "StaticModel" do
6
+ context "A class that inherits from Base" do
7
+ context "an instance" do
8
+
9
+ setup do
10
+ @book = Book.new(book_params)
11
+ end
12
+
13
+ context "initialization" do
14
+
15
+ should "set attributes with a hash" do
16
+ assert @book.id
17
+ end
18
+
19
+ should "raise error if passed anything but hash" do
20
+ assert_raise(StaticModel::BadOptions) do
21
+ Book.new("im bad")
22
+ end
23
+ end
24
+ end
25
+
26
+ context "attributes" do
27
+ should "get attributes by methods" do
28
+ assert_equal book_params[:title], @book.title
29
+ assert_equal book_params[:genre], @book.genre
30
+ end
31
+
32
+ should "set attributes by methods" do
33
+ @book.title = 'New Title'
34
+ assert_equal 'New Title', @book.title
35
+ end
36
+ end
37
+
38
+ context "to_s" do
39
+ should "inspect" do
40
+ assert_equal @book.inspect, @book.to_s
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ context "on the class" do
47
+ context "find" do
48
+
49
+ context "with an integer" do
50
+ setup do
51
+ @book = Book.find(1)
52
+ end
53
+
54
+ should "set loaded?" do
55
+ assert Book.loaded?
56
+ end
57
+
58
+ should "load by id" do
59
+ assert_equal 1, @book.id
60
+ end
61
+
62
+ should "return instance of klass" do
63
+ assert @book.is_a?(Book)
64
+ end
65
+
66
+ should "raise error if cant find record with id" do
67
+ assert_raise(StaticModel::RecordNotFound) do
68
+ @book = Book.find(1000)
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ context "[]" do
75
+ should "be an alias for find by id" do
76
+ assert_equal Book.find(1), Book[1]
77
+ end
78
+ end
79
+
80
+ context "find(:all)" do
81
+ should "be an alias for find_all" do
82
+ assert_equal Book.find_all, Book.find(:all)
83
+ end
84
+ end
85
+ context "find(:first)" do
86
+ should "be an alias for find_first" do
87
+ assert_equal Book.find_first, Book.find(:first)
88
+ end
89
+ end
90
+
91
+ context "all" do
92
+ should "be an alias for find_all" do
93
+ assert_equal Book.all, Book.find(:all)
94
+ end
95
+ end
96
+ context "first" do
97
+ should "be an alias for find_first" do
98
+ assert_equal Book.first, Book.find(:first)
99
+ end
100
+ end
101
+
102
+ context "find_first" do
103
+ setup do
104
+ @book = Book.find_first
105
+ end
106
+
107
+ should "return the first instance from all records" do
108
+ assert_equal Book.find_all.first, @book
109
+ end
110
+
111
+ should "return instance of klass" do
112
+ assert @book.is_a?(Book)
113
+ end
114
+ end
115
+
116
+ context "find_all" do
117
+ setup do
118
+ @books = Book.find_all
119
+ end
120
+
121
+ should "return an array" do
122
+ assert @books.is_a?(Array)
123
+ end
124
+
125
+ should "return all records" do
126
+ assert_equal 4, @books.length
127
+ end
128
+
129
+ should "return set of klass instances" do
130
+ @books.each do |book|
131
+ assert book.is_a?(Book)
132
+ end
133
+ end
134
+ end
135
+
136
+ context "find_first_by" do
137
+ setup do
138
+ @author = 'Michael Pollan'
139
+ @book = Book.find_first_by(:author,@author)
140
+ end
141
+
142
+ should "return an instance of klass" do
143
+ assert @book.is_a?(Book)
144
+ end
145
+
146
+ should "return record matching search" do
147
+ assert @author, @book.author
148
+ end
149
+
150
+ context "when there is no match" do
151
+ should "return nil" do
152
+ assert_nil Book.find_first_by(:author,'Aaron Quint')
153
+ end
154
+ end
155
+ end
156
+
157
+ context "find_all_by" do
158
+ setup do
159
+ @author = 'Michael Pollan'
160
+ @books = Book.find_all_by(:author,@author)
161
+ end
162
+
163
+ should "return an array" do
164
+ assert @books.is_a?(Array)
165
+ end
166
+
167
+ should "return all records that match search" do
168
+ @books.each {|b| assert_equal @author, b.author}
169
+ end
170
+
171
+ should "return set of klass instances" do
172
+ @books.each {|b| assert b.is_a?(Book) }
173
+ end
174
+
175
+ context "when there is no match" do
176
+ should "return an empty array" do
177
+ assert_equal [], Book.find_all_by(:author,'Aaron Quint')
178
+ end
179
+ end
180
+ context "when there is only one match" do
181
+ should "return an array" do
182
+ assert_equal [Book.find(3)], Book.find_all_by(:author,'Piers Anthony')
183
+ end
184
+ end
185
+ end
186
+
187
+ context "find_first_by_*attribute*" do
188
+ should "be equivalent to find_first_by(attribute,)" do
189
+ assert_equal Book.find_first_by(:genre, 'Non-Fiction'), Book.find_first_by_genre('Non-Fiction')
190
+ end
191
+ end
192
+
193
+ context "find_all_by_*attribute*" do
194
+ should "be equivalent to find_all_by(attribute,)" do
195
+ assert_equal Book.find_all_by(:genre, 'Non-Fiction'), Book.find_all_by_genre('Non-Fiction')
196
+ end
197
+ end
198
+
199
+ context "count" do
200
+ should "return the count of all records" do
201
+ assert_equal Book.all.length, Book.count
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+ end
208
+ end
209
+
210
+ protected
211
+ def book_params
212
+ {:id => 15, :title => 'Lord of the Rings', :author => 'J.R. Tolkien', :genre => 'Fantasy'}
213
+ end
214
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Quint
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-09 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ActiveRecord like functionalities for reading from YAML with a simple class implementation
17
+ email:
18
+ - aaron@quirkey.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - History.txt
25
+ - License.txt
26
+ - Manifest.txt
27
+ - README.txt
28
+ files:
29
+ - History.txt
30
+ - License.txt
31
+ - Manifest.txt
32
+ - README.txt
33
+ - Rakefile
34
+ - config/hoe.rb
35
+ - config/requirements.rb
36
+ - lib/static_model.rb
37
+ - lib/static_model/base.rb
38
+ - lib/static_model/errors.rb
39
+ - lib/static_model/rails.rb
40
+ - lib/static_model/version.rb
41
+ - setup.rb
42
+ - test/books.yml
43
+ - test/test_helper.rb
44
+ - test/test_static_model.rb
45
+ has_rdoc: true
46
+ homepage: http://quirkey.rubyforge.org
47
+ post_install_message: ""
48
+ rdoc_options:
49
+ - --main
50
+ - README.txt
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: quirkey
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: ActiveRecord like functionalities for reading from YAML with a simple class implementation
72
+ test_files:
73
+ - test/test_helper.rb
74
+ - test/test_static_model.rb