active_record-associated_object 0.1.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
+ SHA256:
3
+ metadata.gz: '049c38f6aeb242c8ccfee9c15d3e29ace1c8f69eaf67619cdb38b09e5dd97514'
4
+ data.tar.gz: 911ce88d3257b0db22169f325ca2059b12ecdd8bb7121d7a37368588dce37262
5
+ SHA512:
6
+ metadata.gz: 0ee5964826318cb28a0a1d4c9e0a383b157e4ec0f2dd908b944c5eec68ddc7179d0d43be37bb65316c91e42318a6104b67eccd7d72194b99b4f3284312204692
7
+ data.tar.gz: 61df4cf5bc61dd0109650ce1aa95bc1f9e79fd99c2da6e2757429b71cda10b1423d6dda7f29310390fa0c5d01b0a5b77563de4151c277316e7a3e57edf16c699
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-04-19
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in active_record-associated_object.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "minitest", "~> 5.0"
10
+ gem "debug"
11
+
12
+ gem "sqlite3"
13
+
14
+ # Integrations to setup and test with.
15
+ gem "kredis"
16
+ gem "activejob"
17
+ gem "railties"
data/Gemfile.lock ADDED
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_record-associated_object (0.1.0)
5
+ activerecord (>= 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (7.0.2.3)
11
+ actionview (= 7.0.2.3)
12
+ activesupport (= 7.0.2.3)
13
+ rack (~> 2.0, >= 2.2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (7.0.2.3)
18
+ activesupport (= 7.0.2.3)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activejob (7.0.2.3)
24
+ activesupport (= 7.0.2.3)
25
+ globalid (>= 0.3.6)
26
+ activemodel (7.0.2.3)
27
+ activesupport (= 7.0.2.3)
28
+ activerecord (7.0.2.3)
29
+ activemodel (= 7.0.2.3)
30
+ activesupport (= 7.0.2.3)
31
+ activesupport (7.0.2.3)
32
+ concurrent-ruby (~> 1.0, >= 1.0.2)
33
+ i18n (>= 1.6, < 2)
34
+ minitest (>= 5.1)
35
+ tzinfo (~> 2.0)
36
+ builder (3.2.4)
37
+ concurrent-ruby (1.1.10)
38
+ crass (1.0.6)
39
+ debug (1.5.0)
40
+ irb (>= 1.3.6)
41
+ reline (>= 0.2.7)
42
+ erubi (1.10.0)
43
+ globalid (1.0.0)
44
+ activesupport (>= 5.0)
45
+ i18n (1.10.0)
46
+ concurrent-ruby (~> 1.0)
47
+ io-console (0.5.11)
48
+ irb (1.4.1)
49
+ reline (>= 0.3.0)
50
+ kredis (1.1.0)
51
+ activesupport (>= 6.0.0)
52
+ redis (~> 4.2)
53
+ loofah (2.16.0)
54
+ crass (~> 1.0.2)
55
+ nokogiri (>= 1.5.9)
56
+ method_source (1.0.0)
57
+ minitest (5.15.0)
58
+ nokogiri (1.13.4-arm64-darwin)
59
+ racc (~> 1.4)
60
+ nokogiri (1.13.4-x86_64-linux)
61
+ racc (~> 1.4)
62
+ racc (1.6.0)
63
+ rack (2.2.3)
64
+ rack-test (1.1.0)
65
+ rack (>= 1.0, < 3)
66
+ rails-dom-testing (2.0.3)
67
+ activesupport (>= 4.2.0)
68
+ nokogiri (>= 1.6)
69
+ rails-html-sanitizer (1.4.2)
70
+ loofah (~> 2.3)
71
+ railties (7.0.2.3)
72
+ actionpack (= 7.0.2.3)
73
+ activesupport (= 7.0.2.3)
74
+ method_source
75
+ rake (>= 12.2)
76
+ thor (~> 1.0)
77
+ zeitwerk (~> 2.5)
78
+ rake (13.0.6)
79
+ redis (4.6.0)
80
+ reline (0.3.1)
81
+ io-console (~> 0.5)
82
+ sqlite3 (1.4.2)
83
+ thor (1.2.1)
84
+ tzinfo (2.0.4)
85
+ concurrent-ruby (~> 1.0)
86
+ zeitwerk (2.5.4)
87
+
88
+ PLATFORMS
89
+ arm64-darwin-20
90
+ x86_64-linux
91
+
92
+ DEPENDENCIES
93
+ active_record-associated_object!
94
+ activejob
95
+ debug
96
+ kredis
97
+ minitest (~> 5.0)
98
+ railties
99
+ rake (~> 13.0)
100
+ sqlite3
101
+
102
+ BUNDLED WITH
103
+ 2.3.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Kasper Timm Hansen
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,63 @@
1
+ # ActiveRecord::AssociatedObject
2
+
3
+ Associate a Ruby PORO with an Active Record class and have it quack like one. Build and extend your domain model relying on the Active Record association to make it unique.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ # app/models/post.rb
9
+ class Post < ActiveRecord::Base
10
+ end
11
+
12
+ # Create a standard PORO, but derive attributes from the Post:: namespace and its primary key.
13
+ # app/models/post/publisher.rb
14
+ class Post::Publisher < ActiveRecord::AssociatedObject
15
+ kredis_datetime :publish_at # Kredis integration generates a "post:publishers:<post_id>:publish_at" key.
16
+
17
+ # Both a general `record` method and a `post` method is available to access the associated post.
18
+ def publish_now
19
+ transaction do
20
+ post.update! published: true
21
+ post.subscribers.post_published post
22
+ end
23
+ end
24
+
25
+ def publish_later
26
+ PublishJob.set(wait_until: publish_at).perform_later self
27
+ end
28
+ end
29
+
30
+ # Post now has a publisher too.
31
+ Post.first.publisher # => Post::Publisher.new(self) # Where self is Post.first.
32
+
33
+ class Post::Publisher::PublishJob < ActiveJob::Base
34
+ def perform(publisher)
35
+ # Automatic integration via GlobalID means you don't have to do `post.publisher`.
36
+ publisher.publish_now
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Install the gem and add to the application's Gemfile by executing:
44
+
45
+ $ bundle add active_record-associated_object
46
+
47
+ If bundler is not being used to manage dependencies, install the gem by executing:
48
+
49
+ $ gem install active_record-associated_object
50
+
51
+ ## Development
52
+
53
+ 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.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record-associated_object.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/active_record/associated_object/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_record-associated_object"
7
+ spec.version = ActiveRecord::AssociatedObject::VERSION
8
+ spec.authors = ["Kasper Timm Hansen"]
9
+ spec.email = ["hey@kaspth.com"]
10
+
11
+ spec.summary = "Associate a Ruby PORO with an Active Record class and have it quack like one."
12
+ spec.homepage = "https://github.com/kaspth/active_record-associated_object"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "activerecord", ">= 6.1"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,6 @@
1
+ class ActiveRecord::AssociatedObject::Railtie < Rails::Railtie
2
+ initializer "integrations.include" do
3
+ ActiveRecord::AssociatedObject.include Kredis::Attributes if defined?(Kredis)
4
+ ActiveRecord::AssociatedObject.include GlobalID::Identification if defined?(GlobalID)
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class AssociatedObject
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ class ActiveRecord::AssociatedObject
2
+ class << self
3
+ def inherited(klass)
4
+ record_klass = klass.module_parent
5
+ record_name = klass.module_parent_name.underscore
6
+ attribute_name = klass.to_s.demodulize.underscore.to_sym
7
+
8
+ unless record_klass.respond_to?(:descends_from_active_record?) && record_klass.descends_from_active_record?
9
+ raise ArgumentError, "#{record_klass} isn't valid; can only associate with ActiveRecord::Base subclasses"
10
+ end
11
+
12
+ alias_method record_name, :record
13
+ define_singleton_method(:record_klass) { record_klass }
14
+ define_singleton_method(:attribute_name) { attribute_name }
15
+ delegate :record_klass, :attribute_name, to: :class
16
+
17
+ record_klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
18
+ def #{attribute_name}
19
+ @#{attribute_name} ||= #{klass}.new(self)
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ def respond_to_missing?(...) = record_klass.respond_to?(...) || super
25
+ delegate :unscoped, :transaction, to: :record_klass
26
+
27
+ def method_missing(method, ...)
28
+ if !record_klass.respond_to?(method) then super else
29
+ record_klass.public_send(method, ...).then do |value|
30
+ value.respond_to?(:each) ? value.map(&attribute_name) : value&.public_send(attribute_name)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ attr_reader :record
37
+ delegate :id, :transaction, to: :record
38
+
39
+ def initialize(record)
40
+ @record = record
41
+ end
42
+
43
+ def ==(other)
44
+ other.is_a?(self.class) && id == other.id
45
+ end
46
+ end
47
+
48
+ require_relative "associated_object/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-associated_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ description:
28
+ email:
29
+ - hey@kaspth.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - active_record-associated_object.gemspec
41
+ - lib/active_record/associated_object.rb
42
+ - lib/active_record/associated_object/railtie.rb
43
+ - lib/active_record/associated_object/version.rb
44
+ homepage: https://github.com/kaspth/active_record-associated_object
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/kaspth/active_record-associated_object
49
+ source_code_uri: https://github.com/kaspth/active_record-associated_object
50
+ changelog_uri: https://github.com/kaspth/active_record-associated_object/blob/main/CHANGELOG.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.7.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.11
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Associate a Ruby PORO with an Active Record class and have it quack like
70
+ one.
71
+ test_files: []