store 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05c439b9355bde8a092b4bfea3d5d95630177d3b
4
- data.tar.gz: 459439d12096f896eea821d5e57a6a57e7b28217
3
+ metadata.gz: a74cccaaa0ed4cdab16de31489b1ff617729d4c1
4
+ data.tar.gz: 5e91a636c220af914cb10fd87c91c13b69ccf476
5
5
  SHA512:
6
- metadata.gz: 575737d172c70e5338cffe8c85d3b6361a14b775e06ae061f285746832a358f9e9c871c80b80ed1708fdfcd49cc1bca31e431bd5af8e6fe15b3c9f1ab168e9f3
7
- data.tar.gz: c5e233707425ffd8c367c7f4beea0c12748820c893fe1bfb08597f182df19bad000efdd376fed4d47c87eaeaa69f569012ed974080d7f062a80c847e5849a9a8
6
+ metadata.gz: e4e07870ed58bdd9502ccda5399c2f356eb80044f1e35e100f2fa3a671056b1f15205bb765e7e54cfa4b5a01f172d845db925335ac78879336970039e04141a7
7
+ data.tar.gz: 984fc480e714a55d799f2fd57c598e632ddb4477c6a1a5fdc862e50c99f2858a6069992ddcf36f104d47a460eba1ee659c26ec4aa942e5e5a5ae14632968a1c0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Store
1
+ # Store [![Gem Version](https://badge.fury.io/rb/store.png)](http://badge.fury.io/rb/store)
2
2
 
3
3
  Implementations for using the Repository- and Data Mapper Pattern in ruby
4
4
 
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- See example.rb for capabilities of the current implementation.
21
+ See [example.rb](https://github.com/holderbaum/store/blob/master/example.rb) for capabilities of the current implementation.
22
22
 
23
23
  ## Contributing
24
24
 
data/Rakefile CHANGED
@@ -1 +1,17 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:spec) do |t|
6
+ t.libs << "lib"
7
+ t.libs << "spec"
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ desc 'Run test for spec with coverage'
13
+ task :'spec:coverage' do
14
+ system "coverage=1 rake spec"
15
+ end
16
+
17
+ task :default => :spec
data/example.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  require 'store'
2
2
 
3
+ class User
4
+ attr_accessor :name
5
+ end
6
+
3
7
  class Catalog
4
- attr_accessor :name, :products
8
+ attr_accessor :editor, :name, :products
5
9
 
6
10
  def products
7
11
  @products ||= []
@@ -16,9 +20,18 @@ class Product
16
20
  attr_accessor :name, :price
17
21
  end
18
22
 
23
+ class UserEntityMapper < Struct.new(:store, :user)
24
+ def mapped
25
+ {
26
+ 'name' => user.name
27
+ }
28
+ end
29
+ end
30
+
19
31
  class CatalogEntityMapper < Struct.new(:store, :catalog)
20
32
  def mapped
21
33
  {
34
+ 'editor' => store.save(catalog.editor),
22
35
  'name' => catalog.name,
23
36
  'products' => store.save(*catalog.products)
24
37
  }
@@ -34,6 +47,19 @@ class ProductEntityMapper < Struct.new(:store, :product)
34
47
  end
35
48
  end
36
49
 
50
+ class UserDataMapper
51
+ def insert(data)
52
+ reference = :"user_#{rand 100}"
53
+ puts "user_insert[#{reference.inspect}] => #{data.inspect}"
54
+ reference
55
+ end
56
+
57
+ def update(reference, data)
58
+ puts "user_update[#{reference.inspect}] => #{data.inspect}"
59
+ reference
60
+ end
61
+ end
62
+
37
63
  class CatalogDataMapper
38
64
  def insert(data)
39
65
  reference = :"catalog_#{rand 100}"
@@ -62,29 +88,42 @@ end
62
88
 
63
89
 
64
90
  entities = {
91
+ 'User' => User,
65
92
  'Catalog' => Catalog,
66
93
  'Product' => Product
67
94
  }
68
95
 
69
96
  data_mappers = {
97
+ 'User' => UserDataMapper.new,
70
98
  'Catalog' => CatalogDataMapper.new,
71
99
  'Product' => ProductDataMapper.new
72
100
  }
73
101
 
74
102
  entity_mappers = {
103
+ 'User' => UserEntityMapper,
75
104
  'Catalog' => CatalogEntityMapper,
76
105
  'Product' => ProductEntityMapper
77
106
  }
78
107
 
79
108
  store = Store.new(entities, data_mappers, entity_mappers)
80
109
 
81
- catalog = store.build('Catalog', :name => 'Catalog 1')
110
+ editor = store.build 'User', :name => 'Mr. Repo'
111
+
112
+ catalog = store.build 'Catalog', :name => 'Catalog 1', :editor => editor
82
113
 
83
114
  catalog.add_product store.build('Product', :name => 'Pickaxe', :price => 100)
84
115
  catalog.add_product store.build('Product', :name => 'Scredriver', :price => 400)
85
116
 
117
+ p 'save call'
86
118
  store.save(catalog)
87
119
 
88
120
  catalog.products[0].price = 24
89
121
 
122
+ p 'save call'
123
+ store.save(catalog)
124
+
125
+ catalog.products.delete_at(1)
126
+
127
+ p 'save call'
128
+ # no implicit delete here. is this a problem?
90
129
  store.save(catalog)
data/lib/store.rb CHANGED
@@ -5,7 +5,6 @@ class Store
5
5
  @entity_classes = entity_classes
6
6
  @data_mapping = data_mapping
7
7
  @entity_mapping = entity_mapping
8
- @existing_entities = Set.new
9
8
  @entity_references = Hash.new
10
9
  end
11
10
 
@@ -20,7 +19,9 @@ class Store
20
19
  end
21
20
 
22
21
  def save(*entities)
23
- entities.map do |entity|
22
+ references = entities.map do |entity|
23
+ next nil unless entity
24
+
24
25
  data_mapper = data_mapper_for_entity(entity)
25
26
  raise_unless_data_mapper_found(entity, data_mapper)
26
27
 
@@ -32,6 +33,12 @@ class Store
32
33
  data_mapper.update(reference, map_entity(entity))
33
34
  end
34
35
  end
36
+
37
+ if references.size == 1
38
+ references[0]
39
+ else
40
+ references
41
+ end
35
42
  end
36
43
 
37
44
  def remove(entity)
data/lib/store/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Store
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/store_spec.rb CHANGED
@@ -38,15 +38,19 @@ describe 'store' do
38
38
  it 'delegates second save to update' do
39
39
  data_mapper.expect(:insert, :ref1, [entity_hash])
40
40
 
41
- store.save(entity)
41
+ assert_equal :ref1, store.save(entity)
42
42
 
43
- data_mapper.expect(:update, nil, [:ref1, entity_hash])
43
+ data_mapper.expect(:update, :ref1, [:ref1, entity_hash])
44
44
 
45
- store.save(entity)
45
+ assert_equal :ref1, store.save(entity)
46
46
 
47
47
  assert data_mapper.verify
48
48
  end
49
49
 
50
+ it 'returns nil if entity of save is nil' do
51
+ assert_nil store.save(nil)
52
+ end
53
+
50
54
  it 'fails save if data_mapper not found' do
51
55
  entity = OpenStruct.new(:test => 42)
52
56
  error = assert_raises RuntimeError do
@@ -56,6 +60,23 @@ describe 'store' do
56
60
  assert_match(/no data_mapper .* found/i, error.message)
57
61
  end
58
62
 
63
+ it 'returns references from insert/update save' do
64
+ e1 = entity_class.new('e1', 'e1')
65
+ e1_hash = {'email' => 'e1', 'gender' => 'e1'}
66
+ e2 = entity_class.new('e2', 'e2')
67
+ e2_hash = {'email' => 'e2', 'gender' => 'e2'}
68
+
69
+ data_mapper.expect(:insert, :ref1, [e1_hash])
70
+ store.save(e1)
71
+
72
+ data_mapper.expect(:update, :ref1, [:ref1, e1_hash])
73
+ data_mapper.expect(:insert, :ref2, [e2_hash])
74
+
75
+ assert_equal [:ref1, :ref2], store.save(e1, e2)
76
+
77
+ assert data_mapper.verify
78
+ end
79
+
59
80
  it 'delegates query to select' do
60
81
  data_mapper.expect(:select, nil, [query])
61
82
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Holderbaum