document_serializable 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 +12 -0
- data/.rspec +2 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/document_serializable.gemspec +25 -0
- data/lib/document_serializable.rb +48 -0
- data/lib/document_serializable/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e159598f8b640d262643ee4b08e96219d853bb78
|
4
|
+
data.tar.gz: 9739d792e201b54b906f3557057adb52a5956fc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 543eba20857ad0ecf565ee6c42fc15b26ac4522b501106ba06e7908ddfb411c068864373afed42caacf5b844720f622e1290213430f2e6001cacb571ca34868b
|
7
|
+
data.tar.gz: d72a1d0271c2187423c992d9eff3c1a36117560187a801a66b7cfdce16e98c42ec170227f594836c6fda9d49a780b71aca9b2e8e618bdb81b4f6b51801887860
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Alex
|
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,77 @@
|
|
1
|
+
# DocumentSerializable
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/nerdgeschoss/document_serializable/tree/master.svg?style=svg)](https://circleci.com/gh/nerdgeschoss/document_serializable/tree/master)
|
4
|
+
|
5
|
+
Serialize your object hierarchy in a document based style to your relational database via virtus.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'document_serializable'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install document_serializable
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Define your model:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
rails g model Invoice properties:jsonb:index
|
29
|
+
```
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Address
|
33
|
+
include Virtus.model
|
34
|
+
|
35
|
+
attribute :name
|
36
|
+
attribute :city
|
37
|
+
end
|
38
|
+
|
39
|
+
class Invoice < ApplicationRecord
|
40
|
+
include DocumentSerializable
|
41
|
+
|
42
|
+
attribute address, Address
|
43
|
+
attribute subject
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Then initialize it with content and access attributes directly from your model:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
invoice = Invoice.new subject: "Pay me!", address: { name: "Jon Doe", city: "New York" }
|
51
|
+
invoice.address.name # Jon Doe
|
52
|
+
invoice.subject # Pay me!
|
53
|
+
invoice.save!
|
54
|
+
```
|
55
|
+
|
56
|
+
This works for all models that have a serialized attribute named `properties` (e.g. json column in MySQL or jsonb in Postgres).
|
57
|
+
|
58
|
+
You can also query for properties (in Postgres):
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Invoice.where("properties @> ?", { address: { city: "New York" } }.to_json)
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Alex/document_serializable.
|
73
|
+
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "document_serializable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'document_serializable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "document_serializable"
|
8
|
+
spec.version = DocumentSerializable::VERSION
|
9
|
+
spec.authors = ["Alex"]
|
10
|
+
spec.email = ["aleksandra@nerdgeschoss.de"]
|
11
|
+
|
12
|
+
spec.summary = "Document serializer"
|
13
|
+
spec.homepage = "https://github.com/nerdgeschoss/document_serializable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "activesupport"
|
24
|
+
spec.add_dependency "virtus"
|
25
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "document_serializable/version"
|
2
|
+
require "active_support"
|
3
|
+
require "active_support/core_ext"
|
4
|
+
require "virtus"
|
5
|
+
|
6
|
+
module DocumentSerializable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def attributes
|
11
|
+
property_object.attributes.merge(super)
|
12
|
+
end
|
13
|
+
|
14
|
+
def reload(options = nil)
|
15
|
+
super
|
16
|
+
@property_object = nil
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def property_object
|
23
|
+
@property_object ||= begin
|
24
|
+
properties = self.properties
|
25
|
+
properties = JSON.parse(properties) if properties.is_a? String
|
26
|
+
self.class.property_class.new(properties || {})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
before_validation do
|
31
|
+
self.properties = property_object.attributes
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class_methods do
|
36
|
+
def property_class
|
37
|
+
@property_class ||= begin
|
38
|
+
Class.new(superclass.try(:property_class) || Object) { include Virtus.model(nullify_blank: true) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def attribute(name, type = String, options = {})
|
43
|
+
property_class.send :attribute, name, type, options
|
44
|
+
delegate name, to: :property_object
|
45
|
+
delegate "#{name}=".to_sym, to: :property_object
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: document_serializable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: virtus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- aleksandra@nerdgeschoss.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- document_serializable.gemspec
|
57
|
+
- lib/document_serializable.rb
|
58
|
+
- lib/document_serializable/version.rb
|
59
|
+
homepage: https://github.com/nerdgeschoss/document_serializable
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.6.8
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Document serializer
|
83
|
+
test_files: []
|