jsonb_accessor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +45 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +152 -0
- data/Rakefile +10 -0
- data/bin/console +21 -0
- data/bin/setup +7 -0
- data/jsonb_accessor.gemspec +35 -0
- data/lib/jsonb_accessor/class_builder.rb +76 -0
- data/lib/jsonb_accessor/macro.rb +91 -0
- data/lib/jsonb_accessor/nested_base.rb +29 -0
- data/lib/jsonb_accessor/type_helper.rb +108 -0
- data/lib/jsonb_accessor/version.rb +3 -0
- data/lib/jsonb_accessor.rb +18 -0
- metadata +233 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 511456406ed682344483b65ab0042ce8ac92b1da
|
4
|
+
data.tar.gz: ce36d4e73676b7b4fff334ffb59680321fa11f5d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9d16ac37937313eb889a626fba7072adbd615c7d862b7ff15d446a020a5954939be1b2f1e51434b415f5a16fcc533a9efef0c4534ad8f6d961c20c40d6a8bbd
|
7
|
+
data.tar.gz: e29820a1d9e1d86bb0516c8cec2ee0770ea8ebacf02e796c443a0ce3c729e274beae953bdf4445bfb4e857b9ce79b9e61e566ef818d00bc741aa87613438eabb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
AllCops:
|
2
|
+
Include:
|
3
|
+
- Rakefile
|
4
|
+
Lint/SpaceBeforeFirstArg:
|
5
|
+
Enabled: false
|
6
|
+
Lint/UnusedBlockArgument:
|
7
|
+
Enabled: false
|
8
|
+
Lint/UnusedMethodArgument:
|
9
|
+
Enabled: false
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Enabled: false
|
12
|
+
Metrics/ClassLength:
|
13
|
+
Enabled: false
|
14
|
+
Metrics/CyclomaticComplexity:
|
15
|
+
Enabled: false
|
16
|
+
Metrics/LineLength:
|
17
|
+
Enabled: false
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Enabled: false
|
20
|
+
Metrics/PerceivedComplexity:
|
21
|
+
Enabled: false
|
22
|
+
Style/AlignParameters:
|
23
|
+
Enabled: false
|
24
|
+
Style/ClassAndModuleChildren:
|
25
|
+
Enabled: false
|
26
|
+
Style/ClassVars:
|
27
|
+
Enabled: false
|
28
|
+
Style/Documentation:
|
29
|
+
Enabled: false
|
30
|
+
Style/FileName:
|
31
|
+
Enabled: false
|
32
|
+
Style/GuardClause:
|
33
|
+
Enabled: false
|
34
|
+
Style/IndentHash:
|
35
|
+
Enabled: false
|
36
|
+
Style/RescueModifier:
|
37
|
+
Enabled: false
|
38
|
+
Style/SignalException:
|
39
|
+
Enabled: false
|
40
|
+
Style/SingleLineMethods:
|
41
|
+
Enabled: false
|
42
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
43
|
+
Enabled: false
|
44
|
+
Style/StringLiterals:
|
45
|
+
EnforcedStyle: double_quotes
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Michael Crismali
|
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,152 @@
|
|
1
|
+
# JSONb Accessor
|
2
|
+
|
3
|
+
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. This gem plans to provide support for these in the future.
|
4
|
+
|
5
|
+
**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
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's `Gemfile`:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "jsonb_accessor"
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
First we must create a model which has a `jsonb` column available to store data into it:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class CreateProductsTable < ActiveRecord::Migration
|
25
|
+
def change
|
26
|
+
create_table :products do |t|
|
27
|
+
t.jsonb :options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
We can then declare the `jsonb` fields we wish to expose via the accessor:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class Product < ActiveRecord::Base
|
37
|
+
jsonb_accessor(
|
38
|
+
:options,
|
39
|
+
:count, # => value type
|
40
|
+
title: :string,
|
41
|
+
id_value: :value,
|
42
|
+
external_id: :integer,
|
43
|
+
reviewed_at: :datetime
|
44
|
+
)
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
JSONb Accessor accepts both untyped and typed key definitions. Untyped keys are treated as-is and no additional casting is performed. This allows the freedom of dynamic values alongside the power types, which is especially convenient when saving nested form attributes. Typed keys will be cast to their respective values using the same mechanism ActiveRecord uses to coerce standard attribute columns. It's as close to a real column as you can get and the goal is to keep it that way.
|
49
|
+
|
50
|
+
All untyped keys must be defined prior to typed columns. You can declare a typed column with type `value` for explicit dynamic behavior. For reference, the `jsonb_accessor` macro is defined thusly.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
def jsonb_accessor(jsonb_attribute, *value_fields, **typed_fields)
|
54
|
+
...
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
There's quite a bit more to do do and document but we're excited to get this out there while we work on it some more.
|
59
|
+
|
60
|
+
## ActiveRecord Methods Generated for Fields
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Product < ActiveRecord::Base
|
64
|
+
jsonb_accessor :data, field: :string
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
* `field`
|
69
|
+
* `field=`
|
70
|
+
* `field?`
|
71
|
+
* `field_changed?`
|
72
|
+
* `field_was`
|
73
|
+
* `field_change`
|
74
|
+
* `reset_field!`
|
75
|
+
* `restore_field!`
|
76
|
+
* `field_will_change!`
|
77
|
+
|
78
|
+
### Supported Types
|
79
|
+
|
80
|
+
The following types are supported, including typed collections:
|
81
|
+
|
82
|
+
```
|
83
|
+
:array,
|
84
|
+
:boolean,
|
85
|
+
:boolean_array,
|
86
|
+
:date,
|
87
|
+
:date_array,
|
88
|
+
:datetime,
|
89
|
+
:datetime_array,
|
90
|
+
:decimal,
|
91
|
+
:decimal_array,
|
92
|
+
:float,
|
93
|
+
:float_array,
|
94
|
+
:integer,
|
95
|
+
:integer_array,
|
96
|
+
:string,
|
97
|
+
:string_array,
|
98
|
+
:time,
|
99
|
+
:time_array,
|
100
|
+
:value
|
101
|
+
```
|
102
|
+
|
103
|
+
Support for nested types is also available but experimental at this point. If you must, you may try something like this for nested objects.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class Product < ActiveRecord::Base
|
107
|
+
jsonb_accessor(
|
108
|
+
:options,
|
109
|
+
nested_object: { key: :integer }
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
p = Product.new
|
114
|
+
p.nested_object.key = "10"
|
115
|
+
puts p.nested_object.key #=> 10
|
116
|
+
```
|
117
|
+
|
118
|
+
## Single-Table Inheritance
|
119
|
+
|
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).
|
121
|
+
|
122
|
+
## Scopes
|
123
|
+
|
124
|
+
Coming soon...
|
125
|
+
|
126
|
+
## Migrations
|
127
|
+
|
128
|
+
Coming soon...
|
129
|
+
|
130
|
+
`jsonb` supports `GIN`, `GIST`, `btree` and `hash` indexes over `json` column. We have plans to add migrations helpers for generating these indexes for you.
|
131
|
+
|
132
|
+
## Dependencies
|
133
|
+
|
134
|
+
- ActiveRecord 4.2
|
135
|
+
- Postgres 9.4 (in order to use the [jsonb column type](http://www.postgresql.org/docs/9.4/static/datatype-json.html)).
|
136
|
+
|
137
|
+
## Development
|
138
|
+
|
139
|
+
After checking out the repo, run `bin/setup` to install dependencies (make sure postgres is running first).
|
140
|
+
|
141
|
+
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
142
|
+
|
143
|
+
`rake` will run Rubocop and the specs.
|
144
|
+
|
145
|
+
## Contributing
|
146
|
+
|
147
|
+
1. [Fork it](https://github.com/devmynd/jsonb_accessor/fork)
|
148
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
149
|
+
3. Add tests and changes (run the tests with `rake`)
|
150
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
151
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
152
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jsonb_accessor"
|
5
|
+
require "rspec"
|
6
|
+
require "awesome_print"
|
7
|
+
require File.expand_path("../../spec/spec_helper.rb", __FILE__)
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(
|
10
|
+
adapter: "postgresql",
|
11
|
+
database: "jsonb_accessor",
|
12
|
+
username: "postgres"
|
13
|
+
)
|
14
|
+
|
15
|
+
class Product < ActiveRecord::Base
|
16
|
+
jsonb_accessor :options, foo: { bar: { baz: :string } }
|
17
|
+
end
|
18
|
+
|
19
|
+
# x = Product.new
|
20
|
+
|
21
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "jsonb_accessor/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jsonb_accessor"
|
8
|
+
spec.version = JsonbAccessor::VERSION
|
9
|
+
spec.authors = ["Michael Crismali", "Joe Hirn"]
|
10
|
+
spec.email = ["michael.crismali@gmail.com", "joe@devmynd.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Adds typed jsonb backed fields to your ActiveRecord models.}
|
13
|
+
spec.description = %q{Adds typed jsonb backed fields to your ActiveRecord models.}
|
14
|
+
spec.homepage = "https://github.com/devmynd/jsonb_accessor"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activerecord", "~> 4.2.0"
|
23
|
+
spec.add_dependency "pg"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
26
|
+
spec.add_development_dependency "database_cleaner"
|
27
|
+
spec.add_development_dependency "awesome_print"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
spec.add_development_dependency "pry-doc"
|
30
|
+
spec.add_development_dependency "pry-nav"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
33
|
+
spec.add_development_dependency "rubocop"
|
34
|
+
spec.add_development_dependency "shoulda-matchers"
|
35
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module JsonbAccessor
|
2
|
+
UnknownValue = Class.new(StandardError)
|
3
|
+
|
4
|
+
module ClassBuilder
|
5
|
+
class << self
|
6
|
+
def generate_class(namespace, new_class_name, attribute_definitions)
|
7
|
+
grouped_attributes = group_attributes(attribute_definitions)
|
8
|
+
|
9
|
+
klass = Class.new(NestedBase)
|
10
|
+
namespace.const_set(new_class_name.to_s.camelize, klass)
|
11
|
+
nested_classes = generate_nested_classes(klass, grouped_attributes[:nested])
|
12
|
+
|
13
|
+
klass.class_eval do
|
14
|
+
singleton_class.send(:define_method, :nested_classes) { nested_classes }
|
15
|
+
singleton_class.send(:define_method, :attribute_on_parent_name) { new_class_name }
|
16
|
+
|
17
|
+
define_method(:attributes_and_data_types) do
|
18
|
+
@attributes_and_data_types ||= grouped_attributes[:typed].each_with_object({}) do |(name, type), attrs_and_data_types|
|
19
|
+
attrs_and_data_types[name] = TypeHelper.fetch(type)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
grouped_attributes[:typed].keys.each do |attribute_name|
|
24
|
+
define_method(attribute_name) { attributes[attribute_name] }
|
25
|
+
|
26
|
+
define_method("#{attribute_name}=") do |value|
|
27
|
+
cast_value = attributes_and_data_types[attribute_name].type_cast_from_user(value)
|
28
|
+
attributes[attribute_name] = cast_value
|
29
|
+
update_parent
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
grouped_attributes[:nested].keys.each do |attribute_name|
|
34
|
+
attr_reader attribute_name
|
35
|
+
|
36
|
+
define_method("#{attribute_name}=") do |value|
|
37
|
+
instance_class = nested_classes[attribute_name]
|
38
|
+
|
39
|
+
case value
|
40
|
+
when instance_class
|
41
|
+
instance = instance_class.new(value.attributes)
|
42
|
+
when Hash
|
43
|
+
instance = instance_class.new(value)
|
44
|
+
when nil
|
45
|
+
instance = instance_class.new
|
46
|
+
else
|
47
|
+
raise UnknownValue, "unable to set value '#{value}' is not a hash, `nil`, or an instance of #{instance_class} in #{__method__}"
|
48
|
+
end
|
49
|
+
|
50
|
+
instance.parent = self
|
51
|
+
instance_variable_set("@#{attribute_name}", instance)
|
52
|
+
attributes[attribute_name] = instance.attributes
|
53
|
+
update_parent
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
klass
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate_nested_classes(klass, nested_attributes)
|
61
|
+
nested_attributes.each_with_object({}) do |(attribute_name, nested_attrs), nested_classes|
|
62
|
+
nested_classes[attribute_name] = generate_class(klass, attribute_name, nested_attrs)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def group_attributes(attributes)
|
69
|
+
attributes.each_with_object(nested: {}, typed: {}) do |(name, type_or_nested), grouped_attributes|
|
70
|
+
group = type_or_nested.is_a?(Hash) ? grouped_attributes[:nested] : grouped_attributes[:typed]
|
71
|
+
group[name] = type_or_nested
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module JsonbAccessor
|
2
|
+
module Macro
|
3
|
+
class << self
|
4
|
+
def group_attributes(value_fields, typed_fields)
|
5
|
+
value_fields_hash = process_value_fields(value_fields)
|
6
|
+
|
7
|
+
typed_fields.each_with_object(nested: {}, typed: value_fields_hash) do |(attribute_name, type_or_nested), grouped_attributes|
|
8
|
+
group = type_or_nested.is_a?(Hash) ? grouped_attributes[:nested] : grouped_attributes[:typed]
|
9
|
+
group[attribute_name] = type_or_nested
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def process_value_fields(value_fields)
|
14
|
+
value_fields.each_with_object({}) do |value_field, hash_for_value_fields|
|
15
|
+
hash_for_value_fields[value_field] = :value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def jsonb_accessor(jsonb_attribute, *value_fields, **typed_fields)
|
22
|
+
all_fields = Macro.group_attributes(value_fields, typed_fields)
|
23
|
+
nested_fields, typed_fields = all_fields.values_at(:nested, :typed)
|
24
|
+
|
25
|
+
class_namespace = Module.new
|
26
|
+
attribute_namespace = Module.new
|
27
|
+
JsonbAccessor.const_set(name, class_namespace)
|
28
|
+
class_namespace.const_set(jsonb_attribute.to_s.camelize, attribute_namespace)
|
29
|
+
|
30
|
+
nested_classes = ClassBuilder.generate_nested_classes(attribute_namespace, nested_fields)
|
31
|
+
|
32
|
+
singleton_class.send(:define_method, "#{jsonb_attribute}_classes") do
|
33
|
+
nested_classes
|
34
|
+
end
|
35
|
+
|
36
|
+
delegate "#{jsonb_attribute}_classes", to: :class
|
37
|
+
|
38
|
+
define_method(:initialize_jsonb_attrs) do
|
39
|
+
jsonb_attribute_hash = send(jsonb_attribute) || {}
|
40
|
+
(typed_fields.keys + nested_fields.keys).each do |field|
|
41
|
+
send("#{field}=", jsonb_attribute_hash[field.to_s])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
after_initialize :initialize_jsonb_attrs
|
46
|
+
|
47
|
+
jsonb_setters = Module.new
|
48
|
+
|
49
|
+
typed_fields.each do |field, type|
|
50
|
+
attribute(field.to_s, TypeHelper.fetch(type))
|
51
|
+
|
52
|
+
jsonb_setters.instance_eval do
|
53
|
+
define_method("#{field}=") do |value, *args, &block|
|
54
|
+
super(value, *args, &block)
|
55
|
+
new_jsonb_value = (send(jsonb_attribute) || {}).merge(field => attributes[field.to_s])
|
56
|
+
send("#{jsonb_attribute}=", new_jsonb_value)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
nested_fields.each do |field, nested_attributes|
|
62
|
+
attribute(field.to_s, TypeHelper.fetch(:value))
|
63
|
+
|
64
|
+
jsonb_setters.instance_eval do
|
65
|
+
define_method("#{field}=") do |value|
|
66
|
+
instance_class = nested_classes[field]
|
67
|
+
|
68
|
+
case value
|
69
|
+
when instance_class
|
70
|
+
instance = instance_class.new(value.attributes)
|
71
|
+
when Hash
|
72
|
+
instance = instance_class.new(value)
|
73
|
+
when nil
|
74
|
+
instance = instance_class.new
|
75
|
+
else
|
76
|
+
raise UnknownValue, "unable to set value '#{value}' is not a hash, `nil`, or an instance of #{instance_class} in #{__method__}"
|
77
|
+
end
|
78
|
+
|
79
|
+
instance.parent = self
|
80
|
+
new_jsonb_value = (send(jsonb_attribute) || {}).merge(field.to_s => instance.attributes)
|
81
|
+
send("#{jsonb_attribute}=", new_jsonb_value)
|
82
|
+
super(instance)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
include jsonb_setters
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module JsonbAccessor
|
2
|
+
class NestedBase
|
3
|
+
attr_accessor :attributes, :parent
|
4
|
+
alias_method :to_h, :attributes
|
5
|
+
|
6
|
+
delegate :[], to: :attributes
|
7
|
+
delegate :nested_classes, :attribute_on_parent_name, to: :class
|
8
|
+
|
9
|
+
def initialize(attributes = {})
|
10
|
+
self.attributes = {}.with_indifferent_access
|
11
|
+
|
12
|
+
nested_classes.keys.each do |key|
|
13
|
+
send("#{key}=", nil)
|
14
|
+
end
|
15
|
+
|
16
|
+
attributes.each do |name, value|
|
17
|
+
send("#{name}=", value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def update_parent
|
22
|
+
parent.send("#{attribute_on_parent_name}=", self) if parent
|
23
|
+
end
|
24
|
+
|
25
|
+
def []=(key, value)
|
26
|
+
send("#{key}=", value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module JsonbAccessor
|
2
|
+
module TypeHelper
|
3
|
+
TYPES = [
|
4
|
+
:array,
|
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
|
+
]
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def fetch(type)
|
26
|
+
TYPES.include?(type) ? send(type) : value
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def value
|
32
|
+
ActiveRecord::Type::Value.new
|
33
|
+
end
|
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)
|
69
|
+
end
|
70
|
+
|
71
|
+
def string_array
|
72
|
+
new_array(string)
|
73
|
+
end
|
74
|
+
|
75
|
+
def integer_array
|
76
|
+
new_array(integer)
|
77
|
+
end
|
78
|
+
|
79
|
+
def boolean_array
|
80
|
+
new_array(boolean)
|
81
|
+
end
|
82
|
+
|
83
|
+
def date_array
|
84
|
+
new_array(date)
|
85
|
+
end
|
86
|
+
|
87
|
+
def datetime_array
|
88
|
+
new_array(datetime)
|
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)
|
101
|
+
end
|
102
|
+
|
103
|
+
def new_array(subtype)
|
104
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(subtype)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
4
|
+
|
5
|
+
require "jsonb_accessor/version"
|
6
|
+
require "jsonb_accessor/type_helper"
|
7
|
+
require "jsonb_accessor/nested_base"
|
8
|
+
require "jsonb_accessor/class_builder"
|
9
|
+
require "jsonb_accessor/macro"
|
10
|
+
|
11
|
+
module JsonbAccessor
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
include Macro
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveSupport.on_load(:active_record) do
|
17
|
+
ActiveRecord::Base.send(:include, JsonbAccessor)
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonb_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Crismali
|
8
|
+
- Joe Hirn
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-04-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 4.2.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 4.2.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: pg
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.9'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.9'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: database_cleaner
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: awesome_print
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: pry
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: pry-doc
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: pry-nav
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: rake
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - "~>"
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '10.0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - "~>"
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '10.0'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: rspec
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - "~>"
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: 3.2.0
|
147
|
+
type: :development
|
148
|
+
prerelease: false
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - "~>"
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: 3.2.0
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: rubocop
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
- !ruby/object:Gem::Dependency
|
169
|
+
name: shoulda-matchers
|
170
|
+
requirement: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
type: :development
|
176
|
+
prerelease: false
|
177
|
+
version_requirements: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
description: Adds typed jsonb backed fields to your ActiveRecord models.
|
183
|
+
email:
|
184
|
+
- michael.crismali@gmail.com
|
185
|
+
- joe@devmynd.com
|
186
|
+
executables: []
|
187
|
+
extensions: []
|
188
|
+
extra_rdoc_files: []
|
189
|
+
files:
|
190
|
+
- ".gitignore"
|
191
|
+
- ".rspec"
|
192
|
+
- ".rubocop.yml"
|
193
|
+
- ".travis.yml"
|
194
|
+
- CODE_OF_CONDUCT.md
|
195
|
+
- Gemfile
|
196
|
+
- LICENSE.txt
|
197
|
+
- README.md
|
198
|
+
- Rakefile
|
199
|
+
- bin/console
|
200
|
+
- bin/setup
|
201
|
+
- jsonb_accessor.gemspec
|
202
|
+
- lib/jsonb_accessor.rb
|
203
|
+
- lib/jsonb_accessor/class_builder.rb
|
204
|
+
- lib/jsonb_accessor/macro.rb
|
205
|
+
- lib/jsonb_accessor/nested_base.rb
|
206
|
+
- lib/jsonb_accessor/type_helper.rb
|
207
|
+
- lib/jsonb_accessor/version.rb
|
208
|
+
homepage: https://github.com/devmynd/jsonb_accessor
|
209
|
+
licenses:
|
210
|
+
- MIT
|
211
|
+
metadata: {}
|
212
|
+
post_install_message:
|
213
|
+
rdoc_options: []
|
214
|
+
require_paths:
|
215
|
+
- lib
|
216
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
217
|
+
requirements:
|
218
|
+
- - ">="
|
219
|
+
- !ruby/object:Gem::Version
|
220
|
+
version: '0'
|
221
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
222
|
+
requirements:
|
223
|
+
- - ">="
|
224
|
+
- !ruby/object:Gem::Version
|
225
|
+
version: '0'
|
226
|
+
requirements: []
|
227
|
+
rubyforge_project:
|
228
|
+
rubygems_version: 2.2.2
|
229
|
+
signing_key:
|
230
|
+
specification_version: 4
|
231
|
+
summary: Adds typed jsonb backed fields to your ActiveRecord models.
|
232
|
+
test_files: []
|
233
|
+
has_rdoc:
|