array_enum 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afc3f9133c5efe0de0320c4b83e650969ab9b647b29b242f2d44dfb58d2a726f
4
- data.tar.gz: 9a9ee4607992a52138f6fea70e0eb329554b50e675b37f8e700305b04ff161b9
3
+ metadata.gz: 2b351f8ef6ab75778ff9f71e7a9c1a67f409277dcaf644eb5a3d3d8ca1538e81
4
+ data.tar.gz: e2885b84818083a62aa7b65cbf9aa8be594400f417c0edaf2509cfd0a7cf32f1
5
5
  SHA512:
6
- metadata.gz: 3f96c19d50fac52b7bc2041d4983c9880807944226074132ce3c16b3a7ad99a1f00648f501d5535b8edaba1a932216ff0fb6196531dae7c9d09e3eb0bb06f6b9
7
- data.tar.gz: 43e71fc9ba6e47fd70b4fadad42d6b901d03f5e92238201e6e157338212db3d710ffcbffbb10fa8be0cf5be444a4c584de6c09d2272d42ace912d32087224321
6
+ metadata.gz: 206999b4a7e1598a9688cf5041d3a28192cb95cd54efbbf3a7f2ead8dba17f7e34d8e1a909a7a11ff8a3ed5112f5aa070bcebf7975c87ea545082e9bc7deb649
7
+ data.tar.gz: 0fe06f8ebf0b611b8f025db7d8d9971254413f92880cef4d1045f15a220d8d52c812141578fc8556e69d7e1a671c8bfb1e769d7fb8386e6c3552b6992a4afc46
data/.travis.yml CHANGED
@@ -2,7 +2,7 @@ dist: xenial
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.6.0
5
+ - 2.6.3
6
6
 
7
7
  addons:
8
8
  postgresql: "10"
data/Gemfile.lock CHANGED
@@ -1,26 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- array_enum (1.0.1)
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.2)
11
- activesupport (= 5.2.2)
12
- activerecord (5.2.2)
13
- activemodel (= 5.2.2)
14
- activesupport (= 5.2.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.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.4)
23
- i18n (1.5.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
@@ -28,6 +28,7 @@ end
28
28
 
29
29
  user = User.create!(favourite_colors: ["red", "green"])
30
30
  user.favourite_colors # => ["red", "green"]
31
+ User.favourite_colors # => {"red" => 1, "blue" => 2}
31
32
  User.with_favourite_colors("red") # => [user]
32
33
  ```
33
34
 
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
- mapping[value.to_s] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
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| mapping.key(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
- mapping[value.to_s] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
33
+ mapping_hash[value] || raise(ArgumentError, MISSING_VALUE_MESSAGE % {value: value, attr: attr_name})
27
34
  end.uniq
28
35
  end
29
36
  end
@@ -1,3 +1,3 @@
1
1
  module ArrayEnum
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
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.1
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-01-10 00:00:00.000000000 Z
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.2
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.