virtus-group 0.0.1
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 +15 -0
- data/.gitignore +18 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +4 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/Rakefile +8 -0
- data/lib/virtus/group.rb +43 -0
- data/lib/virtus/group/attribute_tracker.rb +25 -0
- data/lib/virtus/group/version.rb +5 -0
- data/spec/integration/virtus_group_integration_spec.rb +76 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/unit/group/attribute_tracker_spec.rb +26 -0
- data/spec/unit/virtus_group_spec.rb +47 -0
- data/virtus-group.gemspec +23 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NDRlZmRjNDc5MWI5MGZmNGRiOGUzMTMzY2NjNjAyNDY4OTU1NTkwZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YzEzMDgyODNlYjJjNmNjYjNlZjhiNTA1NDQ0NTFjZjA4MTdkNjMwNg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MzFiYmRlNjJjNjZmZjZkZDMwNzFhYWRkOGI1NzBhNmU1YTkwN2NlYjk0Zjlh
|
10
|
+
OWMwYjUwM2Q2M2Q0NDJmYzc1ODI0YWVhYmY2MGMwZWM2NzlmNjlkNDBmYWUx
|
11
|
+
MjZlM2I0ZmJmNWUwOGQ2ODYwMWU5ZDRjOWVlZGM4MjhlYmQ0NmQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjZhMzllNWM5NTdiOGRiZGZhNGY3ODY3MDk3ZmNhMjE0YTdmMTc3OTY2NjFm
|
14
|
+
OTA3MGQ2NDIzM2I1YWQ1ODk2YjkyMDZkMWZkMTNjYWZkNzNjOGIyNWNmODNk
|
15
|
+
ZGE4MmU1MTYxZWNiMmU2NmU0M2JhZDJlMDI1MWNmMGYwOTgxMDI=
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
virtus-group
|
data/.ruby-version
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Spas Poptchev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Virtus.group
|
2
|
+
|
3
|
+
The idea of this gem is to define groups over virtus attributes.
|
4
|
+
Which is especially useful when you are working with [form objects](http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'virtus-group'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install virtus-group
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class SignUpForm
|
24
|
+
include Virtus.model
|
25
|
+
include Virtus.group
|
26
|
+
|
27
|
+
group :user do
|
28
|
+
attribute :email, String
|
29
|
+
attribute :password, String
|
30
|
+
end
|
31
|
+
|
32
|
+
group :address do
|
33
|
+
attribute :street, String
|
34
|
+
attribute :zipcode, String
|
35
|
+
attribute :city, String
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Don't worry, the `SignUpForm` class will still behave like a regular Virtus class.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# ---
|
44
|
+
|
45
|
+
SignUpForm.attribute_group
|
46
|
+
# => {user: [:email, :password], address: [:street, :zipcode, :city]}
|
47
|
+
|
48
|
+
SignUpForm.attribute_group[:user]
|
49
|
+
# => [:email, :password]
|
50
|
+
|
51
|
+
SignUpForm.attribute_group[:address]
|
52
|
+
# => [:street, :zipcode, :city]
|
53
|
+
|
54
|
+
# ---
|
55
|
+
|
56
|
+
sign_up_form = SignUpForm.new({
|
57
|
+
email: 'john@example.com', password: '12344321',
|
58
|
+
street: 'ABC-Street 1', zipcode: 'Z1234', city: 'ABC'
|
59
|
+
})
|
60
|
+
|
61
|
+
sign_up_form.attributes_for :user
|
62
|
+
# => {email: 'john@example.com', password: '12344321'}
|
63
|
+
|
64
|
+
sign_up_form.attributes_for :address
|
65
|
+
# => {street: 'ABC-Street 1', zipcode: 'Z1234', city: 'ABC'}
|
66
|
+
```
|
67
|
+
|
68
|
+
Let's say you have a `create` method on your `SignUpForm` you can use the attribute groups to create your models:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class SignUpForm
|
72
|
+
include Virtus.model
|
73
|
+
include Virtus.group
|
74
|
+
|
75
|
+
group :user do
|
76
|
+
attribute :email, String
|
77
|
+
attribute :password, String
|
78
|
+
end
|
79
|
+
|
80
|
+
group :address do
|
81
|
+
attribute :street, String
|
82
|
+
attribute :zipcode, String
|
83
|
+
attribute :city, String
|
84
|
+
end
|
85
|
+
|
86
|
+
def create
|
87
|
+
user = User.build(attributes_for(:user))
|
88
|
+
user.build_address(attributes_for(:address))
|
89
|
+
user.save
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it ( http://github.com/spoptchev/virtus-group/fork )
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create new Pull Request
|
103
|
+
|
104
|
+
|
data/Rakefile
ADDED
data/lib/virtus/group.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "virtus/group/version"
|
2
|
+
require "virtus/group/attribute_tracker"
|
3
|
+
|
4
|
+
module Virtus
|
5
|
+
|
6
|
+
# stick to virtus api
|
7
|
+
def self.group
|
8
|
+
mod = Module.new
|
9
|
+
mod.define_singleton_method :included do |object|
|
10
|
+
object.send(:include, Group)
|
11
|
+
end
|
12
|
+
mod
|
13
|
+
end
|
14
|
+
|
15
|
+
module Group
|
16
|
+
|
17
|
+
def self.included(base)
|
18
|
+
base.class_eval do
|
19
|
+
extend ClassMethods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
|
25
|
+
def group(name, &block)
|
26
|
+
attribute_tracker = AttributeTracker.new(self, &block)
|
27
|
+
attribute_group[name] = attribute_tracker.tracked_attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
def attribute_group
|
31
|
+
@attribute_group ||= {}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def attributes_for(group_name)
|
37
|
+
attributes_in_group = self.class.attribute_group[group_name.to_sym]
|
38
|
+
self.attributes.select{|attribute, _| attributes_in_group.include?(attribute)}
|
39
|
+
end
|
40
|
+
alias :with_attributes_for :attributes_for
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Virtus
|
4
|
+
module Group
|
5
|
+
|
6
|
+
class AttributeTracker < SimpleDelegator
|
7
|
+
|
8
|
+
def initialize(clazz, &block)
|
9
|
+
super(clazz)
|
10
|
+
instance_eval(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def attribute(*args)
|
14
|
+
tracked_attributes << args.first.to_sym
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def tracked_attributes
|
19
|
+
@tracked_attributes ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
shared_examples 'a virtus object' do
|
4
|
+
it "should return the attributes" do
|
5
|
+
expect(model.attributes).to eq attributes
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
shared_examples 'an object with a virtus group' do
|
10
|
+
let(:model) { model_class.new(attributes) }
|
11
|
+
|
12
|
+
describe "#attributes_for" do
|
13
|
+
it "should return the attributes for the user group" do
|
14
|
+
expect(model.attributes_for(:user)).to eq user_attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not return any attributes of the address group" do
|
18
|
+
expect(model.attributes_for(:user)).not_to include address_attributes
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return the attributes for the address group" do
|
22
|
+
expect(model.attributes_for(:address)).to eq address_attributes
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not return any attributes for the user group" do
|
26
|
+
expect(model.attributes_for(:address)).not_to include user_attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it_behaves_like "a virtus object"
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Virtus::Group do
|
34
|
+
|
35
|
+
let(:user_attributes) { Hash[email: 'john@example.com'] }
|
36
|
+
let(:address_attributes) { Hash[city: 'Home'] }
|
37
|
+
let(:attributes) { user_attributes.merge(address_attributes) }
|
38
|
+
|
39
|
+
it_behaves_like 'an object with a virtus group' do
|
40
|
+
let(:model_class) do
|
41
|
+
Class.new do
|
42
|
+
include Virtus.model
|
43
|
+
include Virtus.group
|
44
|
+
|
45
|
+
group :user do
|
46
|
+
attribute :email, String
|
47
|
+
end
|
48
|
+
|
49
|
+
group :address do
|
50
|
+
attribute :city, String
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it_behaves_like 'an object with a virtus group' do
|
57
|
+
let(:model_class) do
|
58
|
+
Class.new do
|
59
|
+
include Virtus.value_object
|
60
|
+
include Virtus.group
|
61
|
+
|
62
|
+
values do
|
63
|
+
group :user do
|
64
|
+
attribute :email, String
|
65
|
+
end
|
66
|
+
|
67
|
+
group :address do
|
68
|
+
attribute :city, String
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Virtus::Group::AttributeTracker do
|
4
|
+
|
5
|
+
let(:model_class) { Class.new.send(:include, Virtus.group) }
|
6
|
+
|
7
|
+
describe "#attribute" do
|
8
|
+
let(:proc) { Proc.new { attribute(:email, String) } }
|
9
|
+
|
10
|
+
it "should add the attribute to tracked_attributes" do
|
11
|
+
expect(model_class).to receive(:attribute).with(:email, String)
|
12
|
+
instance = described_class.new(model_class, &proc)
|
13
|
+
expect(instance.tracked_attributes).to include :email
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#tracked_attributes" do
|
18
|
+
let(:proc) { Proc.new {} }
|
19
|
+
let(:instance) { described_class.new(model_class, &proc) }
|
20
|
+
|
21
|
+
it "should return an empty array" do
|
22
|
+
expect(instance.tracked_attributes).to eq []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Virtus::Group do
|
4
|
+
|
5
|
+
let(:model_class) { Class.new.send(:include, Virtus.group) }
|
6
|
+
|
7
|
+
describe ".group" do
|
8
|
+
let(:attribute_tracker_instance) { double(:attribute_tracker_instance) }
|
9
|
+
let(:proc) { Proc.new {} }
|
10
|
+
let(:tracked_attributes) { [:email] }
|
11
|
+
let(:group_name) { :user }
|
12
|
+
|
13
|
+
it "should add tracked attributes to an attribute group" do
|
14
|
+
expect(Virtus::Group::AttributeTracker).to receive(:new).once.
|
15
|
+
with(model_class, &proc).and_return(attribute_tracker_instance)
|
16
|
+
|
17
|
+
expect(attribute_tracker_instance).to receive(:tracked_attributes).once.and_return(tracked_attributes)
|
18
|
+
|
19
|
+
expect(model_class.group(group_name, &proc)).to eq tracked_attributes
|
20
|
+
expect(model_class.attribute_group[group_name]).to eq tracked_attributes
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".attribute_group" do
|
25
|
+
it "should return an empty hash" do
|
26
|
+
expect(model_class.attribute_group).to eq Hash.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#attributes_for" do
|
31
|
+
let(:attribute_group) { Hash[user: [:email, :password], address: [:city]] }
|
32
|
+
let(:instance) { model_class.new }
|
33
|
+
let(:attributes) { Hash[email: 'john@example.com', password: '12344321', city: 'Home'] }
|
34
|
+
|
35
|
+
it "should return all attributes and their values in the selected group" do
|
36
|
+
expect(model_class).to receive(:attribute_group).once.and_return(attribute_group)
|
37
|
+
expect(instance).to receive(:attributes).once.and_return(attributes)
|
38
|
+
|
39
|
+
result = instance.attributes_for(:user)
|
40
|
+
expect(result).to eq Hash[email: 'john@example.com', password: '12344321']
|
41
|
+
expect(result).not_to include :city
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../lib/virtus/group/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "virtus-group"
|
7
|
+
spec.version = Virtus::Group::VERSION
|
8
|
+
spec.authors = ["Spas Poptchev"]
|
9
|
+
spec.email = ["spas.poptchev@me.com"]
|
10
|
+
spec.description = %q{Define groups over virtus attributes.}
|
11
|
+
spec.summary = spec.description
|
12
|
+
spec.homepage = "https://github.com/spoptchev/virtus-group"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "virtus", ">= 1.0.0"
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.1"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: virtus-group
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Spas Poptchev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: virtus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
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.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
description: Define groups over virtus attributes.
|
56
|
+
email:
|
57
|
+
- spas.poptchev@me.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .ruby-gemset
|
64
|
+
- .ruby-version
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/virtus/group.rb
|
70
|
+
- lib/virtus/group/attribute_tracker.rb
|
71
|
+
- lib/virtus/group/version.rb
|
72
|
+
- spec/integration/virtus_group_integration_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/unit/group/attribute_tracker_spec.rb
|
75
|
+
- spec/unit/virtus_group_spec.rb
|
76
|
+
- virtus-group.gemspec
|
77
|
+
homepage: https://github.com/spoptchev/virtus-group
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Define groups over virtus attributes.
|
101
|
+
test_files:
|
102
|
+
- spec/integration/virtus_group_integration_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/unit/group/attribute_tracker_spec.rb
|
105
|
+
- spec/unit/virtus_group_spec.rb
|