array_attributes 0.0.1

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
+ SHA1:
3
+ metadata.gz: 91fd7dec868fb4904344f29ad018a588a8ddaf6b
4
+ data.tar.gz: 0a65945fe5b81e4d097b6cf471d7d44690109b76
5
+ SHA512:
6
+ metadata.gz: 2dcd05f05d9ac46b9bf61f82fdb7d7a30c9c0ad4db7f67eb97b35ed9ae00082bed48fe84562b87fbad0e7739d764d60dc6b311c4a607f3a2acf37bc68da372e1
7
+ data.tar.gz: 976908da05a51b7cc8dd18e614f3094128bbbbd6dc3a74a768444afc667a76728552af30f23f2334503cef0dec643b065d7d90e217019f633ce39b8f192d1fdc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 GoGrow
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 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,
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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # ArrayAttributes
2
+
3
+ A Rails plugin to encapsulate common array attributes methods.
4
+
5
+ This gem can be used to avoid repeating the same methods in models when working with arrays.
6
+
7
+ ## Support
8
+
9
+ **This branch targets Ruby 2.4+ only**
10
+
11
+
12
+ ## Usage
13
+
14
+ #### Install gem:
15
+
16
+ Add this line to your Gemfile:
17
+ ```ruby
18
+ gem 'array_attributes'
19
+ ```
20
+
21
+ then run:
22
+ ```shell
23
+ bundle install
24
+ ```
25
+
26
+ #### Enable ArrayAttributes
27
+
28
+ ```ruby
29
+ class Book < ActiveRecord::Base
30
+ string_array_attributes [:authors, :countries]
31
+ end
32
+ ```
33
+
34
+ It assumes you are passing string attributes.
35
+
36
+ It also supports the following options:
37
+
38
+ * `:reject_blank`: whether the attribute should skip blank values
39
+ * `:reject_if`: condition to reject values
40
+
41
+ ```ruby
42
+ class Device < ActiveRecord::Base
43
+ string_array_attributes [:ips], reject_blank: true, reject_if: ->(value) { value == '127.0.0.1' }
44
+ end
45
+ ```
46
+
47
+ ### attribute_raw
48
+
49
+ You can invoke `attribute_raw` to get the array values joined by commas.
50
+
51
+ ```ruby
52
+ book = Book.create(authors: ['John Doe', 'Jane Doe'])
53
+ book.authors_raw #=> "John Doe, Jane Doe"
54
+ ```
55
+
56
+ ### attribute_raw=
57
+
58
+ You can invoke `attribute_raw=` to set the array value by entering the values split by commas.
59
+
60
+ ```ruby
61
+ book = Book.new
62
+ book.authors_raw="John Doe, Jane Doe"
63
+ book.save!
64
+ book.authors #=> ["John Doe", "Jane Doe"]
65
+ ```
66
+
67
+ ### compact_attribute
68
+
69
+ You can invoke `compact_attribute` to remove blank items from the array attribute.
70
+
71
+ ```ruby
72
+ book = Book.new(authors: ['John Doe', 'Jane Doe', ''])
73
+ book.compact_authors
74
+ book.save!
75
+ book.authors #=> ["John Doe", "Jane Doe"]
76
+ ```
77
+
78
+ See `LICENSE`.
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArrayAttributes
4
+ module Concerns
5
+ module StringAttributes
6
+ def string_array_attributes(attrs, opts)
7
+ attrs.each do |attribute|
8
+ get_raw_value(attribute)
9
+ assign_raw_value(attribute, opts)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def get_raw_value(attribute)
16
+ define_method :"#{attribute}_raw" do
17
+ send(attribute)&.join(', ')
18
+ end
19
+ end
20
+
21
+ def assign_raw_value(attribute, opts)
22
+ define_method :"#{attribute}_raw=" do |values|
23
+ return unless values.present?
24
+
25
+ new_values = values.split(',').map(&:strip)
26
+
27
+ new_values.reject!(&:blank?) if opts[:reject_blank] == true
28
+
29
+ reject_if = opts[:reject_if]
30
+ new_values.reject! { |v| reject_if.call(v) } if reject_if.is_a?(Proc)
31
+
32
+ assign_attributes("#{attribute}": new_values)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArrayAttributes
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'array_attributes/version'
5
+ require 'array_attributes/concerns/string_attributes'
6
+
7
+ module ArrayAttributes
8
+ include ArrayAttributes::Concerns::StringAttributes
9
+ end
10
+
11
+ ActiveRecord::Base.extend ArrayAttributes
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - GoGrow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-15 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: '5.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rubocop
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: ArrayAttributes - Rails Concern to encapsulate common array attributes
48
+ methods
49
+ email: hello@gogrow.dev
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - LICENSE
55
+ - README.md
56
+ - lib/array_attributes.rb
57
+ - lib/array_attributes/concerns/string_attributes.rb
58
+ - lib/array_attributes/version.rb
59
+ homepage: https://github.com/gogrow-dev/array_attributes
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ rubygems_mfa_required: 'true'
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.4.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.6.8
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: ArrayAttributes - Rails Concern to encapsulate common array attributes methods
84
+ test_files: []