activejsonmodel 0.1.1 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3b7eed61989b4be5a09f4d258b54c3d5cdda35c3b129f21596e6a33dbf82347
4
- data.tar.gz: d62f8023f6e64689c4cf01998e8f1fed2b4acfc5654ecb5fed073e69bf1075aa
3
+ metadata.gz: e7dc0c113f2d8bd650b237c5888d8cfdc8e0bb7747dc994eacc88bed98ed86fd
4
+ data.tar.gz: 88ef99a717976d7ba37de7523f101fbff1f49ec848bd6e216f33f7a33482d530
5
5
  SHA512:
6
- metadata.gz: 04c87f280772d42105d22547deb96d7920536469f4656fa1541c95a67145e95e7c210aa0b8054e3406058604ad303fd635732e6f560c56651d75fe9d40b57d66
7
- data.tar.gz: 03fc5c92a441bc639c23e194eaaab53f86fe37a1d427d689c305aa898d7ec247a6f52db991961c9224cdfdeaa6f30ba55d7692e4ad8fefbea6f8c8a4e9527618
6
+ metadata.gz: '09bb083c925bed9b1efdf6fa7b343cb85e3b7ac5419d614218590980657bce605ff444a1012680c20e97639ae535ebc0a413f7b2eb51f90f8ad5518a11f7a0eb'
7
+ data.tar.gz: 74455ab06735cfb9b27d68cbe44d6457cb968b3eeaae2dc79e691f1bc5a36f161aa015403cf6effdd0f970b8d1d2f7dc25e46f7637a0ec5b74ad0746f12e3521
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.
@@ -6,7 +6,7 @@ require 'active_support'
6
6
  require_relative './json_attribute'
7
7
  require_relative './after_load_callback'
8
8
 
9
- if defined?(::ActiveRecord)
9
+ if Gem.find_files("active_record").any?
10
10
  require_relative './active_record_type'
11
11
  require_relative './active_record_encrypted_type'
12
12
  end
@@ -238,7 +238,11 @@ 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
+ if Gem.find_files("active_record").any?
242
+ @attribute_type ||= ::ActiveJsonModel::ActiveRecordType.new(self)
243
+ else
244
+ raise RuntimeError.new('ActiveRecord must be installed to use attribute_type')
245
+ end
242
246
  end
243
247
 
244
248
  # Allow this model to be used as ActiveRecord attribute type in Rails 5+.
@@ -253,7 +257,15 @@ module ActiveJsonModel
253
257
  # Note that this array_data would be stored as a string in the database, encrypted using
254
258
  # a symmetric key at the application level.
255
259
  def encrypted_attribute_type
256
- @encrypted_attribute_type ||= ActiveModelJsonSerializableEncryptedType.new(self)
260
+ if Gem.find_files("active_record").any?
261
+ if Gem.find_files("symmetric-encryption").any?
262
+ @encrypted_attribute_type ||= ::ActiveJsonModel::ActiveRecordEncryptedType.new(self)
263
+ else
264
+ raise RuntimeError.new('symmetric-encryption must be installed to use attribute_type')
265
+ end
266
+ else
267
+ raise RuntimeError.new('active_record must be installed to use attribute_type')
268
+ end
257
269
  end
258
270
  end
259
271
 
@@ -1,10 +1,11 @@
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
 
7
- if defined?(::ActiveRecord)
8
+ if Gem.find_files("active_record").any?
8
9
  require_relative './active_record_type'
9
10
  require_relative './active_record_encrypted_type'
10
11
  end
@@ -274,7 +275,11 @@ 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
+ if Gem.find_files("active_record").any?
279
+ @attribute_type ||= ::ActiveJsonModel::ActiveRecordType.new(self)
280
+ else
281
+ raise RuntimeError.new('ActiveRecord must be installed to use attribute_type')
282
+ end
278
283
  end
279
284
 
280
285
  # Allow this model to be used as ActiveRecord attribute type in Rails 5+.
@@ -289,7 +294,15 @@ module ActiveJsonModel
289
294
  # Note that this data would be stored as a string in the database, encrypted using
290
295
  # a symmetric key at the application level.
291
296
  def encrypted_attribute_type
292
- @encrypted_attribute_type ||= ActiveModelJsonSerializableEncryptedType.new(self)
297
+ if Gem.find_files("active_record").any?
298
+ if Gem.find_files("symmetric-encryption").any?
299
+ @encrypted_attribute_type ||= ::ActiveJsonModel::ActiveRecordEncryptedType.new(self)
300
+ else
301
+ raise RuntimeError.new('symmetric-encryption must be installed to use attribute_type')
302
+ end
303
+ else
304
+ raise RuntimeError.new('active_record must be installed to use attribute_type')
305
+ end
293
306
  end
294
307
  end
295
308
 
@@ -572,7 +585,7 @@ module ActiveJsonModel
572
585
  end
573
586
 
574
587
  # 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
588
+ data = json_data.is_a?(String) ? ::JSON.parse(json_data) : json_data
576
589
 
577
590
  # Recursively make the value have indifferent access
578
591
  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.1".freeze
4
+ VERSION = "0.1.4".freeze
5
5
  end
@@ -1,8 +1,13 @@
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 :ActiveRecordEncryptedType, "active_json_model/active_record_encrypted_type" unless Gem.find_files("active_record").none? || Gem.find_files("symmetric-encryption").none?
8
+ autoload :ActiveRecordType, "active_json_model/active_record_type" unless Gem.find_files("active_record").none?
9
+ autoload :Array, "active_json_model/array"
10
+ autoload :Model, "active_json_model/model"
11
+ autoload :Utils, "active_json_model/utils"
12
+ autoload :VERSION, "active_json_model/version"
8
13
  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.1
4
+ version: 0.1.4
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-03 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
  - - ">="