embedded_model 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/embedded_model.gemspec +24 -0
- data/lib/embedded_model/active_record_extension.rb +15 -0
- data/lib/embedded_model/embedder.rb +39 -0
- data/lib/embedded_model/type.rb +27 -0
- data/lib/embedded_model/version.rb +3 -0
- data/lib/embedded_model.rb +8 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02293fdb185c52cf9e47900b15c6671242a71cc1
|
4
|
+
data.tar.gz: 7196be5ee607c4a4424411569d274c3ffb424dab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 046380f5d754a6a53c953dd29805f0cc3910ad3168a2025a945a04a33f1e855849a3bd8d621e48201b121606c603ee8496333f9fc99180c41241cae823c6ba68
|
7
|
+
data.tar.gz: 9bed4cb001562418f5802374b1f162a2a72bd06c1fcc5d8e2698173b4068c483ef8801f4290ea47f05003658b81661d430351703902695548e710677dbd1cc08
|
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) 2016 Nikolai Vasilenko
|
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,101 @@
|
|
1
|
+
# EmbeddedModel
|
2
|
+
|
3
|
+
A simple extension for ActiveRecord allowing to embed plain-models and store them as JSON. It's well-suited in case the use of full-featured models with underlying tables is redundant.
|
4
|
+
|
5
|
+
Works with ActiveRecord ~> 4.2. Support of the 5th version has not been implemented yet.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'embedded_model'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install embedded_model
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Embeddable models
|
26
|
+
|
27
|
+
Any class allowing to populate their attributes by passing hash into constructor will suit.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Person
|
31
|
+
attr_accessor :name
|
32
|
+
|
33
|
+
def initialize(attrs = {})
|
34
|
+
@name = attrs.with_indifferent_access['name']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
I recommend using [Virtus](https://github.com/solnic/virtus).
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class Person
|
43
|
+
include Virtus.model
|
44
|
+
|
45
|
+
attribute :name, String
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Embedding into container model
|
50
|
+
|
51
|
+
Add columns to be used as storage for embedded objects. If your database supports JSON (e.g. PostgreSQL), use type `json`, otherwise use `text`.
|
52
|
+
|
53
|
+
Use `embeds_one` method for embedding single objects and `embeds_many` for collections.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Department < ActiveRecord::Base
|
57
|
+
embeds_one :boss
|
58
|
+
embeds_many :employees
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
By default, names of the embeddable models are inferred from the column names. Should your classes have other names, it is necessary to specify them explicitly.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Party < ActiveRecord::Base
|
66
|
+
embeds_one :tank, class_name: 'Player'
|
67
|
+
embeds_many :dps, class_name: 'Player'
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Example
|
72
|
+
|
73
|
+
As soon as everything gets done, it simply works.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
department_params = {
|
77
|
+
boss: { name: 'Todd' },
|
78
|
+
employees: [{ name: 'Andy' }, { name: 'Jim' }]
|
79
|
+
}
|
80
|
+
|
81
|
+
department = Department.new(department_params)
|
82
|
+
|
83
|
+
# Do something with your model objects
|
84
|
+
department.boss.command('Work!') # => Todd commands 'Work!'
|
85
|
+
department.employees.each(&:work) # => Andy works hard Jim works hard
|
86
|
+
|
87
|
+
department.save
|
88
|
+
department.reload
|
89
|
+
|
90
|
+
# Do something else
|
91
|
+
department.boss.command('Good job!') # => Todd commands 'Good job!'
|
92
|
+
department.employees.each(&:go_home) # => Andy goes home Jim goes home
|
93
|
+
```
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vasilenko/embedded_model.
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'embedded_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'embedded_model'
|
8
|
+
spec.version = EmbeddedModel::VERSION
|
9
|
+
spec.authors = ['Nikolai Vasilenko']
|
10
|
+
spec.email = ['colormailbox@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Embeds your PORO models into ActiveRecord ones and stores them as JSON'
|
13
|
+
spec.homepage = 'https://github.com/vasilenko/embedded_model'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_dependency 'activerecord', '~> 4.2'
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
spec.add_development_dependency 'sqlite3', '~> 1.0'
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EmbeddedModel
|
2
|
+
module ActiveRecordExtension
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def embeds_one(column, options = {})
|
7
|
+
EmbeddedModel::Embedder.new(self, column, options).embed_object
|
8
|
+
end
|
9
|
+
|
10
|
+
def embeds_many(column, options = {})
|
11
|
+
EmbeddedModel::Embedder.new(self, column, options).embed_collection
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module EmbeddedModel
|
2
|
+
class Embedder
|
3
|
+
def initialize(container, column, options = {})
|
4
|
+
@container = container
|
5
|
+
@column = column
|
6
|
+
@class_name = options[:class_name]
|
7
|
+
end
|
8
|
+
|
9
|
+
def embed_object
|
10
|
+
embed object_builder
|
11
|
+
end
|
12
|
+
|
13
|
+
def embed_collection
|
14
|
+
embed collection_builder, default: []
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def embed(builder, options = {})
|
20
|
+
@container.attribute(@column, EmbeddedModel::Type.new(&builder), options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def model
|
24
|
+
@model ||=
|
25
|
+
begin
|
26
|
+
return @class_name if @class_name.is_a?(Class)
|
27
|
+
(@class_name || @column).to_s.classify.constantize
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def object_builder
|
32
|
+
->(attrs) { attrs.is_a?(model) ? attrs : model.new(attrs) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def collection_builder
|
36
|
+
->(arr) { arr.map(&object_builder) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_record/connection_adapters/postgresql/oid/json'
|
2
|
+
|
3
|
+
module EmbeddedModel
|
4
|
+
class Type < ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json
|
5
|
+
def initialize(&builder)
|
6
|
+
@builder = builder
|
7
|
+
end
|
8
|
+
|
9
|
+
def type_cast_from_database(value)
|
10
|
+
type_cast(super)
|
11
|
+
end
|
12
|
+
|
13
|
+
def type_cast_from_user(value)
|
14
|
+
type_cast(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def type_cast_for_database(value)
|
18
|
+
super(value.as_json)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def cast_value(value)
|
24
|
+
@builder.call(value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embedded_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikolai Vasilenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- colormailbox@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- embedded_model.gemspec
|
82
|
+
- lib/embedded_model.rb
|
83
|
+
- lib/embedded_model/active_record_extension.rb
|
84
|
+
- lib/embedded_model/embedder.rb
|
85
|
+
- lib/embedded_model/type.rb
|
86
|
+
- lib/embedded_model/version.rb
|
87
|
+
homepage: https://github.com/vasilenko/embedded_model
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.5.1
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Embeds your PORO models into ActiveRecord ones and stores them as JSON
|
111
|
+
test_files: []
|