static_model 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/lib/static_model/base.rb +4 -3
- data/lib/static_model/version.rb +1 -1
- data/test/test_static_model.rb +28 -0
- data/test/test_static_model_scope.rb +8 -4
- metadata +2 -2
data/History.txt
CHANGED
data/lib/static_model/base.rb
CHANGED
@@ -9,7 +9,7 @@ module StaticModel
|
|
9
9
|
attr_reader :id
|
10
10
|
|
11
11
|
def initialize(attribute_hash = {})
|
12
|
-
raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {}") unless attribute_hash.is_a?(Hash)
|
12
|
+
raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
|
13
13
|
@id = attribute_hash.delete('id') || attribute_hash.delete(:id) || (self.class.count + 1)
|
14
14
|
self.attributes = attribute_hash
|
15
15
|
end
|
@@ -68,6 +68,7 @@ module StaticModel
|
|
68
68
|
def find_first_by(attribute, value)
|
69
69
|
records.find {|r| r.send(attribute) == value }
|
70
70
|
end
|
71
|
+
alias_method :find_by, :find_first_by
|
71
72
|
|
72
73
|
def load(reload = false)
|
73
74
|
return if loaded? && !reload
|
@@ -110,8 +111,8 @@ module StaticModel
|
|
110
111
|
private
|
111
112
|
def method_missing(meth, *args)
|
112
113
|
meth_name = meth.to_s
|
113
|
-
if meth_name =~ /^find_(
|
114
|
-
attribute_name = meth_name.gsub(/^find_(
|
114
|
+
if meth_name =~ /^find_(all_by|first_by|by)_(.+)/
|
115
|
+
attribute_name = meth_name.gsub(/^find_(all_by|first_by|by)_/, '')
|
115
116
|
finder = meth_name.gsub(/_#{attribute_name}/, '')
|
116
117
|
return self.send(finder, attribute_name, *args)
|
117
118
|
end
|
data/lib/static_model/version.rb
CHANGED
data/test/test_static_model.rb
CHANGED
@@ -194,6 +194,28 @@ class TestStaticModel < Test::Unit::TestCase
|
|
194
194
|
end
|
195
195
|
end
|
196
196
|
end
|
197
|
+
|
198
|
+
context "find_by" do
|
199
|
+
setup do
|
200
|
+
@author = 'Michael Pollan'
|
201
|
+
@book = Book.find_by(:author,@author)
|
202
|
+
end
|
203
|
+
|
204
|
+
should "return an instance of klass" do
|
205
|
+
assert @book.is_a?(Book)
|
206
|
+
end
|
207
|
+
|
208
|
+
should "return record matching search" do
|
209
|
+
assert @author, @book.author
|
210
|
+
end
|
211
|
+
|
212
|
+
context "when there is no match" do
|
213
|
+
should "return nil" do
|
214
|
+
assert_nil Book.find_by(:author,'Aaron Quint')
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
197
219
|
|
198
220
|
context "find_all_by" do
|
199
221
|
setup do
|
@@ -225,6 +247,12 @@ class TestStaticModel < Test::Unit::TestCase
|
|
225
247
|
end
|
226
248
|
end
|
227
249
|
|
250
|
+
context "find_by_*attribute*" do
|
251
|
+
should "be equivalent to find_first_by(attribute,)" do
|
252
|
+
assert_equal Book.find_by(:genre, 'Non-Fiction'), Book.find_first_by_genre('Non-Fiction')
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
228
256
|
context "find_first_by_*attribute*" do
|
229
257
|
should "be equivalent to find_first_by(attribute,)" do
|
230
258
|
assert_equal Book.find_first_by(:genre, 'Non-Fiction'), Book.find_first_by_genre('Non-Fiction')
|
@@ -2,9 +2,13 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestStaticModelScope < Test::Unit::TestCase
|
4
4
|
|
5
|
-
context "a static model class" do
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
# context "a static model class" do
|
6
|
+
# context "A class with scope" do
|
7
|
+
#
|
8
|
+
# end
|
9
|
+
# end
|
10
|
+
|
11
|
+
def test_truth
|
12
|
+
assert true
|
9
13
|
end
|
10
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Quint
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-27 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|