rom-factory 0.9.1 → 0.10.0

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
  SHA256:
3
- metadata.gz: 8ad9450271880c55bfc617af5dff975c71f76c2296a757c0c92c170056378589
4
- data.tar.gz: 2818b87189af66417f28fdb81760d5ecda7bc60901bb646366cdd526910e8f82
3
+ metadata.gz: 6d9ea536ecec5b699d9d75f0f738a4213922eaae03c7fca0f707934d7744f71a
4
+ data.tar.gz: bbf3688728b9d282bebeed47c38b083542af414d28c80adeefa5b33db103899c
5
5
  SHA512:
6
- metadata.gz: 80dea70f99c2fedcd7544395ae0692954411470b60181433a441e8070022eb14f168bdcd2789f214319794d3196d2e9f900d4d976fd2692e9c069730136e5911
7
- data.tar.gz: 452c37582fa999d0b053415e9022fc8b16993ff2e1acfb15c8ad4711eb09b37cd8b47166b1be3192b8495f7f62bfeed54fffc0ff07804ba12a419a466b112690
6
+ metadata.gz: 85246d5ba4663c72a9e34cd2145af5e905eb95f2ee288d9b9920e176298ebc0c8729f619aac1b6402db15c03cd4bf66268ba57182740bfeeaccc38e73c6cc5dc
7
+ data.tar.gz: 4d2c58b4bb729efce704e6bc62999cfc0662b9e90513e9569e8991fe8863d14feb4dd2b014dfbd65a5e9a20a15809547afd1b00df98042f132e4ad8f675d5887
data/.codeclimate.yml CHANGED
@@ -1,18 +1,11 @@
1
- version: "2"
1
+ # this file is managed by rom-rb/devtools project
2
2
 
3
- prepare:
4
- fetch:
5
- - url: "https://raw.githubusercontent.com/rom-rb/devtools/master/.rubocop.yml"
6
- path: ".rubocop.yml"
3
+ version: "2"
7
4
 
8
5
  exclude_patterns:
9
6
  - "benchmarks/"
10
7
  - "examples/"
11
8
  - "spec/"
12
- - "*/spec/"
13
- - "*/examples/"
14
- - "*/benchmarks/"
15
- - "!*.rb"
16
9
 
17
10
  plugins:
18
11
  rubocop:
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: "⚠️ Please don't ask for support via issues"
3
- about: "See CONTRIBUTING.md for more information"
3
+ about: See CONTRIBUTING.md for more information
4
4
  title: ''
5
5
  labels: ''
6
6
  assignees: ''
@@ -0,0 +1,34 @@
1
+ ---
2
+ name: "\U0001F41B Bug report"
3
+ about: See CONTRIBUTING.md for more information
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Before you submit this: WE ONLY ACCEPT BUG REPORTS AND FEATURE REQUESTS**
11
+
12
+ For more information see [our contribution guidelines](https://github.com/rom-rb/rom/blob/master/CONTRIBUTING.md)
13
+
14
+ **Before you report**
15
+
16
+ :warning: If you have a problem related to a schema, please **report it under [dry-schema issues](https://github.com/rom-rb/dry-schema/issues/new?assignees=&labels=bug&template=---bug-report.md&title=)** instead.
17
+
18
+ **Describe the bug**
19
+
20
+ A clear and concise description of what the bug is.
21
+
22
+ **To Reproduce**
23
+
24
+ Provide detailed steps to reproduce, an executable script would be best.
25
+
26
+ **Expected behavior**
27
+
28
+ A clear and concise description of what you expected to happen.
29
+
30
+ **Your environment**
31
+
32
+ - Affects my production application: **YES/NO**
33
+ - Ruby version: ...
34
+ - OS: ...
@@ -0,0 +1,18 @@
1
+ ---
2
+ name: "\U0001F6E0 Feature request"
3
+ about: See CONTRIBUTING.md for more information
4
+ title: ''
5
+ labels: feature
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ Summary of what the feature is supposed to do.
11
+
12
+ ## Examples
13
+
14
+ Code examples showing how the feature could be used.
15
+
16
+ ## Resources
17
+
18
+ Additional information, like a link to the discussion forum thread where the feature was discussed etc.
@@ -0,0 +1,110 @@
1
+ name: ci
2
+
3
+ on:
4
+ push:
5
+ paths:
6
+ - .github/workflows/ci.yml
7
+ - lib/**
8
+ - spec/**
9
+ - Gemfile
10
+ - "*.gemspec"
11
+ pull_request:
12
+ branches:
13
+ - master
14
+
15
+ jobs:
16
+ tests-mri:
17
+ runs-on: ubuntu-latest
18
+ services:
19
+ db:
20
+ image: postgres:10.8
21
+ env:
22
+ POSTGRES_USER: runner
23
+ POSTGRES_PASSWORD: ""
24
+ POSTGRES_DB: rom_factory
25
+ ports:
26
+ - 5432:5432
27
+ # needed because the postgres container does not provide a healthcheck
28
+ options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
29
+ strategy:
30
+ fail-fast: false
31
+ matrix:
32
+ ruby: ["2.6.x", "2.5.x", "2.4.x"]
33
+ include:
34
+ - ruby: "2.6.x"
35
+ coverage: "true"
36
+ steps:
37
+ - uses: actions/checkout@v1
38
+ - name: Install dependencies
39
+ run: sudo apt-get install -y --no-install-recommends libpq-dev
40
+ - name: Set up Ruby
41
+ uses: actions/setup-ruby@v1
42
+ with:
43
+ ruby-version: ${{matrix.ruby}}
44
+ - name: Install bundler
45
+ run: gem install bundler
46
+ - name: Bundle
47
+ run: bundle install --jobs 4 --retry 3 --without tools docs benchmarks
48
+ - name: Run all tests
49
+ env:
50
+ COVERAGE: ${{matrix.coverage}}
51
+ run: bundle exec rake
52
+ - name: Download test reporter
53
+ if: "matrix.coverage == 'true'"
54
+ run: |
55
+ mkdir -p tmp/
56
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/cc-test-reporter
57
+ chmod +x ./tmp/cc-test-reporter
58
+ ./tmp/cc-test-reporter before-build
59
+ - name: Send coverage results
60
+ if: "matrix.coverage == 'true'"
61
+ env:
62
+ CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
63
+ GIT_COMMIT_SHA: ${{github.sha}}
64
+ GIT_BRANCH: ${{github.ref}}
65
+ GIT_COMMITTED_AT: ${{github.event.head_commit.timestamp}}
66
+ run: |
67
+ GIT_BRANCH=`ruby -e "puts ENV['GITHUB_REF'].split('/', 3).last"` \
68
+ GIT_COMMITTED_AT=`ruby -r time -e "puts Time.iso8601(ENV['GIT_COMMITTED_AT']).to_i"` \
69
+ ./tmp/cc-test-reporter after-build
70
+
71
+ tests-others:
72
+ runs-on: ubuntu-latest
73
+ services:
74
+ db:
75
+ image: postgres:10.8
76
+ env:
77
+ POSTGRES_USER: root
78
+ POSTGRES_PASSWORD: ""
79
+ POSTGRES_DB: rom_factory
80
+ ports:
81
+ - 5432:5432
82
+ # needed because the postgres container does not provide a healthcheck
83
+ options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
84
+ strategy:
85
+ fail-fast: false
86
+ matrix:
87
+ image: ["jruby:9.2.9", "ruby:rc"]
88
+ include:
89
+ - image: "jruby:9.2.9"
90
+ database_url: "jdbc:postgresql://db/rom_factory"
91
+ - image: "ruby:rc"
92
+ database_url: "postgres://db/rom_factory"
93
+ container:
94
+ image: ${{matrix.image}}
95
+ steps:
96
+ - uses: actions/checkout@v1
97
+ - name: Install git
98
+ run: |
99
+ apt-get update
100
+ apt-get install -y --no-install-recommends git
101
+ - name: Install dependencies
102
+ run: apt-get install -y --no-install-recommends libpq-dev
103
+ - name: Install bundler
104
+ run: gem install bundler
105
+ - name: Bundle
106
+ run: bundle install --jobs 4 --retry 3 --without tools docs benchmarks
107
+ - name: Run all tests
108
+ run: bundle exec rake
109
+ env:
110
+ DATABASE_URL: ${{matrix.database_url}}
@@ -0,0 +1,34 @@
1
+ # this file is managed by rom-rb/devtools project
2
+
3
+ name: docsite
4
+
5
+ on:
6
+ push:
7
+ paths:
8
+ - docsite/**
9
+ - .github/workflows/docsite.yml
10
+ branches:
11
+ - master
12
+ - release-**
13
+ tags:
14
+
15
+ jobs:
16
+ update-docs:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@v1
20
+ - name: Set up Ruby
21
+ uses: actions/setup-ruby@v1
22
+ with:
23
+ ruby-version: "2.6.x"
24
+ - name: Install dependencies
25
+ run: |
26
+ gem install bundler
27
+ bundle install --jobs 4 --retry 3 --without benchmarks sql
28
+ - name: Symlink ossy
29
+ run: mkdir -p bin && ln -sf "$(bundle show ossy)/bin/ossy" bin/ossy
30
+ - name: Trigger rom-rb.org deploy
31
+ env:
32
+ GITHUB_LOGIN: ${{ github.actor }}
33
+ GITHUB_TOKEN: ${{ secrets.GH_PAT }}
34
+ run: bin/ossy github workflow rom-rb/rom-rb.org ci
@@ -0,0 +1,30 @@
1
+ # this file is managed by rom-rb/devtools project
2
+
3
+ name: sync_configs
4
+
5
+ on:
6
+ repository_dispatch:
7
+
8
+ jobs:
9
+ sync-configs:
10
+ runs-on: ubuntu-latest
11
+ if: github.event.action == 'sync_configs'
12
+ steps:
13
+ - uses: actions/checkout@v1
14
+ - name: Update configuration files from devtools
15
+ env:
16
+ GITHUB_LOGIN: ${{ github.actor }}
17
+ GITHUB_TOKEN: ${{ secrets.GH_PAT }}
18
+ run: |
19
+ git clone https://github.com/rom-rb/devtools.git tmp/devtools
20
+
21
+ rsync -av tmp/devtools/shared/ .
22
+
23
+ git config --local user.email "rom-bot@rom-rb.org"
24
+ git config --local user.name "rom-bot"
25
+ git add -A
26
+ git commit -m "[devtools] config sync" || echo "nothing changed"
27
+ - name: Push changes
28
+ uses: ad-m/github-push-action@master
29
+ with:
30
+ github_token: ${{ secrets.GH_PAT }}
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --color
2
+ --require spec_helper
2
3
  --order random
3
- --require ./spec/spec_helper
4
+
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## 0.10.0 2019-12-11
2
+
3
+
4
+ ### Added
5
+
6
+ * `struct_namespace` option is supported by factory builders (@graceful-potato)
7
+
8
+ ``` ruby
9
+ factories.define(:user, struct_namespace: MyApp::Entities) do |f|
10
+ # ...
11
+ end
12
+ ```
13
+
14
+ ### Fixed
15
+
16
+ * Support building structs when child assoc does not define parent (@psparrow)
17
+ * Fixed `TupleEvaluator#struct_attrs` for non-standard output schema (@AMHOL)
18
+
19
+ [Compare v0.9.1...v0.10.0](https://github.com/rom-rb/rom-factory/compare/v0.9.1...v0.10.0)
20
+
1
21
  ## 0.9.1 2019-10-23
2
22
 
3
23
  ### Fixed
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,49 +1,13 @@
1
1
  # Contributor Code of Conduct
2
2
 
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
7
4
 
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
12
6
 
13
- Examples of unacceptable behavior by participants include:
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
14
8
 
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
22
10
 
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
28
12
 
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at janjiss@gmail.com. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,29 @@
1
+ # Issue Guidelines
2
+
3
+ ## Reporting bugs
4
+
5
+ If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
6
+
7
+ ## Reporting feature requests
8
+
9
+ Report a feature request **only after discussing it first on [discourse.rom-rb.org](https://discourse.rom-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
10
+
11
+ ## Reporting questions, support requests, ideas, concerns etc.
12
+
13
+ **PLEASE DON'T** - use [discourse.rom-rb.org](http://discourse.rom-rb.org) instead.
14
+
15
+ # Pull Request Guidelines
16
+
17
+ A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
18
+
19
+ Other requirements:
20
+
21
+ 1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
22
+ 2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
+ 3) Add API documentation if it's a new feature
24
+ 4) Update API documentation if it changes an existing feature
25
+ 5) Bonus points for sending a PR to [github.com/rom-rb/rom-rb.org](github.com/rom-rb/rom-rb.org) which updates user documentation and guides
26
+
27
+ # Asking for help
28
+
29
+ If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.rom-rb.org](https://discourse.rom-rb.org) or join [our chat](https://rom-rb.zulipchat.com).
data/Gemfile CHANGED
@@ -19,11 +19,7 @@ group :test do
19
19
  gem 'simplecov'
20
20
 
21
21
  gem 'pg', '~> 0.21', platforms: [:mri, :truffleruby]
22
- gem 'mysql2', platforms: [:mri, :truffleruby]
23
22
  gem 'jdbc-postgres', platforms: :jruby
24
- gem 'jdbc-mysql', platforms: :jruby
25
- gem 'sqlite3', '~> 1.3', platforms: [:mri, :truffleruby]
26
- gem 'jdbc-sqlite3', platforms: :jruby
27
23
  end
28
24
 
29
25
  group :tools do
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2019 rom-rb team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  [gem]: https://rubygems.org/gems/rom-factory
2
- [travis]: https://travis-ci.com/rom-rb/rom-factory
2
+ [actions]: https://github.com/rom-rb/rom-factory/actions
3
3
  [codeclimate]: https://codeclimate.com/github/rom-rb/rom-factory
4
4
  [inchpages]: http://inch-ci.org/github/rom-rb/rom-factory
5
5
 
6
6
  # rom-factory
7
7
 
8
8
  [![Gem Version](https://badge.fury.io/rb/rom-factory.svg)][gem]
9
- [![Build Status](https://travis-ci.com/rom-rb/rom-factory.svg?branch=master)][travis]
9
+ [![CI Status](https://github.com/rom-rb/rom-factory/workflows/ci/badge.svg)][actions]
10
10
  [![Code Climate](https://codeclimate.com/github/rom-rb/rom-factory/badges/gpa.svg)][codeclimate]
11
11
  [![Test Coverage](https://codeclimate.com/github/rom-rb/rom-factory/badges/coverage.svg)][codeclimate]
12
12
  [![Inline docs](http://inch-ci.org/github/rom-rb/rom-factory.svg?branch=master)][inchpages]
@@ -0,0 +1,3 @@
1
+ # rom-factory
2
+
3
+ TODO: write docs :)
@@ -99,6 +99,9 @@ module ROM::Factory
99
99
  class OneToOne < OneToMany
100
100
  # @api private
101
101
  def call(attrs = EMPTY_HASH, parent, opts)
102
+ # do not associate if count is 0
103
+ return { name => nil } if count.zero?
104
+
102
105
  return if attrs.key?(name)
103
106
 
104
107
  association_hash = assoc.associate(attrs, parent)
@@ -114,6 +117,11 @@ module ROM::Factory
114
117
 
115
118
  { name => struct }
116
119
  end
120
+
121
+ # @api private
122
+ def count
123
+ options.fetch(:count, 1)
124
+ end
117
125
  end
118
126
  end
119
127
  end
@@ -26,6 +26,10 @@ module ROM::Factory
26
26
  # @return [ROM::Relation]
27
27
  option :relation, reader: false
28
28
 
29
+ # @!attribute [r] struct_namespace
30
+ # @return [Module] Custom struct namespace
31
+ option :struct_namespace, reader: false
32
+
29
33
  # @api private
30
34
  def tuple(*traits, **attrs)
31
35
  tuple_evaluator.defaults(traits, attrs)
@@ -33,24 +37,34 @@ module ROM::Factory
33
37
 
34
38
  # @api private
35
39
  def struct(*traits, **attrs)
36
- validate_keys(traits, attrs)
40
+ validate_keys(traits, attrs, allow_associations: true)
41
+
37
42
  tuple_evaluator.struct(*traits, attrs)
38
43
  end
39
44
  alias_method :create, :struct
40
45
 
41
46
  # @api private
42
47
  def struct_namespace(namespace)
43
- with(relation: relation.struct_namespace(namespace))
48
+ if options[:struct_namespace][:overridable]
49
+ with(struct_namespace: options[:struct_namespace].merge(namespace: namespace))
50
+ else
51
+ self
52
+ end
44
53
  end
45
54
 
46
55
  # @api private
47
- def persistable(struct_namespace = ROM::Struct)
48
- Persistable.new(self, relation.struct_namespace(struct_namespace))
56
+ def persistable
57
+ Persistable.new(self, relation)
49
58
  end
50
59
 
51
60
  # @api private
52
61
  def tuple_evaluator
53
- @__tuple_evaluator__ ||= TupleEvaluator.new(attributes, options[:relation], traits)
62
+ @__tuple_evaluator__ ||= TupleEvaluator.new(attributes, tuple_evaluator_relation, traits)
63
+ end
64
+
65
+ # @api private
66
+ def tuple_evaluator_relation
67
+ options[:relation].struct_namespace(options[:struct_namespace][:namespace])
54
68
  end
55
69
 
56
70
  # @api private
@@ -59,11 +73,13 @@ module ROM::Factory
59
73
  end
60
74
 
61
75
  # @api private
62
- def validate_keys(traits, tuple)
76
+ def validate_keys(traits, tuple, allow_associations: false)
63
77
  schema_keys = relation.schema.attributes.map(&:name)
64
78
  assoc_keys = tuple_evaluator.assoc_names(traits)
65
79
  unknown_keys = tuple.keys - schema_keys - assoc_keys
66
80
 
81
+ unknown_keys = unknown_keys - relation.schema.associations.to_h.keys if allow_associations
82
+
67
83
  raise UnknownFactoryAttributes, unknown_keys unless unknown_keys.empty?
68
84
  end
69
85
  end
@@ -27,14 +27,15 @@ module ROM
27
27
  class DSL < BasicObject
28
28
  define_method(:rand, ::Kernel.instance_method(:rand))
29
29
 
30
- attr_reader :_name, :_relation, :_attributes, :_factories, :_valid_names
30
+ attr_reader :_name, :_relation, :_attributes, :_factories, :_struct_namespace, :_valid_names
31
31
  attr_reader :_traits
32
32
 
33
33
  # @api private
34
- def initialize(name, attributes: AttributeRegistry.new, relation:, factories:)
34
+ def initialize(name, attributes: AttributeRegistry.new, relation:, factories:, struct_namespace:)
35
35
  @_name = name
36
36
  @_relation = relation
37
37
  @_factories = factories
38
+ @_struct_namespace = struct_namespace
38
39
  @_attributes = attributes.dup
39
40
  @_traits = {}
40
41
  @_valid_names = _relation.schema.attributes.map(&:name)
@@ -43,7 +44,7 @@ module ROM
43
44
 
44
45
  # @api private
45
46
  def call
46
- ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation)
47
+ ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation, struct_namespace: _struct_namespace)
47
48
  end
48
49
 
49
50
  # Delegate to a builder and persist a struct
@@ -110,6 +111,7 @@ module ROM
110
111
  ),
111
112
  relation: _relation,
112
113
  factories: _factories,
114
+ struct_namespace: _struct_namespace,
113
115
  &block
114
116
  )._attributes
115
117
  end
@@ -124,11 +126,16 @@ module ROM
124
126
  #
125
127
  # @param [Symbol] name The name of the configured association
126
128
  # @param [Hash] options Additional options
127
- # @option options [Integer] count Number of objects to generate (has-many only)
129
+ # @option options [Integer] count Number of objects to generate
128
130
  #
129
131
  # @api public
130
132
  def association(name, *traits, **options)
131
133
  assoc = _relation.associations[name]
134
+
135
+ if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && options.fetch(:count, 1) > 1
136
+ ::Kernel.raise ::ArgumentError, 'count cannot be greater than 1 on a OneToOne'
137
+ end
138
+
132
139
  builder = -> { _factories.for_relation(assoc.target) }
133
140
 
134
141
  _attributes << attributes::Association.new(assoc, builder, *traits, **options)
@@ -131,6 +131,8 @@ module ROM::Factory
131
131
  # @api public
132
132
  def define(spec, **opts, &block)
133
133
  name, parent = spec.is_a?(Hash) ? spec.flatten(1) : spec
134
+ namespace = opts[:struct_namespace]
135
+ relation_name = opts.fetch(:relation) { infer_relation(name) }
134
136
 
135
137
  if registry.key?(name)
136
138
  raise ArgumentError, "#{name.inspect} factory has been already defined"
@@ -138,11 +140,12 @@ module ROM::Factory
138
140
 
139
141
  builder =
140
142
  if parent
141
- extend_builder(name, registry[parent], &block)
143
+ extend_builder(name, registry[parent], relation_name, namespace, &block)
142
144
  else
143
- relation_name = opts.fetch(:relation) { infer_relation(name) }
144
145
  relation = rom.relations[relation_name]
145
- DSL.new(name, relation: relation.struct_namespace(struct_namespace), factories: self, &block).call
146
+ DSL.new(name, { relation: relation,
147
+ factories: self,
148
+ struct_namespace: builder_sturct_namespace(namespace) }, &block).call
146
149
  end
147
150
 
148
151
  registry[name] = builder
@@ -163,7 +166,7 @@ module ROM::Factory
163
166
  #
164
167
  # @api public
165
168
  def [](name, *traits, **attrs)
166
- registry[name].persistable(struct_namespace).create(*traits, attrs)
169
+ registry[name].struct_namespace(struct_namespace).persistable.create(*traits, attrs)
167
170
  end
168
171
 
169
172
  # Return in-memory struct builder
@@ -196,6 +199,11 @@ module ROM::Factory
196
199
  end
197
200
  end
198
201
 
202
+ # @api private
203
+ def builder_sturct_namespace(ns)
204
+ ns ? { namespace: ns, overridable: false } : { namespace: struct_namespace, overridable: true }
205
+ end
206
+
199
207
  # @api private
200
208
  def for_relation(relation)
201
209
  registry[infer_factory_name(relation.name.to_sym)]
@@ -212,8 +220,14 @@ module ROM::Factory
212
220
  end
213
221
 
214
222
  # @api private
215
- def extend_builder(name, parent, &block)
216
- DSL.new(name, attributes: parent.attributes, relation: parent.relation, factories: self, &block).call
223
+ def extend_builder(name, parent, relation_name, ns, &block)
224
+ namespace = parent.options[:struct_namespace]
225
+ namespace = builder_sturct_namespace(ns) if ns
226
+ relation = rom.relations.fetch(relation_name) { parent.relation }
227
+ DSL.new(name, { attributes: parent.attributes,
228
+ relation: relation,
229
+ factories: self,
230
+ struct_namespace: namespace }, &block).call
217
231
  end
218
232
  end
219
233
  end
@@ -136,11 +136,15 @@ module ROM
136
136
 
137
137
  # @api private
138
138
  def struct_attrs
139
- relation.schema.
139
+ struct_attrs = relation.schema.
140
140
  reject(&:primary_key?).
141
- map { |attr| [attr.name, nil] }.
142
- to_h.
143
- merge(primary_key => next_id)
141
+ map { |attr| [attr.name, nil] }.to_h
142
+
143
+ if primary_key
144
+ struct_attrs.merge(primary_key => next_id)
145
+ else
146
+ struct_attrs
147
+ end
144
148
  end
145
149
 
146
150
  # @api private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ROM
4
4
  module Factory
5
- VERSION = '0.9.1'
5
+ VERSION = '0.10.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janis Miezitis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-10-23 00:00:00.000000000 Z
12
+ date: 2019-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-configurable
@@ -79,17 +79,24 @@ files:
79
79
  - ".github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md"
80
80
  - ".github/ISSUE_TEMPLATE/----please-don-t-report-feature-requests-via-issues.md"
81
81
  - ".github/ISSUE_TEMPLATE/---a-detailed-bug-report.md"
82
+ - ".github/ISSUE_TEMPLATE/---bug-report.md"
83
+ - ".github/ISSUE_TEMPLATE/---feature-request.md"
84
+ - ".github/workflows/custom_ci.yml"
85
+ - ".github/workflows/docsite.yml"
86
+ - ".github/workflows/sync_configs.yml"
82
87
  - ".gitignore"
83
88
  - ".rspec"
84
- - ".travis.yml"
85
89
  - ".yardopts"
86
90
  - CHANGELOG.md
87
91
  - CODE_OF_CONDUCT.md
92
+ - CONTRIBUTING.md
88
93
  - Gemfile
94
+ - LICENSE
89
95
  - LICENSE.txt
90
96
  - README.md
91
97
  - Rakefile
92
98
  - benchmarks/basic.rb
99
+ - docsite/source/index.html.md
93
100
  - lib/rom-factory.rb
94
101
  - lib/rom/factory.rb
95
102
  - lib/rom/factory/attribute_registry.rb
data/.travis.yml DELETED
@@ -1,27 +0,0 @@
1
- language: ruby
2
-
3
- services:
4
- - postgresql
5
- - mysql
6
- bundler_args: --without tools benchmarks
7
- before_install:
8
- - gem install bundler
9
- before_script:
10
- - psql -c 'create database rom_factory;' -U postgres
11
- after_success:
12
- - '[ -d coverage ] && bundle exec codeclimate-test-reporter'
13
- rvm:
14
- - 2.6.3
15
- - 2.5.5
16
- - 2.4.6
17
- - jruby-9.2.6.0
18
- env:
19
- global:
20
- - COVERAGE='true'
21
- notifications:
22
- webhooks:
23
- urls:
24
- - https://webhooks.gitter.im/e/39e1225f489f38b0bd09
25
- on_success: change
26
- on_failure: always
27
- on_start: false