dao-gateway-active_record 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10e40905c9322147fb4a8d3e5d9989ce118b7164
4
+ data.tar.gz: 3fce7ef0daaac24b884667469ecf37920375e302
5
+ SHA512:
6
+ metadata.gz: 5e9ec803e46f23b6eeb12656b8ade09baddf0549dcf1df9c161f7d26abe325e990fef248ebfc21c324897fe954989ac74d4e0104c2a9230c63d24f0a3ea9d134
7
+ data.tar.gz: 2e761bf8354b113b1034b54f9892b2f7cf1fe5b01caa35dbfa32a25794d76671b485d3903219bf6a0c22b4b1e89fe3e9344d5688724be030fe88bc81cb01d548
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ .ruby-gemset
12
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 llxff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Dao::Gateway::ActiveRecord
2
+
3
+ [![Build Status](https://travis-ci.org/dao-rb/dao-gateway-active_record.svg?branch=master)](https://travis-ci.org/dao-rb/dao-gateway-active_record)
4
+ [![Code Climate](https://codeclimate.com/github/dao-rb/dao-gateway-active_record/badges/gpa.svg)](https://codeclimate.com/github/dao-rb/dao-gateway-active_record)
5
+ [![Test Coverage](https://codeclimate.com/github/dao-rb/dao-gateway-active_record/badges/coverage.svg)](https://codeclimate.com/github/dao-rb/dao-gateway-active_record/coverage)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dao-gateway-active_record'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dao-gateway-active_record
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'dao/entity'
27
+ require 'dao/repository'
28
+ require 'dao/gateway/active_record'
29
+
30
+ class Comment < ApplicationRecord
31
+ end
32
+
33
+ class Post < ApplicationRecord
34
+ has_many :comments
35
+ end
36
+
37
+ class CommentEntity < Dao::Entity::Base
38
+ attribute :body, String
39
+ end
40
+
41
+ class PostEntity < Dao::Entity::Base
42
+ attribute :id, Integer
43
+ attribute :body, String
44
+
45
+ attribute :comments, Array[CommentEntity]
46
+ end
47
+
48
+ class PostRepository < Dao::Repository::Base
49
+ entity PostEntity
50
+ gateway Dao::Gateway::ActiveRecord::Base, Post, Dao::Gateway::ActiveRecord::BaseTransformer
51
+ end
52
+
53
+ post = PostRepository.last(with: :comments)
54
+
55
+ post.id # => 1
56
+ post.body # => "Post body"
57
+ post.comments # => [#<CommentEntity:0x007ffdcb923a30>]
58
+ ```
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dao-rb/dao-gateway-active_record.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dao/gateway/active_record"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'dao/gateway/active_record/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'dao-gateway-active_record'
7
+ spec.version = Dao::Gateway::ActiveRecord::VERSION
8
+ spec.authors = ['llxff', 'ssnikolay']
9
+ spec.email = ['ll.wg.bin@gmail.com']
10
+
11
+ spec.summary = 'ActiveRecord gateway for dao-repository'
12
+ spec.description = 'ActiveRecord gateway for dao-repository'
13
+ spec.homepage = 'https://github.com/dao-rb/dao-gateway-active_record'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 2.2.2'
22
+
23
+ spec.add_dependency 'dao-gateway', '~> 1.0'
24
+ spec.add_dependency 'activerecord', '>= 4.2'
25
+ spec.add_dependency 'activesupport', '>= 4.2'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.11'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.4'
30
+ spec.add_development_dependency 'rspec-its'
31
+ end
@@ -0,0 +1,16 @@
1
+ require 'dao/gateway'
2
+ require 'dao/gateway/active_record/version'
3
+
4
+ require 'active_record'
5
+ require 'active_support/hash_with_indifferent_access'
6
+ require 'active_support/rescuable'
7
+
8
+ require 'dao/gateway/active_record/base'
9
+ require 'dao/gateway/active_record/base_transformer'
10
+
11
+ module Dao
12
+ module Gateway
13
+ module ActiveRecord
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,94 @@
1
+ module Dao
2
+ module Gateway
3
+ module ActiveRecord
4
+ class Base < Dao::Gateway::Base
5
+ include ActiveSupport::Rescuable
6
+
7
+ def initialize(source, transformer)
8
+ super
9
+ @black_list_attributes += source_relations
10
+ end
11
+
12
+ def save!(domain, attributes)
13
+ record = export(domain, record(domain.id))
14
+ record.assign_attributes(attributes)
15
+ record.save!
16
+ domain.attributes = import(record, domain.initialized_with).attributes
17
+ domain
18
+ rescue ::ActiveRecord::RecordInvalid
19
+ raise Dao::Gateway::InvalidRecord.new(record.errors.to_hash)
20
+ rescue ::ActiveRecord::RecordNotFound => e
21
+ raise Dao::Gateway::RecordNotFound, e.message
22
+ rescue Exception => e
23
+ rescue_with_handler(e) || raise
24
+ end
25
+
26
+ def delete(domain_id)
27
+ record(domain_id).destroy if domain_id.present?
28
+ end
29
+
30
+ def chain(scope, method_name, args, &block)
31
+ scope.public_send(method_name, *args, &block)
32
+ rescue ::ActiveRecord::RecordNotFound => e
33
+ raise Dao::Gateway::RecordNotFound, e.message
34
+ end
35
+
36
+ def add_relations(scope, relations)
37
+ scope.eager_load(*relations)
38
+ end
39
+
40
+ def with_transaction(&block)
41
+ source.transaction(&block)
42
+ end
43
+
44
+ protected
45
+
46
+ def export(base, record = nil)
47
+ return unless base
48
+ record ||= source.new
49
+ attributes = base.attributes.except(*@black_list_attributes)
50
+
51
+ record.assign_attributes(attributes)
52
+ record
53
+ end
54
+
55
+ def import(relation, associations)
56
+ @transformer.associations = associations
57
+ unless relation.nil?
58
+ if collection_scope?(relation)
59
+ @transformer.many(relation)
60
+ elsif source_scope?(relation)
61
+ @transformer.one(relation)
62
+ else
63
+ @transformer.other(relation)
64
+ end
65
+ end
66
+ end
67
+
68
+ def record(domain_id)
69
+ source.find_by_id(domain_id) if domain_id.present?
70
+ end
71
+
72
+ def source_relations
73
+ @_relations ||= @source.reflections.keys.map(&:to_sym)
74
+ end
75
+
76
+ private
77
+
78
+ def collection_scope?(relation)
79
+ if relation.is_a?(::ActiveRecord::Relation)
80
+ true
81
+ elsif relation.is_a?(Array)
82
+ source_scope?(relation.first)
83
+ else
84
+ false
85
+ end
86
+ end
87
+
88
+ def source_scope?(relation)
89
+ relation.is_a?(source)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,24 @@
1
+ module Dao
2
+ module Gateway
3
+ module ActiveRecord
4
+ class BaseTransformer < Dao::Gateway::ScopeTransformer
5
+ def one(relation)
6
+ super(Array(relation))
7
+ end
8
+
9
+ private
10
+
11
+ def transform(relation)
12
+ super(relation) do |record|
13
+ collect_attributes(record, associations)
14
+ end
15
+ end
16
+
17
+ def collect_attributes(record, associations)
18
+ associations = associations.first if associations.count == 1
19
+ ::HashWithIndifferentAccess.new(record.try(:serializable_hash, force_except: [], include: associations))
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Dao
2
+ module Gateway
3
+ module ActiveRecord
4
+ VERSION = '1.0.0'
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dao-gateway-active_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - llxff
8
+ - ssnikolay
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dao-gateway
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '4.2'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '4.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activesupport
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '4.2'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '4.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.11'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.11'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '10.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '10.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.4'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.4'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec-its
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: ActiveRecord gateway for dao-repository
113
+ email:
114
+ - ll.wg.bin@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - dao-gateway-active_record.gemspec
129
+ - lib/dao/gateway/active_record.rb
130
+ - lib/dao/gateway/active_record/base.rb
131
+ - lib/dao/gateway/active_record/base_transformer.rb
132
+ - lib/dao/gateway/active_record/version.rb
133
+ homepage: https://github.com/dao-rb/dao-gateway-active_record
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 2.2.2
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: ActiveRecord gateway for dao-repository
157
+ test_files: []