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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/mini_model.rb +13 -11
  4. data/test/all.rb +5 -7
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8e0f797ba49f5f53cd9f8951740f3be6cfd0106
4
- data.tar.gz: 60127c7c9cf2997c7ffcc33b0eb99a8ac69293ba
3
+ metadata.gz: 9e8708424bd24b1a49650936429addbe7a258728
4
+ data.tar.gz: 677ed6b7a4b871dcff0f9c1eaee2e44c9c06cce4
5
5
  SHA512:
6
- metadata.gz: b28d1b7ea7b1d3d3fb356eca45294c528beaab5f6d96addc8ceb633a76a003e2437c3160e24182b500f0c0d455c5c576d6250f0dbd8aecde05c13001256d7d74
7
- data.tar.gz: 9ce5cc6657617da3953f50897cdc2e71432efe0f419821c652a4bf26bb2b850ac4fb0e49a54fdb80deffad8849ed7c14f1b26002bf28a29d6f05dddb7dc133c8
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` Builds a new instance of the model when given attributes.
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.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(attributes)
40
- if attributes
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
- build(dataset.first(id: id))
50
+ first(id: id)
55
51
  end
56
52
 
57
53
  def first(*args, &block)
58
- build(dataset.first(*args, &block))
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).map { |each| build(each) }
64
+ build(dataset.all(&block))
63
65
  end
64
66
 
65
67
  def where(*args, &block)
66
- dataset.where(*args, &block).map { |each| build(each) }
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 returns an instance when given a hash' do
99
- user = User.build({})
98
+ test '::build concerts a dataset to an array of model instances' do
99
+ User.create(email: 'one@example.com')
100
100
 
101
- assert user.is_a?(User)
102
- end
101
+ models = User.build(User.dataset.all)
103
102
 
104
- test '::build returns nil when given a falsey value' do
105
- user = User.build(false)
103
+ assert models.is_a?(Array)
106
104
 
107
- assert user.nil?
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Weiss