insertion 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b1ddb71d7011145dc3a725a0eae1ceeccd8725a80446dc378e7cc3fcfdef0418
4
+ data.tar.gz: a1c8028e6816d6f9b10b10bfd2ad002c390800db37bc24f62387ca20ecf98c4d
5
+ SHA512:
6
+ metadata.gz: c8e343d8434780b3ab3d46a47d5e00995d2660f91fe8fbc4a32ba814021c87e4efa42e098f4ca6971f425ba4b8c7d7187a534c4fe7b3336d63381882a9e6b24e
7
+ data.tar.gz: 1839465cf1eef7c408773e5e7be44b6b5f139b71adaab3f05ed480d9d0fed11ae947fdf8185e0adb3ee8ef24aed3c53ce2902088ff8cb8e1183df5dcbcb6cc76
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Insertion
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/insertion`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joelmoss/insertion.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.configure do
4
+ config.autoload_paths << "#{root}/db/inserts"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Insertion
4
+ class BareInsert < Insert
5
+ def initialize(model, **)
6
+ @model = model
7
+ super(**)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Insertion
4
+ class Railtie < Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Insertion
4
+ class Insert
5
+ class Error < ::StandardError
6
+ def initialize(message = nil, class_name: nil)
7
+ @class_name = class_name
8
+ super(message)
9
+ end
10
+
11
+ def to_s
12
+ @class_name ? "(#{@class_name}) #{super}" : super
13
+ end
14
+ end
15
+
16
+ class ArgumentError < Error; end
17
+
18
+ def self.insert(model_name, *, **) = Insertion.insert(model_name, *, **)
19
+
20
+ def initialize(**attributes)
21
+ @attributes = attributes
22
+ end
23
+
24
+ def attributes
25
+ @attributes ||= {}
26
+ end
27
+
28
+ def do_insert!
29
+ result = model.insert!(attributes, returning: Arel.sql('*'))
30
+ record = model.instantiate result.to_a.first
31
+
32
+ after_insert(record) if respond_to?(:after_insert)
33
+
34
+ record
35
+ end
36
+
37
+ def build_insert!
38
+ result = nil
39
+ model.transaction do
40
+ result = model.insert!(attributes, returning: Arel.sql('*'))
41
+ raise ActiveRecord::Rollback
42
+ end
43
+
44
+ record = model.instantiate(result.to_a.first).dup
45
+
46
+ after_build(record) if respond_to?(:after_build)
47
+
48
+ record
49
+ end
50
+
51
+ private
52
+
53
+ def insert(model_name, *, **) = Insertion.insert(model_name, *, **)
54
+ def build(model_name, *, **) = Insertion.build(model_name, *, **)
55
+
56
+ def model
57
+ @model ||= self.class.name.sub(/Insert$/, '').constantize
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Insertion
4
+ VERSION = '0.1.0'
5
+ end
data/lib/insertion.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'insertion/version'
4
+ require_relative 'insertion/engine'
5
+
6
+ module Insertion
7
+ autoload :Insert, 'insertion/insert'
8
+ autoload :BareInsert, 'insertion/bare_insert'
9
+
10
+ module_function
11
+
12
+ def insert(model_name, *, **)
13
+ model_name = model_name.to_s.classify
14
+
15
+ if (insert = "#{model_name}Insert".safe_constantize)
16
+ begin
17
+ insert.new(*, **).do_insert!
18
+ rescue ArgumentError => e
19
+ raise insert::ArgumentError.new(e.message, class_name: insert), cause: e
20
+ end
21
+ else
22
+ BareInsert.new(model_name.constantize, *, **).do_insert!
23
+ end
24
+ end
25
+
26
+ def build(model_name, *, **)
27
+ model_name = model_name.to_s.classify
28
+
29
+ if (insert = "#{model_name}Insert".safe_constantize)
30
+ begin
31
+ insert.new(*, **).build_insert!
32
+ rescue ArgumentError => e
33
+ raise insert::ArgumentError.new(e.message, class_name: insert), cause: e
34
+ end
35
+ else
36
+ BareInsert.new(model_name.constantize, *, **).build_insert!
37
+ end
38
+ end
39
+ end
data/sig/insertion.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Insertion
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insertion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Moss
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - joel@developwithstyle.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - README.md
19
+ - Rakefile
20
+ - config/environments/test.rb
21
+ - lib/insertion.rb
22
+ - lib/insertion/bare_insert.rb
23
+ - lib/insertion/engine.rb
24
+ - lib/insertion/insert.rb
25
+ - lib/insertion/version.rb
26
+ - sig/insertion.rbs
27
+ homepage: https://github.com/joelmoss/insertion
28
+ licenses: []
29
+ metadata:
30
+ homepage_uri: https://github.com/joelmoss/insertion
31
+ source_code_uri: https://github.com/joelmoss/insertion
32
+ changelog_uri: https://github.com/joelmoss/insertion/releases
33
+ rubygems_mfa_required: 'true'
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.3.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 4.0.3
49
+ specification_version: 4
50
+ summary: PORO fixtures for Rails
51
+ test_files: []