activejsonmodel 0.1.1 → 0.1.2
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 +4 -4
- data/README.md +152 -5
- data/lib/activejsonmodel/model.rb +2 -1
- data/lib/activejsonmodel/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f2bf815c387423461ead46e38e315eb8ad0c866f4745ae9d67882a36e7e0b0c
|
4
|
+
data.tar.gz: 59abf0fbc153f61787fd422962e5f23c74d69276de65d0483d82b3b8b1d618ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db6f3794674d502cdc6ffc2b7c25e23e93685cb1255eba0c5a42fb3b431f170b6e70b874d630eaa3319a5a4f81660aa24ab4141b92f864f2a478f5931f67a660
|
7
|
+
data.tar.gz: 8ca90836a7bae3ef2cc78bfe10035486c3612fc3f6c8117568320de1082b0b447fb08d4cc909e23253328396b336e0a194b98db9c0d4a5c421737106186df42d
|
data/README.md
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
-
#
|
1
|
+
# Active JSON Model
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/activejsonmodel)
|
4
|
-
|
5
|
-
[](https://codeclimate.com/github/rmorlok/activejsonmodel)
|
4
|
+

|
6
5
|
|
7
|
-
|
6
|
+
A library for creating Active Models that can serialize/deserialize to JSON. This includes full support for validation
|
7
|
+
and change detection through nested models.
|
8
|
+
|
9
|
+
Active JSON Model can optionally be combined with Active Record to create nested child models via JSON/JSONB columns.
|
8
10
|
|
9
11
|
---
|
10
12
|
|
11
13
|
- [Quick start](#quick-start)
|
14
|
+
- [Gem on RubyGems](https://rubygems.org/gems/activejsonmodel)
|
12
15
|
- [Support](#support)
|
13
16
|
- [License](#license)
|
14
17
|
- [Code of conduct](#code-of-conduct)
|
@@ -16,14 +19,109 @@ TODO: Description of this gem goes here.
|
|
16
19
|
|
17
20
|
## Quick start
|
18
21
|
|
22
|
+
Install the gem:
|
23
|
+
|
19
24
|
```
|
20
25
|
$ gem install activejsonmodel
|
21
26
|
```
|
22
27
|
|
28
|
+
If not using in an auto-loading context (i.e. Rails), import it:
|
29
|
+
|
23
30
|
```ruby
|
24
31
|
require "active_json_model"
|
25
32
|
```
|
26
33
|
|
34
|
+
define a model:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class Point
|
38
|
+
include ActiveJsonModel::Model
|
39
|
+
|
40
|
+
json_attribute :x, Integer
|
41
|
+
json_attribute :y, Integer
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
create an instance of a model:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
origin = Point.new(x: 0, y:0)
|
49
|
+
# => #<Point:0x00007f9f0d0e6538 @mutations_before_last_save=nil, @mutations_from_database=nil, @x=0, @x_is_default=false, @y=0, @y_is_default=false>
|
50
|
+
```
|
51
|
+
|
52
|
+
export it to a JSON-like hash:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
data = origin.dump_to_json
|
56
|
+
# => {:x=>0, :y=>0}
|
57
|
+
```
|
58
|
+
|
59
|
+
encode it as JSON:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
JSON.dump(origin.dump_to_json)
|
63
|
+
# => "{\"x\":0,\"y\":0}"
|
64
|
+
```
|
65
|
+
|
66
|
+
load data from a hash object:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
point2 = Point.load({x: 17, y:42})
|
70
|
+
# => #<Point:0x00007f9f0d10d5c0 @_active_json_model_loaded=true, @mutations_before_last_save=nil, @mutations_from_database=nil, @x=17, @x_is_default=false, @y=42, @y_is_default=false>
|
71
|
+
```
|
72
|
+
|
73
|
+
load from a raw JSON string:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
point3 = Point.load("{\"x\":12,\"y\":19}")
|
77
|
+
# => #<Point:0x00007fe9c0113c88 @_active_json_model_loaded=true, @mutations_before_last_save=nil, @mutations_from_database=nil, @x=12, @x_is_default=false, @y=19, @y_is_default=false>
|
78
|
+
```
|
79
|
+
|
80
|
+
nest models:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class Rectangle
|
84
|
+
include ActiveJsonModel::Model
|
85
|
+
|
86
|
+
json_attribute :top_left, Point
|
87
|
+
json_attribute :bottom_right, Point
|
88
|
+
|
89
|
+
def contains(point)
|
90
|
+
point.x >= top_left.x &&
|
91
|
+
point.x <= bottom_right.x &&
|
92
|
+
point.y <= top_left.y &&
|
93
|
+
point.y >= bottom_right.y
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
If you are using Active Record, use the model as an attribute:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
# db/migrate/20220101000001_create_image_annotations.rb
|
102
|
+
class CreateImageAnnotations < ActiveRecord::Migration[7.0]
|
103
|
+
def change
|
104
|
+
create_table :image_annotations do |t|
|
105
|
+
t.jsonb :region_of_interest, null: false, default: {}
|
106
|
+
t.string :note, null: false, default: ''
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# app/models/image_annotation.rb
|
112
|
+
class ImageAnnotation < ActiveRecord::Base
|
113
|
+
attribute :region_of_interest, Rectangle.attribute_type
|
114
|
+
end
|
115
|
+
|
116
|
+
# use...
|
117
|
+
rect = Rectangle.new(
|
118
|
+
top_left: Point.new(x: 10, y: 100),
|
119
|
+
bottom_right: Point.new(x: 110, y: 0)
|
120
|
+
)
|
121
|
+
ia = ImageAnnotation.new(region_of_interest: rect, note: "Check out this mistake")
|
122
|
+
ia.save
|
123
|
+
```
|
124
|
+
|
27
125
|
## Support
|
28
126
|
|
29
127
|
If you want to report a bug, or have ideas, feedback or questions about the gem, [let me know via GitHub issues](https://github.com/rmorlok/activejsonmodel/issues/new) and I will do my best to provide a helpful answer. Happy hacking!
|
@@ -76,4 +174,53 @@ Active JSON Model uses [minitest](https://github.com/minitest/minitest). To run
|
|
76
174
|
|
77
175
|
```bash
|
78
176
|
rake test
|
79
|
-
```
|
177
|
+
```
|
178
|
+
|
179
|
+
### Building Gem Locally
|
180
|
+
|
181
|
+
Build the gem to `pkg/activejsonmodel-x.x.x.gem`:
|
182
|
+
|
183
|
+
```bash
|
184
|
+
rake build
|
185
|
+
```
|
186
|
+
|
187
|
+
Install the gem locally and import it:
|
188
|
+
|
189
|
+
```bash
|
190
|
+
$ gem install ./pkg/activejsonmodel-x.x.x.gem
|
191
|
+
Successfully installed activejsonmodel-x.x.x
|
192
|
+
Parsing documentation for activejsonmodel-x.x.x
|
193
|
+
Installing ri documentation for activejsonmodel-x.x.x
|
194
|
+
Done installing documentation for activejsonmodel after 0 seconds
|
195
|
+
1 gem installed
|
196
|
+
|
197
|
+
$ irb
|
198
|
+
irb(main):001:0> require 'active_json_model'
|
199
|
+
=> true
|
200
|
+
```
|
201
|
+
|
202
|
+
### Releasing a new version
|
203
|
+
|
204
|
+
1. Update `lib/activejsonmodel/version.rb`
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
module ActiveJsonModel
|
208
|
+
VERSION = "x.x.x".freeze
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
2. Commit all changes
|
213
|
+
3. Release the changes using rake:
|
214
|
+
|
215
|
+
```bash
|
216
|
+
$ rake release
|
217
|
+
activejsonmodel x.x.x built to pkg/activejsonmodel-x.x.x.gem.
|
218
|
+
Tagged vx.x.x.
|
219
|
+
Pushed git commits and release tag.
|
220
|
+
Pushing gem to https://rubygems.org...
|
221
|
+
Successfully registered gem: activejsonmodel (x.x.x)
|
222
|
+
Pushed activejsonmodel x.x.x to rubygems.org
|
223
|
+
Don't forget to publish the release on GitHub!
|
224
|
+
```
|
225
|
+
|
226
|
+
Note that this pushes changes to github and creates a draft release on github.
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'active_support'
|
4
|
+
require 'json'
|
4
5
|
require_relative './json_attribute'
|
5
6
|
require_relative './after_load_callback'
|
6
7
|
|
@@ -572,7 +573,7 @@ module ActiveJsonModel
|
|
572
573
|
end
|
573
574
|
|
574
575
|
# Get the data to a hash, regardless of the starting data type
|
575
|
-
data = json_data.is_a?(String) ? JSON.parse(json_data) : json_data
|
576
|
+
data = json_data.is_a?(String) ? ::JSON.parse(json_data) : json_data
|
576
577
|
|
577
578
|
# Recursively make the value have indifferent access
|
578
579
|
data = ::ActiveJsonModel::Utils.recursively_make_indifferent(data)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activejsonmodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Morlok
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: 2.
|
88
|
+
version: 2.7.0
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
91
|
- - ">="
|