jsonb_accessor 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +10 -1
- data/.ruby-version +1 -0
- data/.travis.yml +11 -0
- data/Appraisals +3 -0
- data/README.md +8 -2
- data/Rakefile +2 -0
- data/bin/console +3 -3
- data/bin/setup +3 -2
- data/db/config.yml +12 -0
- data/db/migrate/20150407031737_set_up_testing_db.rb +22 -0
- data/db/schema.rb +37 -0
- data/docker-compose.yml +4 -0
- data/gemfiles/activerecord_4.2.1.gemfile +7 -0
- data/gemfiles/activerecord_4.2.1.gemfile.lock +141 -0
- data/jsonb_accessor.gemspec +3 -1
- data/lib/jsonb_accessor/macro.rb +33 -10
- data/lib/jsonb_accessor/nested_base.rb +4 -0
- data/lib/jsonb_accessor/type_helper.rb +33 -83
- data/lib/jsonb_accessor/version.rb +1 -1
- metadata +43 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9aa4a83990520b8f0190a339f1973ead1de118c2
|
4
|
+
data.tar.gz: e12959b405e89178e7bbacbad07948c822db635e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3a2da61536078ceef74ce35eb35f9645443389304710cb801fbbb49ff6aa534b738e89c4d29454361ff8916dbe0ac71b600ee697ee9b43c5f1c2fb84bb24d81
|
7
|
+
data.tar.gz: b8e45b17608b058ecc56c721b32785097422503f24aeddd0dc44243a0d041f6fa55ad812c34f862eea1b38a7f4ca373a37768e82d6615ea15eb9430f0798ca3a
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
AllCops:
|
2
2
|
Include:
|
3
|
-
- Rakefile
|
3
|
+
- "**/Rakefile"
|
4
|
+
Exclude:
|
5
|
+
- db/**/*
|
6
|
+
- vendor/**/*
|
4
7
|
Lint/SpaceBeforeFirstArg:
|
5
8
|
Enabled: false
|
6
9
|
Lint/UnusedBlockArgument:
|
@@ -27,12 +30,18 @@ Style/ClassVars:
|
|
27
30
|
Enabled: false
|
28
31
|
Style/Documentation:
|
29
32
|
Enabled: false
|
33
|
+
Style/DoubleNegation:
|
34
|
+
Enabled: false
|
30
35
|
Style/FileName:
|
31
36
|
Enabled: false
|
32
37
|
Style/GuardClause:
|
33
38
|
Enabled: false
|
34
39
|
Style/IndentHash:
|
35
40
|
Enabled: false
|
41
|
+
Style/NilComparison:
|
42
|
+
Enabled: false
|
43
|
+
Style/OpMethod:
|
44
|
+
Enabled: false
|
36
45
|
Style/RescueModifier:
|
37
46
|
Enabled: false
|
38
47
|
Style/SignalException:
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.0
|
data/.travis.yml
CHANGED
@@ -1,3 +1,14 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
3
|
- 2.1.5
|
4
|
+
- 2.2.0
|
5
|
+
addons:
|
6
|
+
postgresql: '9.4'
|
7
|
+
before_install:
|
8
|
+
- gem update --system
|
9
|
+
- gem --version
|
10
|
+
- gem install bundler
|
11
|
+
before_script:
|
12
|
+
- bundle exec rake db:create
|
13
|
+
- bundle exec rake db:migrate
|
14
|
+
cache: bundler
|
data/Appraisals
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# JSONb Accessor
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/devmynd/jsonb_accessor.svg)](https://travis-ci.org/devmynd/jsonb_accessor)
|
4
|
+
|
5
|
+
Adds typed `jsonb` backed fields as first class citizens to your `ActiveRecord` models. This gem is similar in spirit to [HstoreAccessor](https://github.com/devmynd/hstore_accessor), but the `jsonb` column in PostgreSQL has a few distinct advantages, mostly around nested documents and support for collections.
|
4
6
|
|
5
7
|
**This gem is under heavy development. Please use cautiously and help us with feedback by opening issues for defects and feature requests. The current API is subject to change.**
|
6
8
|
|
@@ -115,9 +117,13 @@ p.nested_object.key = "10"
|
|
115
117
|
puts p.nested_object.key #=> 10
|
116
118
|
```
|
117
119
|
|
120
|
+
## Validations
|
121
|
+
|
122
|
+
Because this gem promotes attributes nested into the JSON column to first level attributes, most validations should just work. We still have to add some testing and support around this feature but feel free to try and leave us feedback if they're not working as expected.
|
123
|
+
|
118
124
|
## Single-Table Inheritance
|
119
125
|
|
120
|
-
You can use it for STI in the same spirit as [hstore_accessor, which documented here.](https://github.com/devmynd/hstore_accessor#single-table-inheritance).
|
126
|
+
You can use it for STI in the same spirit as [hstore_accessor, which is documented here.](https://github.com/devmynd/hstore_accessor#single-table-inheritance).
|
121
127
|
|
122
128
|
## Scopes
|
123
129
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# rubocop:disable Lint/UselessAssignment
|
2
3
|
|
3
4
|
require "bundler/setup"
|
4
5
|
require "jsonb_accessor"
|
5
6
|
require "rspec"
|
6
|
-
require "awesome_print"
|
7
7
|
require File.expand_path("../../spec/spec_helper.rb", __FILE__)
|
8
8
|
|
9
9
|
ActiveRecord::Base.establish_connection(
|
@@ -13,9 +13,9 @@ ActiveRecord::Base.establish_connection(
|
|
13
13
|
)
|
14
14
|
|
15
15
|
class Product < ActiveRecord::Base
|
16
|
-
jsonb_accessor :options, foo: { bar: { baz: :string } }
|
16
|
+
jsonb_accessor :options, title: :string, foo: { bar: { baz: :string } }
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
x = Product.new
|
20
20
|
|
21
21
|
Pry.start
|
data/bin/setup
CHANGED
data/db/config.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class SetUpTestingDb < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :products do |t|
|
4
|
+
t.jsonb :options
|
5
|
+
t.jsonb :data
|
6
|
+
|
7
|
+
t.string :string_type
|
8
|
+
t.integer :integer_type
|
9
|
+
t.integer :product_category_id
|
10
|
+
t.boolean :boolean_type
|
11
|
+
t.float :float_type
|
12
|
+
t.time :time_type
|
13
|
+
t.date :date_type
|
14
|
+
t.datetime :datetime_type
|
15
|
+
t.decimal :decimal_type
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table :product_categories do |t|
|
19
|
+
t.jsonb :options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/db/schema.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended that you check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(version: 20150407031737) do
|
15
|
+
|
16
|
+
# These are extensions that must be enabled in order to support this database
|
17
|
+
enable_extension "plpgsql"
|
18
|
+
|
19
|
+
create_table "product_categories", force: :cascade do |t|
|
20
|
+
t.jsonb "options"
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table "products", force: :cascade do |t|
|
24
|
+
t.jsonb "options"
|
25
|
+
t.jsonb "data"
|
26
|
+
t.string "string_type"
|
27
|
+
t.integer "integer_type"
|
28
|
+
t.integer "product_category_id"
|
29
|
+
t.boolean "boolean_type"
|
30
|
+
t.float "float_type"
|
31
|
+
t.time "time_type"
|
32
|
+
t.date "date_type"
|
33
|
+
t.datetime "datetime_type"
|
34
|
+
t.decimal "decimal_type"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
jsonb_accessor (0.1.0)
|
5
|
+
activerecord (>= 4.2.1)
|
6
|
+
pg
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (4.2.1)
|
12
|
+
actionview (= 4.2.1)
|
13
|
+
activesupport (= 4.2.1)
|
14
|
+
rack (~> 1.6)
|
15
|
+
rack-test (~> 0.6.2)
|
16
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
17
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
18
|
+
actionview (4.2.1)
|
19
|
+
activesupport (= 4.2.1)
|
20
|
+
builder (~> 3.1)
|
21
|
+
erubis (~> 2.7.0)
|
22
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
23
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
24
|
+
activemodel (4.2.1)
|
25
|
+
activesupport (= 4.2.1)
|
26
|
+
builder (~> 3.1)
|
27
|
+
activerecord (4.2.1)
|
28
|
+
activemodel (= 4.2.1)
|
29
|
+
activesupport (= 4.2.1)
|
30
|
+
arel (~> 6.0)
|
31
|
+
activesupport (4.2.1)
|
32
|
+
i18n (~> 0.7)
|
33
|
+
json (~> 1.7, >= 1.7.7)
|
34
|
+
minitest (~> 5.1)
|
35
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
36
|
+
tzinfo (~> 1.1)
|
37
|
+
appraisal (1.0.3)
|
38
|
+
bundler
|
39
|
+
rake
|
40
|
+
thor (>= 0.14.0)
|
41
|
+
arel (6.0.0)
|
42
|
+
ast (2.0.0)
|
43
|
+
astrolabe (1.3.0)
|
44
|
+
parser (>= 2.2.0.pre.3, < 3.0)
|
45
|
+
awesome_print (1.6.1)
|
46
|
+
builder (3.2.2)
|
47
|
+
coderay (1.1.0)
|
48
|
+
database_cleaner (1.4.1)
|
49
|
+
diff-lcs (1.2.5)
|
50
|
+
erubis (2.7.0)
|
51
|
+
i18n (0.7.0)
|
52
|
+
json (1.8.2)
|
53
|
+
loofah (2.0.1)
|
54
|
+
nokogiri (>= 1.5.9)
|
55
|
+
method_source (0.8.2)
|
56
|
+
mini_portile (0.6.2)
|
57
|
+
minitest (5.5.1)
|
58
|
+
nokogiri (1.6.6.2)
|
59
|
+
mini_portile (~> 0.6.0)
|
60
|
+
parser (2.2.0.3)
|
61
|
+
ast (>= 1.1, < 3.0)
|
62
|
+
pg (0.18.1)
|
63
|
+
powerpack (0.1.0)
|
64
|
+
pry (0.10.1)
|
65
|
+
coderay (~> 1.1.0)
|
66
|
+
method_source (~> 0.8.1)
|
67
|
+
slop (~> 3.4)
|
68
|
+
pry-doc (0.6.0)
|
69
|
+
pry (~> 0.9)
|
70
|
+
yard (~> 0.8)
|
71
|
+
pry-nav (0.2.4)
|
72
|
+
pry (>= 0.9.10, < 0.11.0)
|
73
|
+
rack (1.6.0)
|
74
|
+
rack-test (0.6.3)
|
75
|
+
rack (>= 1.0)
|
76
|
+
rails-deprecated_sanitizer (1.0.3)
|
77
|
+
activesupport (>= 4.2.0.alpha)
|
78
|
+
rails-dom-testing (1.0.6)
|
79
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
80
|
+
nokogiri (~> 1.6.0)
|
81
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
82
|
+
rails-html-sanitizer (1.0.2)
|
83
|
+
loofah (~> 2.0)
|
84
|
+
railties (4.2.1)
|
85
|
+
actionpack (= 4.2.1)
|
86
|
+
activesupport (= 4.2.1)
|
87
|
+
rake (>= 0.8.7)
|
88
|
+
thor (>= 0.18.1, < 2.0)
|
89
|
+
rainbow (2.0.0)
|
90
|
+
rake (10.4.2)
|
91
|
+
rspec (3.2.0)
|
92
|
+
rspec-core (~> 3.2.0)
|
93
|
+
rspec-expectations (~> 3.2.0)
|
94
|
+
rspec-mocks (~> 3.2.0)
|
95
|
+
rspec-core (3.2.3)
|
96
|
+
rspec-support (~> 3.2.0)
|
97
|
+
rspec-expectations (3.2.1)
|
98
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
99
|
+
rspec-support (~> 3.2.0)
|
100
|
+
rspec-mocks (3.2.1)
|
101
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
102
|
+
rspec-support (~> 3.2.0)
|
103
|
+
rspec-support (3.2.2)
|
104
|
+
rubocop (0.30.0)
|
105
|
+
astrolabe (~> 1.3)
|
106
|
+
parser (>= 2.2.0.1, < 3.0)
|
107
|
+
powerpack (~> 0.1)
|
108
|
+
rainbow (>= 1.99.1, < 3.0)
|
109
|
+
ruby-progressbar (~> 1.4)
|
110
|
+
ruby-progressbar (1.7.5)
|
111
|
+
shoulda-matchers (2.8.0)
|
112
|
+
activesupport (>= 3.0.0)
|
113
|
+
slop (3.6.0)
|
114
|
+
standalone_migrations (4.0.1)
|
115
|
+
activerecord (~> 4.2.0)
|
116
|
+
railties (~> 4.2.0)
|
117
|
+
rake (~> 10.0)
|
118
|
+
thor (0.19.1)
|
119
|
+
thread_safe (0.3.5)
|
120
|
+
tzinfo (1.2.2)
|
121
|
+
thread_safe (~> 0.1)
|
122
|
+
yard (0.8.7.6)
|
123
|
+
|
124
|
+
PLATFORMS
|
125
|
+
ruby
|
126
|
+
|
127
|
+
DEPENDENCIES
|
128
|
+
activerecord (= 4.2.1)
|
129
|
+
appraisal
|
130
|
+
awesome_print
|
131
|
+
bundler (~> 1.9)
|
132
|
+
database_cleaner
|
133
|
+
jsonb_accessor!
|
134
|
+
pry
|
135
|
+
pry-doc
|
136
|
+
pry-nav
|
137
|
+
rake (~> 10.0)
|
138
|
+
rspec (~> 3.2.0)
|
139
|
+
rubocop
|
140
|
+
shoulda-matchers
|
141
|
+
standalone_migrations
|
data/jsonb_accessor.gemspec
CHANGED
@@ -19,9 +19,10 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "activerecord", "
|
22
|
+
spec.add_dependency "activerecord", ">= 4.2.1"
|
23
23
|
spec.add_dependency "pg"
|
24
24
|
|
25
|
+
spec.add_development_dependency "appraisal"
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.9"
|
26
27
|
spec.add_development_dependency "database_cleaner"
|
27
28
|
spec.add_development_dependency "awesome_print"
|
@@ -32,4 +33,5 @@ Gem::Specification.new do |spec|
|
|
32
33
|
spec.add_development_dependency "rspec", "~> 3.2.0"
|
33
34
|
spec.add_development_dependency "rubocop"
|
34
35
|
spec.add_development_dependency "shoulda-matchers"
|
36
|
+
spec.add_development_dependency "standalone_migrations"
|
35
37
|
end
|
data/lib/jsonb_accessor/macro.rb
CHANGED
@@ -15,6 +15,17 @@ module JsonbAccessor
|
|
15
15
|
hash_for_value_fields[value_field] = :value
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
def build_class_namespace(class_name)
|
20
|
+
class_name = class_name.gsub(CONSTANT_SEPARATOR, "")
|
21
|
+
if JsonbAccessor.constants.any? { |c| c.to_s == class_name }
|
22
|
+
class_namespace = JsonbAccessor.const_get(class_name)
|
23
|
+
else
|
24
|
+
class_namespace = Module.new
|
25
|
+
JsonbAccessor.const_set(class_name, class_namespace)
|
26
|
+
end
|
27
|
+
class_namespace
|
28
|
+
end
|
18
29
|
end
|
19
30
|
|
20
31
|
module ClassMethods
|
@@ -22,9 +33,8 @@ module JsonbAccessor
|
|
22
33
|
all_fields = Macro.group_attributes(value_fields, typed_fields)
|
23
34
|
nested_fields, typed_fields = all_fields.values_at(:nested, :typed)
|
24
35
|
|
25
|
-
class_namespace =
|
36
|
+
class_namespace = Macro.build_class_namespace(name)
|
26
37
|
attribute_namespace = Module.new
|
27
|
-
JsonbAccessor.const_set(name, class_namespace)
|
28
38
|
class_namespace.const_set(jsonb_attribute.to_s.camelize, attribute_namespace)
|
29
39
|
|
30
40
|
nested_classes = ClassBuilder.generate_nested_classes(attribute_namespace, nested_fields)
|
@@ -35,25 +45,38 @@ module JsonbAccessor
|
|
35
45
|
|
36
46
|
delegate "#{jsonb_attribute}_classes", to: :class
|
37
47
|
|
38
|
-
|
48
|
+
jsonb_attribute_initialization_method_name = "initialize_jsonb_attrs_for_#{jsonb_attribute}"
|
49
|
+
|
50
|
+
define_method(jsonb_attribute_initialization_method_name) do
|
39
51
|
jsonb_attribute_hash = send(jsonb_attribute) || {}
|
40
52
|
(typed_fields.keys + nested_fields.keys).each do |field|
|
41
53
|
send("#{field}=", jsonb_attribute_hash[field.to_s])
|
42
54
|
end
|
43
55
|
end
|
44
56
|
|
45
|
-
after_initialize
|
57
|
+
after_initialize(jsonb_attribute_initialization_method_name)
|
46
58
|
|
47
|
-
|
59
|
+
jsonb_accessor_methods = Module.new do
|
60
|
+
define_method("#{jsonb_attribute}=") do |value|
|
61
|
+
write_attribute(jsonb_attribute, value)
|
62
|
+
send(jsonb_attribute_initialization_method_name)
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method(:reload) do |*args, &block|
|
66
|
+
super(*args, &block)
|
67
|
+
send(jsonb_attribute_initialization_method_name)
|
68
|
+
self
|
69
|
+
end
|
70
|
+
end
|
48
71
|
|
49
72
|
typed_fields.each do |field, type|
|
50
73
|
attribute(field.to_s, TypeHelper.fetch(type))
|
51
74
|
|
52
|
-
|
75
|
+
jsonb_accessor_methods.instance_eval do
|
53
76
|
define_method("#{field}=") do |value, *args, &block|
|
54
77
|
super(value, *args, &block)
|
55
78
|
new_jsonb_value = (send(jsonb_attribute) || {}).merge(field => attributes[field.to_s])
|
56
|
-
|
79
|
+
write_attribute(jsonb_attribute, new_jsonb_value)
|
57
80
|
end
|
58
81
|
end
|
59
82
|
end
|
@@ -61,7 +84,7 @@ module JsonbAccessor
|
|
61
84
|
nested_fields.each do |field, nested_attributes|
|
62
85
|
attribute(field.to_s, TypeHelper.fetch(:value))
|
63
86
|
|
64
|
-
|
87
|
+
jsonb_accessor_methods.instance_eval do
|
65
88
|
define_method("#{field}=") do |value|
|
66
89
|
instance_class = nested_classes[field]
|
67
90
|
|
@@ -78,13 +101,13 @@ module JsonbAccessor
|
|
78
101
|
|
79
102
|
instance.parent = self
|
80
103
|
new_jsonb_value = (send(jsonb_attribute) || {}).merge(field.to_s => instance.attributes)
|
81
|
-
|
104
|
+
write_attribute(jsonb_attribute, new_jsonb_value)
|
82
105
|
super(instance)
|
83
106
|
end
|
84
107
|
end
|
85
108
|
end
|
86
109
|
|
87
|
-
include
|
110
|
+
include jsonb_accessor_methods
|
88
111
|
end
|
89
112
|
end
|
90
113
|
end
|
@@ -1,103 +1,53 @@
|
|
1
1
|
module JsonbAccessor
|
2
|
+
CONSTANT_SEPARATOR = "::"
|
2
3
|
module TypeHelper
|
3
|
-
|
4
|
-
|
5
|
-
:boolean,
|
6
|
-
:boolean_array,
|
7
|
-
:date,
|
8
|
-
:date_array,
|
9
|
-
:datetime,
|
10
|
-
:datetime_array,
|
11
|
-
:decimal,
|
12
|
-
:decimal_array,
|
13
|
-
:float,
|
14
|
-
:float_array,
|
15
|
-
:integer,
|
16
|
-
:integer_array,
|
17
|
-
:string,
|
18
|
-
:string_array,
|
19
|
-
:time,
|
20
|
-
:time_array,
|
21
|
-
:value
|
22
|
-
]
|
4
|
+
ARRAY_MATCHER = /_array\z/
|
5
|
+
UnknownType = Class.new(StandardError)
|
23
6
|
|
24
7
|
class << self
|
25
8
|
def fetch(type)
|
26
|
-
|
9
|
+
case type
|
10
|
+
when :array
|
11
|
+
new_array(value)
|
12
|
+
when ARRAY_MATCHER
|
13
|
+
fetch_active_record_array_type(type)
|
14
|
+
when :value
|
15
|
+
value
|
16
|
+
else
|
17
|
+
fetch_active_record_type(type)
|
18
|
+
end
|
27
19
|
end
|
28
20
|
|
29
21
|
private
|
30
22
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
def string
|
36
|
-
ActiveRecord::Type::String.new
|
37
|
-
end
|
38
|
-
|
39
|
-
def integer
|
40
|
-
ActiveRecord::Type::Integer.new
|
41
|
-
end
|
42
|
-
|
43
|
-
def boolean
|
44
|
-
ActiveRecord::Type::Boolean.new
|
45
|
-
end
|
46
|
-
|
47
|
-
def date
|
48
|
-
ActiveRecord::Type::Date.new
|
49
|
-
end
|
50
|
-
|
51
|
-
def datetime
|
52
|
-
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime.new
|
53
|
-
end
|
54
|
-
|
55
|
-
def decimal
|
56
|
-
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal.new
|
57
|
-
end
|
58
|
-
|
59
|
-
def time
|
60
|
-
ActiveRecord::Type::Time.new
|
61
|
-
end
|
62
|
-
|
63
|
-
def float
|
64
|
-
ActiveRecord::Type::Float.new
|
65
|
-
end
|
66
|
-
|
67
|
-
def array
|
68
|
-
new_array(value)
|
23
|
+
def fetch_active_record_array_type(type)
|
24
|
+
subtype = type.to_s.sub(ARRAY_MATCHER, "")
|
25
|
+
new_array(fetch_active_record_type(subtype))
|
69
26
|
end
|
70
27
|
|
71
|
-
def
|
72
|
-
|
73
|
-
|
28
|
+
def fetch_active_record_type(type)
|
29
|
+
class_name = type.to_s.camelize
|
30
|
+
klass = value_descendants.find do |ar_type|
|
31
|
+
ar_type.to_s.split(CONSTANT_SEPARATOR).last == class_name
|
32
|
+
end
|
74
33
|
|
75
|
-
|
76
|
-
|
34
|
+
if klass
|
35
|
+
klass.new
|
36
|
+
else
|
37
|
+
raise JsonbAccessor::TypeHelper::UnknownType
|
38
|
+
end
|
77
39
|
end
|
78
40
|
|
79
|
-
def
|
80
|
-
|
81
|
-
|
41
|
+
def value_descendants
|
42
|
+
grouped_types = ActiveRecord::Type::Value.descendants.group_by do |ar_type|
|
43
|
+
!!ar_type.to_s.match(ActiveRecord::ConnectionAdapters::PostgreSQL::OID.to_s)
|
44
|
+
end
|
82
45
|
|
83
|
-
|
84
|
-
new_array(date)
|
46
|
+
grouped_types[true] + grouped_types[false]
|
85
47
|
end
|
86
48
|
|
87
|
-
def
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
def decimal_array
|
92
|
-
new_array(decimal)
|
93
|
-
end
|
94
|
-
|
95
|
-
def time_array
|
96
|
-
new_array(time)
|
97
|
-
end
|
98
|
-
|
99
|
-
def float_array
|
100
|
-
new_array(float)
|
49
|
+
def value
|
50
|
+
ActiveRecord::Type::Value.new
|
101
51
|
end
|
102
52
|
|
103
53
|
def new_array(subtype)
|
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: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Crismali
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-04-
|
12
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 4.2.
|
20
|
+
version: 4.2.1
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 4.2.
|
27
|
+
version: 4.2.1
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: pg
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: appraisal
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: bundler
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -179,6 +193,20 @@ dependencies:
|
|
179
193
|
- - ">="
|
180
194
|
- !ruby/object:Gem::Version
|
181
195
|
version: '0'
|
196
|
+
- !ruby/object:Gem::Dependency
|
197
|
+
name: standalone_migrations
|
198
|
+
requirement: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
type: :development
|
204
|
+
prerelease: false
|
205
|
+
version_requirements: !ruby/object:Gem::Requirement
|
206
|
+
requirements:
|
207
|
+
- - ">="
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: '0'
|
182
210
|
description: Adds typed jsonb backed fields to your ActiveRecord models.
|
183
211
|
email:
|
184
212
|
- michael.crismali@gmail.com
|
@@ -190,7 +218,9 @@ files:
|
|
190
218
|
- ".gitignore"
|
191
219
|
- ".rspec"
|
192
220
|
- ".rubocop.yml"
|
221
|
+
- ".ruby-version"
|
193
222
|
- ".travis.yml"
|
223
|
+
- Appraisals
|
194
224
|
- CODE_OF_CONDUCT.md
|
195
225
|
- Gemfile
|
196
226
|
- LICENSE.txt
|
@@ -198,6 +228,12 @@ files:
|
|
198
228
|
- Rakefile
|
199
229
|
- bin/console
|
200
230
|
- bin/setup
|
231
|
+
- db/config.yml
|
232
|
+
- db/migrate/20150407031737_set_up_testing_db.rb
|
233
|
+
- db/schema.rb
|
234
|
+
- docker-compose.yml
|
235
|
+
- gemfiles/activerecord_4.2.1.gemfile
|
236
|
+
- gemfiles/activerecord_4.2.1.gemfile.lock
|
201
237
|
- jsonb_accessor.gemspec
|
202
238
|
- lib/jsonb_accessor.rb
|
203
239
|
- lib/jsonb_accessor/class_builder.rb
|
@@ -225,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
225
261
|
version: '0'
|
226
262
|
requirements: []
|
227
263
|
rubyforge_project:
|
228
|
-
rubygems_version: 2.
|
264
|
+
rubygems_version: 2.4.5
|
229
265
|
signing_key:
|
230
266
|
specification_version: 4
|
231
267
|
summary: Adds typed jsonb backed fields to your ActiveRecord models.
|