datamappify 0.20.1 → 0.30.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +10 -4
- data/README.md +28 -33
- data/datamappify.gemspec +3 -1
- data/lib/datamappify/data/criteria/active_record/count.rb +12 -0
- data/lib/datamappify/data/criteria/active_record/destroy.rb +17 -0
- data/lib/datamappify/data/criteria/active_record/exists.rb +13 -0
- data/lib/datamappify/data/criteria/active_record/find.rb +12 -0
- data/lib/datamappify/data/criteria/active_record/find_by_key.rb +12 -0
- data/lib/datamappify/data/criteria/active_record/save.rb +23 -0
- data/lib/datamappify/data/criteria/active_record/save_by_key.rb +14 -0
- data/lib/datamappify/data/criteria/active_record/transaction.rb +13 -0
- data/lib/datamappify/data/criteria/common.rb +96 -0
- data/lib/datamappify/data/criteria/relational/count.rb +13 -0
- data/lib/datamappify/data/criteria/relational/find.rb +23 -0
- data/lib/datamappify/data/criteria/relational/find_by_key.rb +17 -0
- data/lib/datamappify/data/criteria/relational/save.rb +26 -0
- data/lib/datamappify/data/criteria/relational/save_by_key.rb +15 -0
- data/lib/datamappify/data/criteria/sequel/count.rb +12 -0
- data/lib/datamappify/data/criteria/sequel/destroy.rb +17 -0
- data/lib/datamappify/data/criteria/sequel/exists.rb +13 -0
- data/lib/datamappify/data/criteria/sequel/find.rb +12 -0
- data/lib/datamappify/data/criteria/sequel/find_by_key.rb +12 -0
- data/lib/datamappify/data/criteria/sequel/save.rb +23 -0
- data/lib/datamappify/data/criteria/sequel/save_by_key.rb +14 -0
- data/lib/datamappify/data/criteria/sequel/transaction.rb +13 -0
- data/lib/datamappify/data/criteria.rb +8 -0
- data/lib/datamappify/data/errors.rb +1 -0
- data/lib/datamappify/data/mapper/attribute.rb +52 -0
- data/lib/datamappify/data/mapper.rb +95 -0
- data/lib/datamappify/data/provider/active_record.rb +7 -7
- data/lib/datamappify/data/provider/common_provider.rb +67 -0
- data/lib/datamappify/data/provider/sequel.rb +9 -9
- data/lib/datamappify/data/provider.rb +12 -0
- data/lib/datamappify/data/record.rb +13 -0
- data/lib/datamappify/data.rb +4 -0
- data/lib/datamappify/repository/mapping_dsl.rb +28 -0
- data/lib/datamappify/repository/query_method/count.rb +12 -0
- data/lib/datamappify/repository/query_method/destroy.rb +25 -0
- data/lib/datamappify/repository/query_method/find.rb +41 -0
- data/lib/datamappify/repository/query_method/method.rb +73 -0
- data/lib/datamappify/repository/query_method/save.rb +42 -0
- data/lib/datamappify/repository/query_method/transaction.rb +18 -0
- data/lib/datamappify/repository/query_method.rb +3 -0
- data/lib/datamappify/repository.rb +58 -92
- data/lib/datamappify/version.rb +1 -1
- data/lib/datamappify.rb +10 -5
- data/spec/repository/persistence_spec.rb +15 -23
- data/spec/repository_spec.rb +9 -5
- metadata +70 -15
- data/lib/datamappify/data/provider/active_record/persistence.rb +0 -31
- data/lib/datamappify/data/provider/common/persistence.rb +0 -57
- data/lib/datamappify/data/provider/common/relational/persistence.rb +0 -85
- data/lib/datamappify/data/provider/common/relational/record/mapper.rb +0 -24
- data/lib/datamappify/data/provider/common/relational/record/writer.rb +0 -67
- data/lib/datamappify/data/provider/sequel/persistence.rb +0 -31
- data/lib/datamappify/repository/attribute_source_data_class_builder.rb +0 -28
- data/lib/datamappify/repository/attributes_mapper.rb +0 -55
- data/lib/datamappify/repository/dsl.rb +0 -22
- data/lib/datamappify/repository/mapping_hash.rb +0 -8
- data/lib/datamappify/util.rb +0 -13
@@ -1,118 +1,84 @@
|
|
1
|
-
require '
|
2
|
-
require 'datamappify/
|
3
|
-
require 'datamappify/
|
4
|
-
require 'datamappify/repository/dsl'
|
5
|
-
require 'datamappify/repository/attributes_mapper'
|
1
|
+
require 'datamappify/repository/mapping_dsl'
|
2
|
+
require 'datamappify/repository/query_method'
|
3
|
+
require 'datamappify/data'
|
6
4
|
|
7
5
|
module Datamappify
|
8
6
|
module Repository
|
9
7
|
def self.included(klass)
|
10
8
|
klass.class_eval do
|
11
|
-
mattr_accessor :entity_class
|
12
|
-
mattr_accessor :default_provider_class_name
|
13
|
-
mattr_accessor :default_data_class_name
|
14
|
-
mattr_accessor :custom_attributes_mapping
|
15
|
-
mattr_accessor :data_mapping
|
16
|
-
|
17
|
-
klass.custom_attributes_mapping = {}
|
18
|
-
klass.data_mapping = {}
|
19
|
-
|
20
9
|
include Singleton
|
21
|
-
extend
|
22
|
-
end
|
23
|
-
end
|
10
|
+
extend SingletonWrapper
|
24
11
|
|
25
|
-
|
26
|
-
AttributesMapper.new(self).build_data_classes
|
27
|
-
self
|
28
|
-
end
|
12
|
+
cattr_accessor :data_mapper
|
29
13
|
|
30
|
-
|
31
|
-
entities = Array.wrap(id_or_ids).map { |id| entity_class.new(:id => id) }
|
14
|
+
self.data_mapper = Data::Mapper.new
|
32
15
|
|
33
|
-
|
34
|
-
|
35
|
-
provider_class_name, entities, data_class_name
|
36
|
-
).find(data_fields_mapping)
|
16
|
+
extend MappingDSL
|
17
|
+
include InstanceMethods
|
37
18
|
end
|
38
|
-
|
39
|
-
id_or_ids.is_a?(Array) ? entities : entities[0]
|
40
19
|
end
|
41
20
|
|
42
|
-
|
43
|
-
|
44
|
-
|
21
|
+
module InstanceMethods
|
22
|
+
# @param id_or_ids [Integer, Array<Integer>]
|
23
|
+
# an entity id or a collection of entity ids
|
24
|
+
#
|
25
|
+
# @return [Entity, Array<Entity>, nil]
|
26
|
+
def find(id_or_ids)
|
27
|
+
QueryMethod::Find.new(data_mapper, id_or_ids).result
|
45
28
|
end
|
46
29
|
|
47
|
-
entity_or_entities
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
save(entity_or_entities) || raise(Datamappify::Data::EntityNotSaved)
|
54
|
-
end
|
55
|
-
|
56
|
-
def destroy(id_ids_or_entity_entities)
|
57
|
-
ids = Array.wrap(id_ids_or_entity_entities).map do |id_or_entity|
|
58
|
-
Util.extract_entity_id(id_or_entity)
|
30
|
+
# @param entity_or_entities [Entity, Array<Entity>]
|
31
|
+
# an entity or a collection of entities
|
32
|
+
#
|
33
|
+
# @return [Entity, Array<Entity>, false]
|
34
|
+
def save(entity_or_entities)
|
35
|
+
QueryMethod::Save.new(data_mapper, entity_or_entities).result
|
59
36
|
end
|
60
37
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
def method_missing(symbol, *args)
|
69
|
-
default_persistence.send symbol, *args
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def default_persistence
|
75
|
-
persistence_class(default_provider_class_name).new(
|
76
|
-
default_provider_class_name, [], default_data_class_name
|
77
|
-
)
|
78
|
-
end
|
79
|
-
|
80
|
-
def persistence_class(provider_class_name)
|
81
|
-
"Datamappify::Data::Provider::#{provider_class_name}::Persistence".constantize
|
82
|
-
end
|
83
|
-
|
84
|
-
def create_or_update(entity)
|
85
|
-
raise Datamappify::Data::EntityInvalid.new(entity) if entity.invalid?
|
86
|
-
|
87
|
-
default_persistence.exists?(entity.id) ? update(entity) : create(entity)
|
88
|
-
end
|
89
|
-
|
90
|
-
def create(entity)
|
91
|
-
data_mapping_walker do |provider_class_name, data_class_name, data_fields_mapping|
|
92
|
-
persistence_class(provider_class_name).new(
|
93
|
-
provider_class_name, [entity], data_class_name
|
94
|
-
).create(data_fields_mapping)
|
38
|
+
# @param (see #save)
|
39
|
+
#
|
40
|
+
# @raise [Data::EntityNotSaved]
|
41
|
+
#
|
42
|
+
# @return [Entity, Array<Entity>]
|
43
|
+
def save!(entity_or_entities)
|
44
|
+
save(entity_or_entities) || raise(Data::EntityNotSaved)
|
95
45
|
end
|
96
46
|
|
97
|
-
|
98
|
-
|
47
|
+
# @param id_or_ids_or_entity_or_entities [Entity, Array<Entity>]
|
48
|
+
# an entity or a collection of ids or entities
|
49
|
+
#
|
50
|
+
# @return [void, false]
|
51
|
+
def destroy(id_or_ids_or_entity_or_entities)
|
52
|
+
QueryMethod::Destroy.new(data_mapper, id_or_ids_or_entity_or_entities).result
|
53
|
+
end
|
99
54
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
55
|
+
# @param (see #destroy)
|
56
|
+
#
|
57
|
+
# @raise [Data::EntityNotDestroyed]
|
58
|
+
#
|
59
|
+
# @return [void]
|
60
|
+
def destroy!(id_or_ids_or_entity_or_entities)
|
61
|
+
destroy(id_or_ids_or_entity_or_entities) || raise(Data::EntityNotDestroyed)
|
105
62
|
end
|
106
63
|
|
107
|
-
|
64
|
+
# @return [Integer]
|
65
|
+
def count
|
66
|
+
QueryMethod::Count.new(data_mapper).result
|
67
|
+
end
|
108
68
|
end
|
109
69
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
70
|
+
# Wraps a ruby Singleton class so that calling `instance` is no longer necessary.
|
71
|
+
#
|
72
|
+
# @example With `instance`
|
73
|
+
# UserRepository.instance.count
|
74
|
+
#
|
75
|
+
# @example Without `instance`
|
76
|
+
# UserRepository.count
|
77
|
+
module SingletonWrapper
|
78
|
+
def self.extended(klass)
|
79
|
+
class << klass
|
80
|
+
extend Forwardable
|
81
|
+
def_delegators :instance, *InstanceMethods.instance_methods
|
116
82
|
end
|
117
83
|
end
|
118
84
|
end
|
data/lib/datamappify/version.rb
CHANGED
data/lib/datamappify.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require "datamappify/entity"
|
4
|
-
require "datamappify/data"
|
5
|
-
require "datamappify/repository"
|
1
|
+
require 'active_support'
|
2
|
+
require 'datamappify/version'
|
6
3
|
|
7
4
|
module Datamappify
|
5
|
+
# @return [Pathname]
|
6
|
+
def self.root
|
7
|
+
Pathname.new("#{File.dirname(__FILE__)}/datamappify")
|
8
|
+
end
|
8
9
|
end
|
10
|
+
|
11
|
+
require 'datamappify/entity'
|
12
|
+
require 'datamappify/data'
|
13
|
+
require 'datamappify/repository'
|
@@ -1,22 +1,20 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
shared_examples_for "repository persistence" do |data_provider|
|
4
|
-
let!(:user_repository) { "UserRepository#{data_provider}".constantize
|
5
|
-
let(:existing_user) { User.new(:
|
4
|
+
let!(:user_repository) { "UserRepository#{data_provider}".constantize }
|
5
|
+
let(:existing_user) { user_repository.save(User.new(:first_name => 'Fred', :driver_license => 'FREDWU42')) }
|
6
6
|
let(:new_valid_user) { User.new(:first_name => 'Batman', :driver_license => 'ARKHAMCITY') }
|
7
7
|
let(:new_valid_user2) { User.new(:first_name => 'Ironman', :driver_license => 'NEWYORKCITY') }
|
8
8
|
let(:new_invalid_user) { User.new(:first_name => 'a') }
|
9
9
|
let(:new_invalid_user2) { User.new(:first_name => 'b') }
|
10
|
-
let(:data_passports) { "Datamappify::Data::#{data_provider}::UserPassport".constantize }
|
11
|
-
let(:data_driver_licenses) { "Datamappify::Data::#{data_provider}::UserDriverLicense".constantize }
|
10
|
+
let(:data_passports) { "Datamappify::Data::Record::#{data_provider}::UserPassport".constantize }
|
11
|
+
let(:data_driver_licenses) { "Datamappify::Data::Record::#{data_provider}::UserDriverLicense".constantize }
|
12
12
|
|
13
13
|
describe "#find" do
|
14
14
|
describe "resource" do
|
15
|
-
let!(:user) { user_repository.save(existing_user) }
|
16
|
-
|
17
15
|
it "found" do
|
18
|
-
user_repository.find(
|
19
|
-
user_repository.find([
|
16
|
+
user_repository.find(existing_user.id).should == existing_user
|
17
|
+
user_repository.find([existing_user.id]).should == [existing_user]
|
20
18
|
end
|
21
19
|
|
22
20
|
it "not found" do
|
@@ -25,14 +23,12 @@ shared_examples_for "repository persistence" do |data_provider|
|
|
25
23
|
end
|
26
24
|
|
27
25
|
describe "collection" do
|
28
|
-
let!(:user) { user_repository.save(existing_user) }
|
29
|
-
|
30
26
|
it "found" do
|
31
|
-
user_repository.find([
|
27
|
+
user_repository.find([existing_user.id]).should == [existing_user]
|
32
28
|
end
|
33
29
|
|
34
30
|
it "partial found" do
|
35
|
-
user_repository.find([
|
31
|
+
user_repository.find([existing_user.id, 42]).should == [existing_user]
|
36
32
|
end
|
37
33
|
|
38
34
|
it "not found" do
|
@@ -98,14 +94,12 @@ shared_examples_for "repository persistence" do |data_provider|
|
|
98
94
|
|
99
95
|
describe "update an existing entity" do
|
100
96
|
it "updates existing records" do
|
101
|
-
|
102
|
-
|
103
|
-
user.first_name = 'Vivian'
|
104
|
-
user.driver_license = 'LOCOMOTE'
|
97
|
+
existing_user.first_name = 'Vivian'
|
98
|
+
existing_user.driver_license = 'LOCOMOTE'
|
105
99
|
|
106
100
|
updated_user = nil
|
107
101
|
|
108
|
-
expect { updated_user = user_repository.save(
|
102
|
+
expect { updated_user = user_repository.save(existing_user) }.to change { user_repository.count }.by(0)
|
109
103
|
|
110
104
|
updated_user.first_name.should == 'Vivian'
|
111
105
|
updated_user.driver_license.should == 'LOCOMOTE'
|
@@ -117,14 +111,12 @@ shared_examples_for "repository persistence" do |data_provider|
|
|
117
111
|
end
|
118
112
|
|
119
113
|
it "updates existing and new records" do
|
120
|
-
|
121
|
-
|
122
|
-
user.first_name = 'Vivian'
|
123
|
-
user.health_care = 'BATMANCAVE'
|
114
|
+
existing_user.first_name = 'Vivian'
|
115
|
+
existing_user.health_care = 'BATMANCAVE'
|
124
116
|
|
125
117
|
updated_user = nil
|
126
118
|
|
127
|
-
expect { updated_user = user_repository.save(
|
119
|
+
expect { updated_user = user_repository.save(existing_user) }.to change { user_repository.count }.by(0)
|
128
120
|
|
129
121
|
updated_user.first_name.should == 'Vivian'
|
130
122
|
updated_user.health_care.should == 'BATMANCAVE'
|
@@ -170,7 +162,7 @@ describe Datamappify::Repository do
|
|
170
162
|
end
|
171
163
|
|
172
164
|
describe "entity composed from multiple data providers" do
|
173
|
-
let!(:hero_repository) { HeroUserRepository
|
165
|
+
let!(:hero_repository) { HeroUserRepository }
|
174
166
|
let!(:hero) { HeroUser.new :first_name => 'Aaron', :last_name => 'Patterson' }
|
175
167
|
|
176
168
|
it "#find" do
|
data/spec/repository_spec.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
3
|
shared_examples_for "a repository" do |data_provider|
|
4
|
-
let(:
|
5
|
-
let
|
6
|
-
subject { data_provider_module }
|
4
|
+
let(:namespace) { "Datamappify::Data::Record::#{data_provider}".constantize }
|
5
|
+
let(:user_repository) { "UserRepository#{data_provider}".constantize.instance }
|
7
6
|
|
8
|
-
it "defines the data class
|
9
|
-
|
7
|
+
it "defines the data class" do
|
8
|
+
user_repository.data_mapper.default_source_class
|
9
|
+
namespace.const_defined?(:User, false).should == true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "delegates methods to the instance singleton" do
|
13
|
+
expect { "UserRepository#{data_provider}".constantize.find(1) }.to_not raise_error
|
10
14
|
end
|
11
15
|
end
|
12
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datamappify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.30.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fred Wu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|
@@ -72,6 +72,34 @@ dependencies:
|
|
72
72
|
- - '>='
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: redcarpet
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: yard
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
75
103
|
- !ruby/object:Gem::Dependency
|
76
104
|
name: rspec
|
77
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -210,7 +238,7 @@ dependencies:
|
|
210
238
|
- - <
|
211
239
|
- !ruby/object:Gem::Version
|
212
240
|
version: '2'
|
213
|
-
description:
|
241
|
+
description: Compose and manage domain logic and data persistence separately and intelligently.
|
214
242
|
email:
|
215
243
|
- ifredwu@gmail.com
|
216
244
|
executables: []
|
@@ -220,6 +248,7 @@ files:
|
|
220
248
|
- .gitignore
|
221
249
|
- .rspec
|
222
250
|
- .travis.yml
|
251
|
+
- .yardopts
|
223
252
|
- CHANGELOG.md
|
224
253
|
- Gemfile
|
225
254
|
- README.md
|
@@ -227,22 +256,47 @@ files:
|
|
227
256
|
- datamappify.gemspec
|
228
257
|
- lib/datamappify.rb
|
229
258
|
- lib/datamappify/data.rb
|
259
|
+
- lib/datamappify/data/criteria.rb
|
260
|
+
- lib/datamappify/data/criteria/active_record/count.rb
|
261
|
+
- lib/datamappify/data/criteria/active_record/destroy.rb
|
262
|
+
- lib/datamappify/data/criteria/active_record/exists.rb
|
263
|
+
- lib/datamappify/data/criteria/active_record/find.rb
|
264
|
+
- lib/datamappify/data/criteria/active_record/find_by_key.rb
|
265
|
+
- lib/datamappify/data/criteria/active_record/save.rb
|
266
|
+
- lib/datamappify/data/criteria/active_record/save_by_key.rb
|
267
|
+
- lib/datamappify/data/criteria/active_record/transaction.rb
|
268
|
+
- lib/datamappify/data/criteria/common.rb
|
269
|
+
- lib/datamappify/data/criteria/relational/count.rb
|
270
|
+
- lib/datamappify/data/criteria/relational/find.rb
|
271
|
+
- lib/datamappify/data/criteria/relational/find_by_key.rb
|
272
|
+
- lib/datamappify/data/criteria/relational/save.rb
|
273
|
+
- lib/datamappify/data/criteria/relational/save_by_key.rb
|
274
|
+
- lib/datamappify/data/criteria/sequel/count.rb
|
275
|
+
- lib/datamappify/data/criteria/sequel/destroy.rb
|
276
|
+
- lib/datamappify/data/criteria/sequel/exists.rb
|
277
|
+
- lib/datamappify/data/criteria/sequel/find.rb
|
278
|
+
- lib/datamappify/data/criteria/sequel/find_by_key.rb
|
279
|
+
- lib/datamappify/data/criteria/sequel/save.rb
|
280
|
+
- lib/datamappify/data/criteria/sequel/save_by_key.rb
|
281
|
+
- lib/datamappify/data/criteria/sequel/transaction.rb
|
230
282
|
- lib/datamappify/data/errors.rb
|
283
|
+
- lib/datamappify/data/mapper.rb
|
284
|
+
- lib/datamappify/data/mapper/attribute.rb
|
285
|
+
- lib/datamappify/data/provider.rb
|
231
286
|
- lib/datamappify/data/provider/active_record.rb
|
232
|
-
- lib/datamappify/data/provider/
|
233
|
-
- lib/datamappify/data/provider/common/persistence.rb
|
234
|
-
- lib/datamappify/data/provider/common/relational/persistence.rb
|
235
|
-
- lib/datamappify/data/provider/common/relational/record/mapper.rb
|
236
|
-
- lib/datamappify/data/provider/common/relational/record/writer.rb
|
287
|
+
- lib/datamappify/data/provider/common_provider.rb
|
237
288
|
- lib/datamappify/data/provider/sequel.rb
|
238
|
-
- lib/datamappify/data/
|
289
|
+
- lib/datamappify/data/record.rb
|
239
290
|
- lib/datamappify/entity.rb
|
240
291
|
- lib/datamappify/repository.rb
|
241
|
-
- lib/datamappify/repository/
|
242
|
-
- lib/datamappify/repository/
|
243
|
-
- lib/datamappify/repository/
|
244
|
-
- lib/datamappify/repository/
|
245
|
-
- lib/datamappify/
|
292
|
+
- lib/datamappify/repository/mapping_dsl.rb
|
293
|
+
- lib/datamappify/repository/query_method.rb
|
294
|
+
- lib/datamappify/repository/query_method/count.rb
|
295
|
+
- lib/datamappify/repository/query_method/destroy.rb
|
296
|
+
- lib/datamappify/repository/query_method/find.rb
|
297
|
+
- lib/datamappify/repository/query_method/method.rb
|
298
|
+
- lib/datamappify/repository/query_method/save.rb
|
299
|
+
- lib/datamappify/repository/query_method/transaction.rb
|
246
300
|
- lib/datamappify/version.rb
|
247
301
|
- spec/entity_spec.rb
|
248
302
|
- spec/repository/persistence_spec.rb
|
@@ -287,7 +341,7 @@ rubyforge_project:
|
|
287
341
|
rubygems_version: 2.0.0
|
288
342
|
signing_key:
|
289
343
|
specification_version: 4
|
290
|
-
summary:
|
344
|
+
summary: Compose and manage domain logic and data persistence separately and intelligently.
|
291
345
|
test_files:
|
292
346
|
- spec/entity_spec.rb
|
293
347
|
- spec/repository/persistence_spec.rb
|
@@ -309,3 +363,4 @@ test_files:
|
|
309
363
|
- spec/support/repositories/sequel/user_repository.rb
|
310
364
|
- spec/support/tables/active_record.rb
|
311
365
|
- spec/support/tables/sequel.rb
|
366
|
+
has_rdoc:
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'datamappify/data/provider/common/relational/persistence'
|
2
|
-
require 'datamappify/data/provider/common/relational/record/mapper'
|
3
|
-
require 'datamappify/data/provider/common/relational/record/writer'
|
4
|
-
|
5
|
-
module Datamappify
|
6
|
-
module Data
|
7
|
-
module Provider
|
8
|
-
module ActiveRecord
|
9
|
-
class Persistence < Data::Provider::Common::Relational::Persistence
|
10
|
-
def destroy(ids)
|
11
|
-
data_class.destroy(ids)
|
12
|
-
end
|
13
|
-
|
14
|
-
def exists?(id)
|
15
|
-
data_class.exists?(id)
|
16
|
-
end
|
17
|
-
|
18
|
-
def transaction(&block)
|
19
|
-
data_class.transaction(&block)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def record_attributes_method
|
25
|
-
:attributes
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
module Datamappify
|
2
|
-
module Data
|
3
|
-
module Provider
|
4
|
-
module Common
|
5
|
-
class Persistence
|
6
|
-
def initialize(provider_class_name, entities, data_class_name)
|
7
|
-
@provider_class_name = provider_class_name
|
8
|
-
@entities = entities
|
9
|
-
@data_class_name = data_class_name
|
10
|
-
end
|
11
|
-
|
12
|
-
def find(data_fields_mapping)
|
13
|
-
raise Data::MethodNotImplemented
|
14
|
-
end
|
15
|
-
|
16
|
-
def create(data_fields_mapping)
|
17
|
-
raise Data::MethodNotImplemented
|
18
|
-
end
|
19
|
-
|
20
|
-
def update(data_fields_mapping)
|
21
|
-
raise Data::MethodNotImplemented
|
22
|
-
end
|
23
|
-
|
24
|
-
def destroy(id_or_entity)
|
25
|
-
raise Data::MethodNotImplemented
|
26
|
-
end
|
27
|
-
|
28
|
-
def exists?(id)
|
29
|
-
raise Data::MethodNotImplemented
|
30
|
-
end
|
31
|
-
|
32
|
-
def transaction(&block)
|
33
|
-
raise Data::MethodNotImplemented
|
34
|
-
end
|
35
|
-
|
36
|
-
def method_missing(symbol, *args)
|
37
|
-
data_class.send symbol, *args
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def data_class
|
43
|
-
"Datamappify::Data::#{@provider_class_name}::#{@data_class_name}".constantize
|
44
|
-
end
|
45
|
-
|
46
|
-
def entity_class_name
|
47
|
-
@entities[0].class.name
|
48
|
-
end
|
49
|
-
|
50
|
-
def is_entity_class?
|
51
|
-
@data_class_name == entity_class_name
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'datamappify/data/provider/common/persistence'
|
2
|
-
|
3
|
-
module Datamappify
|
4
|
-
module Data
|
5
|
-
module Provider
|
6
|
-
module Common
|
7
|
-
module Relational
|
8
|
-
class Persistence < Data::Provider::Common::Persistence
|
9
|
-
def find(data_fields_mapping)
|
10
|
-
records = find_records(data_fields_mapping)
|
11
|
-
|
12
|
-
entities_walker do |entity|
|
13
|
-
if record = find_record_for_entity(entity, records)
|
14
|
-
Common::Relational::RecordMapper.new(
|
15
|
-
entity, record.send(record_attributes_method)
|
16
|
-
).map_attributes(data_fields_mapping)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def create(data_fields_mapping)
|
22
|
-
entities_walker do |entity|
|
23
|
-
if has_data_to_insert?(entity, data_fields_mapping)
|
24
|
-
Common::Relational::RecordWriter.new(
|
25
|
-
entity, data_class, key_field_name
|
26
|
-
).insert_record(data_fields_mapping)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def update(data_fields_mapping)
|
32
|
-
records = find_records(data_fields_mapping)
|
33
|
-
|
34
|
-
entities_walker do |entity|
|
35
|
-
record_writer = Common::Relational::RecordWriter.new(entity, data_class, key_field_name)
|
36
|
-
|
37
|
-
if record = find_record_for_entity(entity, records)
|
38
|
-
record_writer.update_record(record, data_fields_mapping)
|
39
|
-
elsif has_data_to_insert?(entity, data_fields_mapping)
|
40
|
-
record_writer.insert_record(data_fields_mapping)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def find_record_for_entity(entity, records)
|
48
|
-
records.find { |r| r.send(key_field_name) == entity.id }
|
49
|
-
end
|
50
|
-
|
51
|
-
def find_records(data_fields_mapping)
|
52
|
-
data_class.select(
|
53
|
-
*field_names(data_fields_mapping)
|
54
|
-
).where(
|
55
|
-
key_field_name => @entities.map(&:id)
|
56
|
-
)
|
57
|
-
end
|
58
|
-
|
59
|
-
def entities_walker
|
60
|
-
walker_method = is_entity_class? ? :keep_if : :each
|
61
|
-
|
62
|
-
@entities.send(walker_method) do |entity|
|
63
|
-
yield(entity)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def key_field_name
|
68
|
-
is_entity_class? ? :id : "#{entity_class_name.underscore}_id".to_sym
|
69
|
-
end
|
70
|
-
|
71
|
-
def field_names(data_fields_mapping)
|
72
|
-
(data_fields_mapping.field_names << key_field_name << :id).uniq
|
73
|
-
end
|
74
|
-
|
75
|
-
def has_data_to_insert?(entity, data_fields_mapping)
|
76
|
-
attribute_names = entity.attributes.keys & data_fields_mapping.attribute_names
|
77
|
-
|
78
|
-
attribute_names.any? && Util.attributes_filtered_by(entity, attribute_names).values.compact.any?
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Datamappify
|
2
|
-
module Data
|
3
|
-
module Provider
|
4
|
-
module Common
|
5
|
-
module Relational
|
6
|
-
class RecordMapper
|
7
|
-
def initialize(entity, record_attributes)
|
8
|
-
@entity = entity
|
9
|
-
@record_attributes = record_attributes
|
10
|
-
end
|
11
|
-
|
12
|
-
def map_attributes(data_fields_mapping)
|
13
|
-
@record_attributes.each do |name, value|
|
14
|
-
return unless data_fields_mapping.has_key?(name.to_sym)
|
15
|
-
|
16
|
-
@entity.send(:"#{data_fields_mapping[name.to_sym]}=", value)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|