autopopulater 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +3 -0
- data/lib/autopopulater/autopopulated.rb +57 -0
- data/lib/autopopulater/railtie.rb +19 -0
- data/lib/autopopulater/version.rb +3 -0
- data/lib/autopopulater.rb +6 -0
- data/lib/tasks/autopopulater_tasks.rake +4 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e5f6e399d8be2ea9b66e2e209b31687209ea5d6788542c2748316613436e7fa8
|
4
|
+
data.tar.gz: 1311add23d473e713f41872b29dff88427fd6b9d90b18b9a3c0f64922ff69a69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7f866a4e89be97fdc9fbc4b989e1120db06e7aa01484295d27fda2a40f2d55add7c0fbcf657c04a7cbf1f01ad168f52e8fa3135ba2c8bc31fbfa6ad5a6ecca05
|
7
|
+
data.tar.gz: 91d7336bffee88cd0ec9d1a18b2247f106ef7a28dbbc92d47cff65ccea378ef414e41177fdc231b47d61a08d26ef810d1bed6aebe578c7b80d6483323d76f597
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright David Van Der Beek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Autopopulater
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "autopopulater"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install autopopulater
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
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,57 @@
|
|
1
|
+
module Autopopulater
|
2
|
+
module Autopopulated
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :autopopulated_attributes
|
7
|
+
end
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def autopopulates(*attributes, with: nil)
|
11
|
+
self.autopopulated_attributes ||= []
|
12
|
+
|
13
|
+
autopopulated_attributes << { keys: attributes, with: with }
|
14
|
+
|
15
|
+
attribute :autopopulated, :boolean, default: true
|
16
|
+
|
17
|
+
before_validation :autopopulate_attributes, on: :create, if: :autopopulated
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def autopopulate_attributes
|
22
|
+
self.class.autopopulated_attributes.each do |a|
|
23
|
+
value = fetch_value(a[:with])
|
24
|
+
|
25
|
+
a[:keys].each do |attr|
|
26
|
+
next unless send(attr).blank?
|
27
|
+
|
28
|
+
send("#{attr}=", attr_value(value, attr))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def fetch_value(method)
|
36
|
+
if method.respond_to?(:call)
|
37
|
+
method.call(self)
|
38
|
+
elsif method.is_a?(Symbol) || method.is_a?(String)
|
39
|
+
send(method)
|
40
|
+
else
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def attr_value(value, attr)
|
46
|
+
if value && value.respond_to?(attr)
|
47
|
+
value.send(attr)
|
48
|
+
elsif value && value.is_a?(Hash) && value.key?(attr)
|
49
|
+
value.with_indifferent_access[attr]
|
50
|
+
elsif value
|
51
|
+
value
|
52
|
+
else
|
53
|
+
send("fetch_#{attr}")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'autopopulater'
|
2
|
+
|
3
|
+
module Autopopulater
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
initializer 'autopopulater.insert_into_active_record' do |app|
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
Autopopulater::Railtie.insert
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Railtie
|
13
|
+
def self.insert
|
14
|
+
if defined?(ActiveRecord)
|
15
|
+
ActiveRecord::Base.send(:include, Autopopulater::Autopopulated)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: autopopulater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Van Der Beek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: debug
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 7.1.3.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 7.1.3.4
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- earlynovrock@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/autopopulater.rb
|
52
|
+
- lib/autopopulater/autopopulated.rb
|
53
|
+
- lib/autopopulater/railtie.rb
|
54
|
+
- lib/autopopulater/version.rb
|
55
|
+
- lib/tasks/autopopulater_tasks.rake
|
56
|
+
homepage: https://github.com/dvanderbeek/autopopulater
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata:
|
60
|
+
homepage_uri: https://github.com/dvanderbeek/autopopulater
|
61
|
+
source_code_uri: https://github.com/dvanderbeek/autopopulater
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubygems_version: 3.4.10
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Autopopulate Rails model attributes via remote APIs
|
81
|
+
test_files: []
|