dao-repository 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd3a81a66271ec727ae280da347726cf13b60ab1
4
+ data.tar.gz: 112f44697bc207fc1ce5078a09677ac0b10345b5
5
+ SHA512:
6
+ metadata.gz: f66caa7d4ce99d461fd89cbc91d7db949bb9cdde07c527c3ba230df571368299f0fce9ee0004b1e4c6923c21c78f1c179f74130be19a746469d9cd031de04a7f
7
+ data.tar.gz: 499da4d7d03883253fafe62c4238b37faaa14259789d6a3da30b30ce87c655ee38545a611aaf578a4049155d95d490c2f5f94a3fd7329d18ed192773ec93f2b4
@@ -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
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -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.
@@ -0,0 +1,74 @@
1
+ # Dao::Repository
2
+
3
+ [![Build Status](https://travis-ci.org/dao-rb/dao-repository.svg?branch=master)](https://travis-ci.org/dao-rb/dao-repository)
4
+ [![Code Climate](https://codeclimate.com/github/dao-rb/dao-repository/badges/gpa.svg)](https://codeclimate.com/github/dao-rb/dao-repository)
5
+ [![Test Coverage](https://codeclimate.com/github/dao-rb/dao-repository/badges/coverage.svg)](https://codeclimate.com/github/dao-rb/dao-repository/coverage)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dao-repository'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dao-repository
22
+
23
+ ## Usage
24
+
25
+ ```
26
+
27
+ require 'dao/entity'
28
+ require 'dao/repository'
29
+ require 'dao/gateway/active_record'
30
+
31
+ class Comment < ApplicationRecord
32
+ end
33
+
34
+ class Post < ApplicationRecord
35
+ has_many :comments
36
+ end
37
+
38
+ class CommentEntity < Dao::Entity::Base
39
+ attribute :body, String
40
+ end
41
+
42
+ class PostEntity < Dao::Entity::Base
43
+ attribute :id, Integer
44
+ attribute :body, String
45
+
46
+ attribute :comments, Array[CommentEntity]
47
+ end
48
+
49
+ class PostRepository < Dao::Repository::Base
50
+ entity PostEntity
51
+ gateway Dao::Gateway::ActiveRecord::Base, Post, Dao::Gateway::ActiveRecord::BaseTransformer
52
+ end
53
+
54
+ post = PostRepository.last(with: :comments)
55
+
56
+ post.id # => 1
57
+ post.body # => "Post body"
58
+ post.comments # => [#<CommentEntity:0x007ffdcb923a30>]
59
+ ```
60
+
61
+ ## Development
62
+
63
+ 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.
64
+
65
+ 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).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dao-rb/dao-repository.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dao/repository"
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
@@ -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,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'dao/repository/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'dao-repository'
7
+ spec.version = Dao::Repository::VERSION
8
+ spec.authors = %w(llxff ssnikolay)
9
+ spec.email = ['ll.wg.bin@gmail.com']
10
+
11
+ spec.summary = 'Base repository for DAO'
12
+ spec.description = 'Base repository for DAO'
13
+ spec.homepage = 'https://github.com/dao-rb/dao-repository'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '>= 2.2.2'
21
+
22
+ spec.add_dependency 'dao-gateway', '~> 1.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.4'
27
+ spec.add_development_dependency 'rspec-its'
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'dao/repository/version'
2
+ require 'dao/repository/base'
3
+ require 'dao/repository/scope'
4
+
5
+ module Dao
6
+ module Repository
7
+ end
8
+ end
@@ -0,0 +1,81 @@
1
+ module Dao
2
+ module Repository
3
+ class Base
4
+ class << self
5
+ def entity(entity)
6
+ @entity = entity
7
+ end
8
+
9
+ def gateway(gateway = nil, source = nil, transformer = nil)
10
+ if gateway && source && transformer
11
+ @gateway = gateway
12
+ @source = source
13
+ @transformer = transformer
14
+ else
15
+ @gateway.new(@source, @transformer.new(@entity))
16
+ end
17
+ end
18
+
19
+ def find(id, with: nil)
20
+ attributes = {}
21
+ attributes[:with] = with if with
22
+ scope.find(id, attributes).apply
23
+ end
24
+
25
+ def find_by_id(id)
26
+ scope.find_by_id(id).apply
27
+ end
28
+
29
+ def last(with: nil)
30
+ attributes = {}
31
+ attributes[:with] = with if with
32
+ scope.last(attributes).apply
33
+ end
34
+
35
+ def count
36
+ scope.count.apply
37
+ end
38
+
39
+ def build(record = nil)
40
+ gateway.map(record, {})
41
+ end
42
+
43
+ def save(domain, attributes = {})
44
+ gateway.save!(domain, attributes)
45
+ end
46
+
47
+ def save_all(domains)
48
+ with_transaction do
49
+ domains.map { |domain| save(domain) }
50
+ end
51
+ end
52
+
53
+ def delete(domain)
54
+ delete_by_id(domain.id) if domain
55
+ end
56
+
57
+ def delete_by_id(domain_id)
58
+ gateway.delete(domain_id)
59
+ end
60
+
61
+ def with_transaction(&block)
62
+ gateway.with_transaction(&block)
63
+ end
64
+
65
+ def by_criteria(criteria)
66
+ criteria.filter(scope).apply
67
+ end
68
+
69
+ def by_criteria_count(criteria)
70
+ criteria.count_of_result_items(scope).apply
71
+ end
72
+
73
+ protected
74
+
75
+ def scope
76
+ Scope.new(gateway)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,57 @@
1
+ module Dao
2
+ module Repository
3
+ class Scope
4
+ attr_accessor :gateway, :scope
5
+
6
+ def initialize(gateway)
7
+ @gateway = gateway
8
+ @scope = gateway.source
9
+ @with = []
10
+ end
11
+
12
+ def method_missing(method_name, *args, **options, &block)
13
+ with = extract_relations(options)
14
+ args << options if options.any?
15
+
16
+ add_relations(with) if with.any?
17
+
18
+ @scope = @gateway.chain(scope, method_name, args, &block)
19
+
20
+ self
21
+ end
22
+
23
+ def with(*args)
24
+ add_relations(args)
25
+
26
+ self
27
+ end
28
+
29
+ def apply
30
+ gateway.map(scope, @with)
31
+ end
32
+
33
+ def export_attributes_black_list
34
+ []
35
+ end
36
+
37
+ private
38
+
39
+ def add_relations(args)
40
+ @scope = @gateway.add_relations(scope, args)
41
+ @with += @gateway.serializable_relations(args)
42
+ end
43
+
44
+ def respond_to?(method_name, include_private = false)
45
+ scope.respond_to?(method_name, include_private) || super
46
+ end
47
+
48
+ def extract_relations(options)
49
+ if options[:with]
50
+ Array([options.delete(:with)]).flatten.compact
51
+ else
52
+ []
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ module Dao
2
+ module Repository
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dao-repository
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: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.11'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.11'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.4'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.4'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec-its
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Base repository for DAO
85
+ email:
86
+ - ll.wg.bin@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - dao-repository.gemspec
101
+ - lib/dao/repository.rb
102
+ - lib/dao/repository/base.rb
103
+ - lib/dao/repository/scope.rb
104
+ - lib/dao/repository/version.rb
105
+ homepage: https://github.com/dao-rb/dao-repository
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.2.2
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.5.1
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Base repository for DAO
129
+ test_files: []