activejsonmodel 0.1.0 → 0.1.3

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
2
  SHA256:
3
- metadata.gz: 4bc1e18ab1cb9a6672dc6ab19a0a05340f4761b5cb51ef626d36800846f85a5c
4
- data.tar.gz: 847077d254ef430b85ee5853f8d882e0df84f12b34494d1d47868aa32198989c
3
+ metadata.gz: f12f2a8c7d4739513ed25aa68753babdcee91046a61bbb853be921f6886d34ea
4
+ data.tar.gz: 977442b81243736fb2fe2ecec108d1e2abfcc4d24c5a2e0a0df24cad388a731e
5
5
  SHA512:
6
- metadata.gz: 79b717de46de263cc15a03ca0bad9c636bf12458f037078c66b61bcf7c31fbead8106b4506b54731cf44d9a1a267a62e062f900ca37a0d30e79520f9b44e1ee3
7
- data.tar.gz: 02cdb543f99326a32c4645898cc99bded4e3136a9b4b3e2d9b09f61792f6761db091ea52f6acf52ec8b4a8f0d1ad4575ddfa74e29ba6e561ea25251f3b1d4630
6
+ metadata.gz: be403bfb95151340f132ed221037c95f814c0c8b7ad056ed3bac3425b31034d76cc20ac798a29eb512d6eed5bdf7c2b755dc8341c553ba383ab7c760b75ff8e7
7
+ data.tar.gz: a8c6ffe2e5e4f67c0835e068a0b45355f2f76681e94e068f7123c5b684a8f4f7f7cefb764fc4b1a8806c1544a5091444efe04afb55c8d04e0656a93eb03a1ee7
data/README.md CHANGED
@@ -1,14 +1,17 @@
1
- # activejsonmodel
1
+ # Active JSON Model
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/activejsonmodel.svg)](https://rubygems.org/gems/activejsonmodel)
4
- [![Circle](https://circleci.com/gh/rmorlok/activejsonmodel/tree/main.svg?style=shield)](https://app.circleci.com/pipelines/github/rmorlok/activejsonmodel?branch=main)
5
- [![Code Climate](https://codeclimate.com/github/rmorlok/activejsonmodel/badges/gpa.svg)](https://codeclimate.com/github/rmorlok/activejsonmodel)
4
+ ![Github Actions CI](https://github.com/rmorlok/activejsonmodel/actions/workflows/ci.yaml/badge.svg)
6
5
 
7
- TODO: Description of this gem goes here.
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
+ active_json_model x.x.x built to pkg/active_json_model-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: active_json_model (x.x.x)
222
+ Pushed active_json_model 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.
@@ -238,7 +238,7 @@ module ActiveJsonModel
238
238
  #
239
239
  # Note that this array_data would be stored as jsonb in the database
240
240
  def attribute_type
241
- @attribute_type ||= ActiveModelJsonSerializableType.new(self)
241
+ @attribute_type ||= ActiveRecordType.new(self)
242
242
  end
243
243
 
244
244
  # Allow this model to be used as ActiveRecord attribute type in Rails 5+.
@@ -253,7 +253,7 @@ module ActiveJsonModel
253
253
  # Note that this array_data would be stored as a string in the database, encrypted using
254
254
  # a symmetric key at the application level.
255
255
  def encrypted_attribute_type
256
- @encrypted_attribute_type ||= ActiveModelJsonSerializableEncryptedType.new(self)
256
+ @encrypted_attribute_type ||= ActiveRecordEncryptedType.new(self)
257
257
  end
258
258
  end
259
259
 
@@ -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
 
@@ -274,7 +275,7 @@ module ActiveJsonModel
274
275
  #
275
276
  # Note that this data would be stored as jsonb in the database
276
277
  def attribute_type
277
- @attribute_type ||= ActiveModelJsonSerializableType.new(self)
278
+ @attribute_type ||= ActiveRecordType.new(self)
278
279
  end
279
280
 
280
281
  # Allow this model to be used as ActiveRecord attribute type in Rails 5+.
@@ -289,7 +290,7 @@ module ActiveJsonModel
289
290
  # Note that this data would be stored as a string in the database, encrypted using
290
291
  # a symmetric key at the application level.
291
292
  def encrypted_attribute_type
292
- @encrypted_attribute_type ||= ActiveModelJsonSerializableEncryptedType.new(self)
293
+ @encrypted_attribute_type ||= ActiveRecordEncryptedType.new(self)
293
294
  end
294
295
  end
295
296
 
@@ -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)
File without changes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveJsonModel
4
- VERSION = "0.1.0".freeze
4
+ VERSION = "0.1.3".freeze
5
5
  end
@@ -1,8 +1,11 @@
1
1
  require 'active_model'
2
+ require "active_support"
2
3
 
3
4
  module ActiveJsonModel
4
- autoload :VERSION, "activejsonmodel/version"
5
- autoload :Model, "activejsonmodel/model"
6
- autoload :Array, "activejsonmodel/array"
7
- autoload :Utils, "activejsonmodel/utils"
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :VERSION, "active_json_model/version"
8
+ autoload :Model, "active_json_model/model"
9
+ autoload :Array, "active_json_model/array"
10
+ autoload :Utils, "active_json_model/utils"
8
11
  end
@@ -0,0 +1,4 @@
1
+ # This file is here to make sure Rail's autoloading of gems works
2
+ # When requiring manually, use:
3
+ # require 'active_json_model'
4
+ require_relative './active_json_model'
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.0
4
+ version: 0.1.3
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-21 00:00:00.000000000 Z
11
+ date: 2022-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -50,7 +50,12 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '7.1'
53
- description:
53
+ description: |
54
+ A library for creating Active Models that can serialize/deserialize to JSON. This includes full support for validation
55
+ and change detection through nested models. You can write polymorphic models or arrays of models and get full support
56
+ for natural serialization.
57
+
58
+ Active JSON Model can optionally be combined with Active Record to create nested child models via JSON/JSONB columns.
54
59
  email:
55
60
  - ryan.morlok@morlok.com
56
61
  executables: []
@@ -60,14 +65,15 @@ files:
60
65
  - LICENSE.txt
61
66
  - README.md
62
67
  - lib/active_json_model.rb
63
- - lib/activejsonmodel/active_record_encrypted_type.rb
64
- - lib/activejsonmodel/active_record_type.rb
65
- - lib/activejsonmodel/after_load_callback.rb
66
- - lib/activejsonmodel/array.rb
67
- - lib/activejsonmodel/json_attribute.rb
68
- - lib/activejsonmodel/model.rb
69
- - lib/activejsonmodel/utils.rb
70
- - lib/activejsonmodel/version.rb
68
+ - lib/active_json_model/active_record_encrypted_type.rb
69
+ - lib/active_json_model/active_record_type.rb
70
+ - lib/active_json_model/after_load_callback.rb
71
+ - lib/active_json_model/array.rb
72
+ - lib/active_json_model/json_attribute.rb
73
+ - lib/active_json_model/model.rb
74
+ - lib/active_json_model/utils.rb
75
+ - lib/active_json_model/version.rb
76
+ - lib/activejsonmodel.rb
71
77
  homepage: https://github.com/rmorlok/activejsonmodel
72
78
  licenses:
73
79
  - MIT
@@ -85,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
91
  requirements:
86
92
  - - ">="
87
93
  - !ruby/object:Gem::Version
88
- version: 2.6.0
94
+ version: 2.7.0
89
95
  required_rubygems_version: !ruby/object:Gem::Requirement
90
96
  requirements:
91
97
  - - ">="