array_enum 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7bf65753b1960e7d732e9dbef138a7adb0f2873ea8ecd02a48623e837ce95fc2
4
+ data.tar.gz: 2adaa2064958a2d39933c1803070493db15e55602b8311b80255cea1d1cf5604
5
+ SHA512:
6
+ metadata.gz: e2a40dff60e27e34b11226ba300def97adcf6859ad6b6dfdb46539de8e7433b0b47db8a4a61cc727ff30bc091800d0e09c577d29065219c9e4a0f4298f4f7668
7
+ data.tar.gz: 9efc6ad57fd3faa33f80dfb087246e894cec0d54bf83cc87a39b6cc680a52df32e82e581454e33feb1dd68b8d0e65fe4f3b8b5ad65a31bd82244d1de29a4e967
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ dist: xenial
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.0
6
+
7
+ addons:
8
+ postgresql: "10"
9
+
10
+ before_script:
11
+ - psql -c 'create database "array_enum_test";' -U postgres
12
+
13
+ script: "bundle exec rake"
14
+
15
+ notifications:
16
+ disabled: true
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in array_enum.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ array_enum (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (5.2.2)
10
+ activesupport (= 5.2.2)
11
+ activerecord (5.2.2)
12
+ activemodel (= 5.2.2)
13
+ activesupport (= 5.2.2)
14
+ arel (>= 9.0)
15
+ activesupport (5.2.2)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ arel (9.0.0)
21
+ concurrent-ruby (1.1.4)
22
+ i18n (1.4.0)
23
+ concurrent-ruby (~> 1.0)
24
+ minitest (5.11.3)
25
+ pg (1.1.3)
26
+ rake (12.3.2)
27
+ thread_safe (0.3.6)
28
+ tzinfo (1.2.5)
29
+ thread_safe (~> 0.1)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ activerecord
36
+ array_enum!
37
+ bundler (>= 1.17)
38
+ minitest (>= 5.0)
39
+ pg
40
+ rake (>= 10.0)
41
+
42
+ BUNDLED WITH
43
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Wojciech Wnętrzak
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,46 @@
1
+ # ArrayEnum
2
+
3
+ Extension for `ActiveRecord` that adds support for `PostgreSQL` array columns, mapping string values to integers.
4
+
5
+ ## Installation
6
+
7
+ `gem install array_enum` or use `Gemfile` with bundler
8
+
9
+ ## Usage
10
+
11
+ Database will store integers that after reading will map to string values.
12
+ Additionally scope is generated with `with_` prefix that will query database for any matching passed value.
13
+
14
+ ```ruby
15
+ ActiveRecord::Schema.define do
16
+ create_table :users, force: true do |t|
17
+ t.integer :favourite_colors, array: true, null: false, default: []
18
+ end
19
+ end
20
+
21
+ class User < ActiveRecord::Base
22
+ extend ArrayEnum
23
+
24
+ array_enum favourite_colors: {"red" => 1, "blue" => 2}
25
+ end
26
+
27
+ user = User.create!(favourite_colors: ["red", "green"])
28
+ user.favourite_colors # => ["red", "green"]
29
+ User.with_favourite_colors("red") # => [user]
30
+ ```
31
+
32
+ ## Development
33
+
34
+ [![Build Status](https://travis-ci.com/freeletics/array_enum.svg?branch=master)](https://travis-ci.com/freeletics/array_enum)
35
+
36
+ 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.
37
+
38
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/freeletics/array_enum.
43
+
44
+ ## License
45
+
46
+ 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,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "array_enum/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "array_enum"
8
+ spec.version = ArrayEnum::VERSION
9
+ spec.authors = ["Wojciech Wnętrzak"]
10
+ spec.email = ["w.wnetrzak@gmail.com", "eng@freeletics.com"]
11
+
12
+ spec.summary = %q{String to integer mapping for PostgreSQL array columns.}
13
+ spec.description = %q{Extension for ActiveRecord that adds support for PostgreSQL array columns, mapping string values to integers.}
14
+ spec.homepage = "https://github.com/freeletics/array_enum"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/freeletics/array_enum"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "activerecord"
37
+ spec.add_development_dependency "pg"
38
+ spec.add_development_dependency "bundler", ">= 1.17"
39
+ spec.add_development_dependency "rake", ">= 10.0"
40
+ spec.add_development_dependency "minitest", ">= 5.0"
41
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "array_enum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module ArrayEnum
2
+ VERSION = "0.1.0"
3
+ end
data/lib/array_enum.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "array_enum/version"
2
+
3
+ module ArrayEnum
4
+ def array_enum(definitions)
5
+ definitions.each do |attr_name, mapping|
6
+ attr_symbol = attr_name.to_sym
7
+
8
+ define_singleton_method("with_#{attr_name}".to_sym) do |values|
9
+ db_values = Array(values).map { |value| mapping.fetch(value.to_s) }
10
+ where("#{attr_name} @> ARRAY[:db_values]", db_values: db_values)
11
+ end
12
+
13
+ define_method(attr_symbol) do
14
+ Array(self[attr_symbol]).map { |value| mapping.key(value) }
15
+ end
16
+
17
+ define_method("#{attr_name}=".to_sym) do |values|
18
+ self[attr_symbol] = Array(values).map { |value| mapping.fetch(value.to_s) }.uniq
19
+ end
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wojciech Wnętrzak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-02 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Extension for ActiveRecord that adds support for PostgreSQL array columns,
84
+ mapping string values to integers.
85
+ email:
86
+ - w.wnetrzak@gmail.com
87
+ - eng@freeletics.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - array_enum.gemspec
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/array_enum.rb
103
+ - lib/array_enum/version.rb
104
+ homepage: https://github.com/freeletics/array_enum
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://github.com/freeletics/array_enum
109
+ source_code_uri: https://github.com/freeletics/array_enum
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.0.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: String to integer mapping for PostgreSQL array columns.
129
+ test_files: []