array_enum 1.0.1 → 1.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 +4 -4
- data/.travis.yml +1 -1
- data/Gemfile.lock +9 -9
- data/README.md +1 -0
- data/lib/array_enum.rb +10 -3
- data/lib/array_enum/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b351f8ef6ab75778ff9f71e7a9c1a67f409277dcaf644eb5a3d3d8ca1538e81
|
4
|
+
data.tar.gz: e2885b84818083a62aa7b65cbf9aa8be594400f417c0edaf2509cfd0a7cf32f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 206999b4a7e1598a9688cf5041d3a28192cb95cd54efbbf3a7f2ead8dba17f7e34d8e1a909a7a11ff8a3ed5112f5aa070bcebf7975c87ea545082e9bc7deb649
|
7
|
+
data.tar.gz: 0fe06f8ebf0b611b8f025db7d8d9971254413f92880cef4d1045f15a220d8d52c812141578fc8556e69d7e1a671c8bfb1e769d7fb8386e6c3552b6992a4afc46
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
array_enum (1.0
|
4
|
+
array_enum (1.1.0)
|
5
5
|
activemodel
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (5.2.
|
11
|
-
activesupport (= 5.2.
|
12
|
-
activerecord (5.2.
|
13
|
-
activemodel (= 5.2.
|
14
|
-
activesupport (= 5.2.
|
10
|
+
activemodel (5.2.3)
|
11
|
+
activesupport (= 5.2.3)
|
12
|
+
activerecord (5.2.3)
|
13
|
+
activemodel (= 5.2.3)
|
14
|
+
activesupport (= 5.2.3)
|
15
15
|
arel (>= 9.0)
|
16
|
-
activesupport (5.2.
|
16
|
+
activesupport (5.2.3)
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
18
|
i18n (>= 0.7, < 2)
|
19
19
|
minitest (~> 5.1)
|
20
20
|
tzinfo (~> 1.1)
|
21
21
|
arel (9.0.0)
|
22
|
-
concurrent-ruby (1.1.
|
23
|
-
i18n (1.
|
22
|
+
concurrent-ruby (1.1.5)
|
23
|
+
i18n (1.6.0)
|
24
24
|
concurrent-ruby (~> 1.0)
|
25
25
|
minitest (5.11.3)
|
26
26
|
pg (1.1.4)
|
data/README.md
CHANGED
data/lib/array_enum.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "array_enum/version"
|
2
2
|
require "array_enum/subset_validator"
|
3
3
|
require "array_enum/railtie" if defined?(Rails)
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
require "active_support/core_ext/string/inflections"
|
4
6
|
|
5
7
|
module ArrayEnum
|
6
8
|
MISSING_VALUE_MESSAGE = "%{value} is not a valid value for %{attr}".freeze
|
@@ -9,21 +11,26 @@ module ArrayEnum
|
|
9
11
|
def array_enum(definitions)
|
10
12
|
definitions.each do |attr_name, mapping|
|
11
13
|
attr_symbol = attr_name.to_sym
|
14
|
+
mapping_hash = ActiveSupport::HashWithIndifferentAccess.new(mapping)
|
15
|
+
|
16
|
+
define_singleton_method(attr_name.to_s.pluralize) do
|
17
|
+
mapping_hash
|
18
|
+
end
|
12
19
|
|
13
20
|
define_singleton_method("with_#{attr_name}".to_sym) do |values|
|
14
21
|
db_values = Array(values).map do |value|
|
15
|
-
|
22
|
+
mapping_hash[value] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
|
16
23
|
end
|
17
24
|
where("#{attr_name} @> ARRAY[:db_values]", db_values: db_values)
|
18
25
|
end
|
19
26
|
|
20
27
|
define_method(attr_symbol) do
|
21
|
-
Array(self[attr_symbol]).map { |value|
|
28
|
+
Array(self[attr_symbol]).map { |value| mapping_hash.key(value) }
|
22
29
|
end
|
23
30
|
|
24
31
|
define_method("#{attr_name}=".to_sym) do |values|
|
25
32
|
self[attr_symbol] = Array(values).map do |value|
|
26
|
-
|
33
|
+
mapping_hash[value] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
|
27
34
|
end.uniq
|
28
35
|
end
|
29
36
|
end
|
data/lib/array_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wojciech Wnętrzak
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
- !ruby/object:Gem::Version
|
139
139
|
version: '0'
|
140
140
|
requirements: []
|
141
|
-
rubygems_version: 3.0.
|
141
|
+
rubygems_version: 3.0.4
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: String to integer mapping for PostgreSQL array columns.
|