mini_model 0.0.0 → 0.0.1
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/mini_model.rb +13 -11
- data/test/all.rb +5 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e8708424bd24b1a49650936429addbe7a258728
|
4
|
+
data.tar.gz: 677ed6b7a4b871dcff0f9c1eaee2e44c9c06cce4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c57dfc9f23486c77ea40014113240c8f5e847a38b0dff3331ce161e383ec0849dc2432b31d118940a4730356d43b890d8a3ca6b4aef960d8ee0889b7aa9a784a
|
7
|
+
data.tar.gz: 3fc9fc50f95689f908bf098fba7d44071a4c42c416d703503fe6eecd8c67f99b02ffd28dd1f2b1cc01e3620e2b76c56dcbb4abdbc7309b97aebbe258bcf028ce
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ but the basics look something like:
|
|
42
42
|
|
43
43
|
`::attribute` Macro for generating an accessor for the attributes hash.
|
44
44
|
|
45
|
-
`::build`
|
45
|
+
`::build` Converts a dataset to an array of model instances.
|
46
46
|
|
47
47
|
`::create` Convenience for `new(attributes).create`.
|
48
48
|
|
data/lib/mini_model.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module MiniModel
|
2
|
-
VERSION = '0.0.
|
2
|
+
VERSION = '0.0.1'
|
3
3
|
|
4
4
|
class Error < StandardError; end
|
5
5
|
|
@@ -36,12 +36,8 @@ module MiniModel
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
def build(
|
40
|
-
|
41
|
-
new(attributes)
|
42
|
-
else
|
43
|
-
nil
|
44
|
-
end
|
39
|
+
def build(dataset)
|
40
|
+
dataset.map { |attributes| new(attributes) }
|
45
41
|
end
|
46
42
|
|
47
43
|
# Convenience for initializin and persisting a
|
@@ -51,19 +47,25 @@ module MiniModel
|
|
51
47
|
end
|
52
48
|
|
53
49
|
def [](id)
|
54
|
-
|
50
|
+
first(id: id)
|
55
51
|
end
|
56
52
|
|
57
53
|
def first(*args, &block)
|
58
|
-
|
54
|
+
attributes = dataset.first(*args, &block)
|
55
|
+
|
56
|
+
if attributes
|
57
|
+
new(attributes)
|
58
|
+
else
|
59
|
+
nil
|
60
|
+
end
|
59
61
|
end
|
60
62
|
|
61
63
|
def all(&block)
|
62
|
-
dataset.all(&block)
|
64
|
+
build(dataset.all(&block))
|
63
65
|
end
|
64
66
|
|
65
67
|
def where(*args, &block)
|
66
|
-
dataset.where(*args, &block)
|
68
|
+
build(dataset.where(*args, &block))
|
67
69
|
end
|
68
70
|
|
69
71
|
def to_foreign_key
|
data/test/all.rb
CHANGED
@@ -95,16 +95,14 @@ test '::attribute defines accessor methods' do
|
|
95
95
|
assert user.respond_to?(:email=)
|
96
96
|
end
|
97
97
|
|
98
|
-
test '::build
|
99
|
-
|
98
|
+
test '::build concerts a dataset to an array of model instances' do
|
99
|
+
User.create(email: 'one@example.com')
|
100
100
|
|
101
|
-
|
102
|
-
end
|
101
|
+
models = User.build(User.dataset.all)
|
103
102
|
|
104
|
-
|
105
|
-
user = User.build(false)
|
103
|
+
assert models.is_a?(Array)
|
106
104
|
|
107
|
-
assert
|
105
|
+
assert models[0].is_a?(User)
|
108
106
|
end
|
109
107
|
|
110
108
|
test '::create creates an instance and persists it in the database' do
|