jsonb_accessor 1.3.0 → 1.3.1

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
- SHA1:
3
- metadata.gz: 12a9ef2a6ecf012dd86338b0962ea0338ac8950f
4
- data.tar.gz: ffb75c6ce59fa8eeb1a6917c822853f0aa63cead
2
+ SHA256:
3
+ metadata.gz: e0206aa16a6170326c1c5683a894715d6466cce4fb4b53f0bbd4326619608aed
4
+ data.tar.gz: e806fe994b60d0e9a4f8d46bda8df39a44266292010f2b4706bd2be3dfe46d44
5
5
  SHA512:
6
- metadata.gz: ddb76f1639699f471680602e9d3f7601eca9666e7c3c03d2f3dce80d8e810a22940712668e0190f2469bccb54f9ae10a7f0b2fb7e763ea8818da32c34893ea91
7
- data.tar.gz: 11e77edee1f60874eb313da3301483beb5cc4a962ad14b8f202e923511a1378a10b664fcb00fb4eae161e06f73b4f3c20878871753a693cc5fd733665a0dd2c4
6
+ metadata.gz: 443182674b5fd917ea91c8a7813756e43b85678343833845db054a4814ccbb9e20598794c2ee44ad44715258a5e957a1fe2cbcdbf2758b2a01200adf266c7381
7
+ data.tar.gz: 7f604bebd874c410c78e554c4e724f18d0c816fe035ebc1d8e470cede3d8d3497f9443a4a25a4d611f0395eb5215063e211aada8ef9d8df8e4ee7502b352eb81
data/.rubocop.yml CHANGED
@@ -1,11 +1,16 @@
1
1
  AllCops:
2
- Include:
3
- - "**/Rakefile"
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.7.2
4
+ SuggestExtensions: false
4
5
  Exclude:
5
- - db/**/*
6
- - gemfiles/**/*
7
- - vendor/**/*
8
- Style/SpaceBeforeFirstArg:
6
+ - "db/**/*"
7
+ - "gemfiles/**/*"
8
+ - "vendor/**/*"
9
+ Layout/SpaceBeforeFirstArg:
10
+ Enabled: false
11
+ Layout/LineLength:
12
+ Enabled: false
13
+ Layout/SpaceAroundEqualsInParameterDefault:
9
14
  Enabled: false
10
15
  Lint/UnusedBlockArgument:
11
16
  Enabled: false
@@ -17,8 +22,6 @@ Metrics/ClassLength:
17
22
  Enabled: false
18
23
  Metrics/CyclomaticComplexity:
19
24
  Enabled: false
20
- Metrics/LineLength:
21
- Enabled: false
22
25
  Metrics/MethodLength:
23
26
  Enabled: false
24
27
  Metrics/ModuleLength:
@@ -29,8 +32,6 @@ Metrics/BlockLength:
29
32
  Enabled: false
30
33
  Security/YAMLLoad:
31
34
  Enabled: false
32
- Style/AlignParameters:
33
- Enabled: false
34
35
  Style/ClassAndModuleChildren:
35
36
  Enabled: false
36
37
  Style/ClassVars:
@@ -39,25 +40,23 @@ Style/Documentation:
39
40
  Enabled: false
40
41
  Style/DoubleNegation:
41
42
  Enabled: false
42
- Style/FileName:
43
+ Naming/FileName:
43
44
  Enabled: false
44
45
  Style/GuardClause:
45
46
  Enabled: false
46
- Style/IndentHash:
47
- Enabled: false
48
47
  Style/NilComparison:
49
48
  Enabled: false
50
- Style/OpMethod:
51
- Enabled: false
52
49
  Style/RescueModifier:
53
50
  Enabled: false
54
51
  Style/SignalException:
55
52
  Enabled: false
56
53
  Style/SingleLineMethods:
57
54
  Enabled: false
58
- Style/SpaceAroundEqualsInParameterDefault:
59
- Enabled: false
60
55
  Style/StringLiterals:
61
56
  EnforcedStyle: double_quotes
62
- Performance/EndWith:
57
+ Naming/BinaryOperatorParameterName:
58
+ Enabled: false
59
+ Naming/VariableNumber:
60
+ Enabled: false
61
+ Gemspec/RequiredRubyVersion:
63
62
  Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4.1
1
+ 2.7.2
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.8
4
3
  - 2.4.5
5
4
  - 2.5.3
6
5
  - 2.6.1
@@ -23,8 +22,6 @@ gemfile:
23
22
  - gemfiles/activerecord_6.1.0.gemfile
24
23
  jobs:
25
24
  exclude:
26
- - rvm: 2.3.8
27
- gemfile: gemfiles/activerecord_6.1.0.gemfile
28
25
  - rvm: 2.4.5
29
26
  gemfile: gemfiles/activerecord_6.1.0.gemfile
30
27
  - rvm: 3.0.0
data/Dockerfile ADDED
@@ -0,0 +1,12 @@
1
+ ARG RUBY_VERSION=latest
2
+ FROM ruby:${RUBY_VERSION}
3
+
4
+ WORKDIR /usr/src/app
5
+
6
+ RUN mkdir -p lib/jsonb_accessor
7
+ COPY lib/jsonb_accessor/version.rb ./lib/jsonb_accessor/
8
+ COPY *.gemspec Gemfile* ./
9
+
10
+ RUN bundle install
11
+
12
+ COPY . .
data/README.md CHANGED
@@ -10,15 +10,15 @@ It also adds generic scopes for querying `jsonb` columns.
10
10
 
11
11
  ## Table of Contents
12
12
 
13
- * [Installation](#installation)
14
- * [Usage](#usage)
15
- * [Scopes](#scopes)
16
- * [Single-Table Inheritance](#single-table-inheritance)
17
- * [Dependencies](#dependencies)
18
- * [Validations](#validations)
19
- * [Upgrading](#upgrading)
20
- * [Development](#development)
21
- * [Contributing](#contributing)
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [Scopes](#scopes)
16
+ - [Single-Table Inheritance](#single-table-inheritance)
17
+ - [Dependencies](#dependencies)
18
+ - [Validations](#validations)
19
+ - [Upgrading](#upgrading)
20
+ - [Development](#development)
21
+ - [Contributing](#contributing)
22
22
 
23
23
  ## Installation
24
24
 
@@ -158,6 +158,7 @@ Product.all.jsonb_where(:data, reviewed_at: { before: Time.current }, p: { great
158
158
 
159
159
  Product.all.data_where(reviewed_at: { before: Time.current }, price: { greater_than: 5 })
160
160
  ```
161
+
161
162
  This scope makes use of the `jsonb_contains`, `jsonb_number_where`, and `jsonb_time_where` `scope`s.
162
163
 
163
164
  ### `jsonb_where_not`
@@ -219,14 +220,14 @@ Product.all.jsonb_number_where(:data, :price_in_cents, :greater_than, 300)
219
220
 
220
221
  It supports:
221
222
 
222
- * `>`
223
- * `>=`
224
- * `greater_than`
225
- * `greater_than_or_equal_to`
226
- * `<`
227
- * `<=`
228
- * `less_than`
229
- * `less_than_or_equal_to`
223
+ - `>`
224
+ - `>=`
225
+ - `greater_than`
226
+ - `greater_than_or_equal_to`
227
+ - `<`
228
+ - `<=`
229
+ - `less_than`
230
+ - `less_than_or_equal_to`
230
231
 
231
232
  and it is indifferent to strings/symbols.
232
233
 
@@ -259,9 +260,9 @@ Product.all.jsonb_time_where_not(:data, :reviewed_at, :before, 2.days.ago)
259
260
  ## Single-Table Inheritance
260
261
 
261
262
  One of the big issues with `ActiveRecord` single-table inheritance (STI)
262
- is sparse columns. Essentially, as sub-types of the original table
263
+ is sparse columns. Essentially, as sub-types of the original table
263
264
  diverge further from their parent more columns are left empty in a given
264
- table. Postgres' `jsonb` type provides part of the solution in that
265
+ table. Postgres' `jsonb` type provides part of the solution in that
265
266
  the values in an `jsonb` column does not impose a structure - different
266
267
  rows can have different values.
267
268
 
@@ -305,7 +306,7 @@ end
305
306
  ```
306
307
 
307
308
  From here any attributes specific to any sub-class can be stored in the
308
- `jsonb` column avoiding sparse data. Indices can also be created on
309
+ `jsonb` column avoiding sparse data. Indices can also be created on
309
310
  individual fields in an `jsonb` column.
310
311
 
311
312
  This approach was originally conceived by Joe Hirn in [this blog
@@ -326,12 +327,24 @@ See the [upgrade guide](UPGRADE_GUIDE.md).
326
327
 
327
328
  ## Development
328
329
 
330
+ ### On your local machine
331
+
329
332
  After checking out the repo, run `bin/setup` to install dependencies (make sure postgres is running first).
330
333
 
331
334
  Run `bin/console` for an interactive prompt that will allow you to experiment.
332
335
 
333
336
  `rake` will run Rubocop and the specs.
334
337
 
338
+ ### With Docker
339
+
340
+ ```
341
+ # setup
342
+ docker-compose build
343
+ docker-compose run ruby rake db:migrate
344
+ # run test suite
345
+ docker-compose run ruby rake spec
346
+ ```
347
+
335
348
  ## Contributing
336
349
 
337
350
  1. [Fork it](https://github.com/madeintandem/jsonb_accessor/fork)
data/Rakefile CHANGED
@@ -5,18 +5,21 @@ require "bundler/setup"
5
5
  require "bundler/gem_tasks"
6
6
  require "rspec/core/rake_task"
7
7
  require "rubocop/rake_task"
8
+ require "active_record"
9
+ require "erb"
8
10
 
9
11
  RSpec::Core::RakeTask.new
10
12
  RuboCop::RakeTask.new
11
13
 
12
- require "active_record"
14
+ # rubocop:disable Style/MixinUsage
13
15
  include ActiveRecord::Tasks
16
+ # rubocop:enable Style/MixinUsage
14
17
 
15
- root = File.expand_path "..", __FILE__
18
+ root = File.expand_path __dir__
16
19
  db_dir = File.join(root, "db")
17
20
  DatabaseTasks.root = root
18
21
  DatabaseTasks.db_dir = db_dir
19
- DatabaseTasks.database_configuration = YAML.load(File.read(File.join(db_dir, "config.yml")))
22
+ DatabaseTasks.database_configuration = YAML.load(ERB.new(File.read(File.join(db_dir, "config.yml"))).result)
20
23
  DatabaseTasks.migrations_paths = [File.join(db_dir, "migrate")]
21
24
  DatabaseTasks.env = "test"
22
25
 
data/bin/console CHANGED
@@ -1,16 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- # rubocop:disable Lint/UselessAssignment
5
-
6
4
  require "bundler/setup"
7
5
  require "jsonb_accessor"
8
6
  require "rspec"
9
- require File.expand_path("../../spec/spec_helper.rb", __FILE__)
7
+ require File.expand_path("../spec/spec_helper.rb", __dir__)
10
8
 
11
9
  dbconfig = YAML.load(File.open("db/config.yml"))
12
10
  ActiveRecord::Base.establish_connection(dbconfig["development"])
13
11
 
12
+ # rubocop:disable Lint/UselessAssignment
14
13
  x = Product.new
14
+ # rubocop:enable Lint/UselessAssignment
15
15
 
16
16
  Pry.start
data/db/config.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  default: &default
2
2
  adapter: postgresql
3
3
  database: jsonb_accessor
4
- host: 127.0.0.1
4
+ host: <%= ENV.fetch("DATABASE_HOST") { "127.0.0.1" } %>
5
5
  user: postgres
6
6
 
7
7
  test:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SetUpTestingDb < ActiveRecord::Migration[5.0]
2
4
  def change
3
5
  create_table :products do |t|
data/db/schema.rb CHANGED
@@ -1,17 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is auto-generated from the current state of the database. Instead
2
4
  # of editing this file, please use the migrations feature of Active Record to
3
5
  # incrementally modify your database, and then regenerate this schema definition.
4
6
  #
5
- # This file is the source Rails uses to define your schema when running `rails
6
- # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
7
- # be faster and is potentially less error prone than running all of your
8
- # migrations from scratch. Old migrations may fail to apply correctly if those
9
- # migrations use external dependencies or application code.
7
+ # Note that this schema.rb definition is the authoritative source for your
8
+ # database schema. If you need to create the application database on another
9
+ # system, you should be using db:schema:load, not running all the migrations
10
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
11
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
12
  #
11
13
  # It's strongly recommended that you check this file into your version control system.
12
14
 
13
- ActiveRecord::Schema.define(version: 2015_04_07_031737) do
14
-
15
+ ActiveRecord::Schema.define(version: 20_150_407_031_737) do
15
16
  # These are extensions that must be enabled in order to support this database
16
17
  enable_extension "plpgsql"
17
18
 
@@ -32,5 +33,4 @@ ActiveRecord::Schema.define(version: 2015_04_07_031737) do
32
33
  t.datetime "datetime_type"
33
34
  t.decimal "decimal_type"
34
35
  end
35
-
36
36
  end
@@ -0,0 +1,26 @@
1
+ version: '3'
2
+
3
+ services:
4
+ ruby:
5
+ environment:
6
+ - DATABASE_HOST=postgres
7
+ build:
8
+ args:
9
+ - RUBY_VERSION=${RUBY_VERSION:-2.7.2}
10
+ context: .
11
+ volumes:
12
+ - '.:/usr/src/app'
13
+ depends_on:
14
+ - postgres
15
+
16
+ postgres:
17
+ image: postgres:12
18
+ environment:
19
+ - POSTGRES_HOST_AUTH_METHOD=trust
20
+ - POSTGRES_DB=jsonb_accessor
21
+ - PGDATA=/var/lib/postgresql/data/pgdata
22
+ volumes:
23
+ - pg_data:/var/lib/postgresql/data/pgdata
24
+
25
+ volumes:
26
+ pg_data:
@@ -1,7 +1,6 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path("lib", __dir__)
5
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
5
  require "jsonb_accessor/version"
7
6
 
@@ -27,12 +26,12 @@ Gem::Specification.new do |spec|
27
26
  spec.add_dependency "pg", ">= 0.18.1"
28
27
 
29
28
  spec.add_development_dependency "appraisal", "~> 2.2.0"
30
- spec.add_development_dependency "database_cleaner", "~> 1.6.0"
31
29
  spec.add_development_dependency "awesome_print"
30
+ spec.add_development_dependency "database_cleaner", "~> 1.6.0"
32
31
  spec.add_development_dependency "pry"
33
32
  spec.add_development_dependency "pry-doc"
34
33
  spec.add_development_dependency "pry-nav"
35
34
  spec.add_development_dependency "rake", ">= 12.3.3"
36
35
  spec.add_development_dependency "rspec", "~> 3.6.0"
37
- spec.add_development_dependency "rubocop", "~> 0.48.1"
36
+ spec.add_development_dependency "rubocop", "~> 1"
38
37
  end
@@ -15,6 +15,6 @@ module JsonbAccessor
15
15
  end
16
16
 
17
17
  ActiveSupport.on_load(:active_record) do
18
- ActiveRecord::Base.send(:include, JsonbAccessor)
19
- ActiveRecord::Base.send(:include, JsonbAccessor::QueryBuilder)
18
+ ActiveRecord::Base.include JsonbAccessor
19
+ ActiveRecord::Base.include JsonbAccessor::QueryBuilder
20
20
  end
@@ -55,7 +55,7 @@ module JsonbAccessor
55
55
  # each time it is evaluated.
56
56
  all_defaults_mapping_proc =
57
57
  if all_defaults_mapping.present?
58
- -> { all_defaults_mapping.map { |key, value| [key, value.respond_to?(:call) ? value.call : value] }.to_h }
58
+ -> { all_defaults_mapping.transform_values { |value| value.respond_to?(:call) ? value.call : value }.to_h.compact }
59
59
  end
60
60
  attribute jsonb_attribute, :jsonb, default: all_defaults_mapping_proc if all_defaults_mapping_proc.present?
61
61
 
@@ -79,7 +79,7 @@ module JsonbAccessor
79
79
  empty_named_attributes = names_to_store_keys.keys.each_with_object({}) { |name, defaults| defaults[name] = nil }
80
80
 
81
81
  store_key_attributes = ::JsonbAccessor::QueryHelper.convert_keys_to_store_keys(value, names_to_store_keys)
82
- write_attribute(jsonb_attribute, empty_store_key_attributes.merge(store_key_attributes))
82
+ write_attribute(jsonb_attribute, empty_store_key_attributes.merge(store_key_attributes).compact)
83
83
 
84
84
  empty_named_attributes.merge(value).each { |name, attribute_value| write_attribute(name, attribute_value) }
85
85
  end
@@ -61,9 +61,7 @@ module JsonbAccessor
61
61
  excludes_attributes = {}
62
62
 
63
63
  attributes.each do |name, value|
64
- if value.is_a?(Range)
65
- raise JsonbAccessor::QueryHelper::NotSupported, "`jsonb_where_not` scope does not accept ranges as arguments. Given `#{value}` for `#{name}` field"
66
- end
64
+ raise JsonbAccessor::QueryHelper::NotSupported, "`jsonb_where_not` scope does not accept ranges as arguments. Given `#{value}` for `#{name}` field" if value.is_a?(Range)
67
65
 
68
66
  if JsonbAccessor::QueryHelper.number_query_arguments?(value)
69
67
  value.each { |operator, query_value| query = query.jsonb_number_where_not(column_name, name, operator, query_value) }
@@ -42,9 +42,7 @@ module JsonbAccessor
42
42
 
43
43
  class << self
44
44
  def validate_column_name!(query, column_name)
45
- if query.model.columns.none? { |column| column.name == column_name.to_s }
46
- raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table"
47
- end
45
+ raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table" if query.model.columns.none? { |column| column.name == column_name.to_s }
48
46
  end
49
47
 
50
48
  def validate_field_name!(query, column_name, field_name)
@@ -56,9 +54,7 @@ module JsonbAccessor
56
54
  end
57
55
 
58
56
  def validate_direction!(option)
59
- if ORDER_DIRECTIONS.exclude?(option)
60
- raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted"
61
- end
57
+ raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted" if ORDER_DIRECTIONS.exclude?(option)
62
58
  end
63
59
 
64
60
  def convert_keys_to_store_keys(attributes, store_key_mapping)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonbAccessor
4
- VERSION = "1.3.0"
4
+ VERSION = "1.3.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonb_accessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Crismali
@@ -69,33 +69,33 @@ dependencies:
69
69
  - !ruby/object:Gem::Version
70
70
  version: 2.2.0
71
71
  - !ruby/object:Gem::Dependency
72
- name: database_cleaner
72
+ name: awesome_print
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - "~>"
75
+ - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 1.6.0
77
+ version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - "~>"
82
+ - - ">="
83
83
  - !ruby/object:Gem::Version
84
- version: 1.6.0
84
+ version: '0'
85
85
  - !ruby/object:Gem::Dependency
86
- name: awesome_print
86
+ name: database_cleaner
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  requirements:
89
- - - ">="
89
+ - - "~>"
90
90
  - !ruby/object:Gem::Version
91
- version: '0'
91
+ version: 1.6.0
92
92
  type: :development
93
93
  prerelease: false
94
94
  version_requirements: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - ">="
96
+ - - "~>"
97
97
  - !ruby/object:Gem::Version
98
- version: '0'
98
+ version: 1.6.0
99
99
  - !ruby/object:Gem::Dependency
100
100
  name: pry
101
101
  requirement: !ruby/object:Gem::Requirement
@@ -172,14 +172,14 @@ dependencies:
172
172
  requirements:
173
173
  - - "~>"
174
174
  - !ruby/object:Gem::Version
175
- version: 0.48.1
175
+ version: '1'
176
176
  type: :development
177
177
  prerelease: false
178
178
  version_requirements: !ruby/object:Gem::Requirement
179
179
  requirements:
180
180
  - - "~>"
181
181
  - !ruby/object:Gem::Version
182
- version: 0.48.1
182
+ version: '1'
183
183
  description: Adds typed jsonb backed fields to your ActiveRecord models.
184
184
  email:
185
185
  - michael@crismali.com
@@ -196,6 +196,7 @@ files:
196
196
  - ".travis.yml"
197
197
  - Appraisals
198
198
  - CODE_OF_CONDUCT.md
199
+ - Dockerfile
199
200
  - Gemfile
200
201
  - LICENSE.txt
201
202
  - README.md
@@ -206,6 +207,7 @@ files:
206
207
  - db/config.yml
207
208
  - db/migrate/20150407031737_set_up_testing_db.rb
208
209
  - db/schema.rb
210
+ - docker-compose.yml
209
211
  - gemfiles/activerecord_5.0.0.gemfile
210
212
  - gemfiles/activerecord_5.1.0.gemfile
211
213
  - gemfiles/activerecord_6.1.0.gemfile
@@ -234,8 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
234
236
  - !ruby/object:Gem::Version
235
237
  version: '0'
236
238
  requirements: []
237
- rubyforge_project:
238
- rubygems_version: 2.6.11
239
+ rubygems_version: 3.1.4
239
240
  signing_key:
240
241
  specification_version: 4
241
242
  summary: Adds typed jsonb backed fields to your ActiveRecord models.