dao-entity 1.0.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 +3 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dao-entity.gemspec +31 -0
- data/lib/dao/entity.rb +12 -0
- data/lib/dao/entity/base.rb +61 -0
- data/lib/dao/entity/state.rb +44 -0
- data/lib/dao/entity/version.rb +5 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42b6d9d9132f8b835b675e81e2b0db4f7671fb72
|
4
|
+
data.tar.gz: 4d2d416595294a9f8f9e7877d0bb333d20e1463c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3affef47f0c8dffd5bcd95a43d809cad170047fe962712541a599df12b59bea72c2ea6eea9ee52bfb783873d7f880a53047450b16d6e423d2dd4ef5bd26e85e
|
7
|
+
data.tar.gz: 8e61d2ea6136e356df1989a612584ece23d08ff5a0ee0707bd94d69cb697409b86b884b991210711b657f57678da9a9df602af58237e0efa968b43f49ff70e0c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 llxff
|
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,64 @@
|
|
1
|
+
# Dao::Entity
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/dao-rb/dao-entity.svg?branch=master)](https://travis-ci.org/dao-rb/dao-entity)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/dao-rb/dao-entity/badges/gpa.svg)](https://codeclimate.com/github/dao-rb/dao-entity)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/dao-rb/dao-entity/badges/coverage.svg)](https://codeclimate.com/github/dao-rb/dao-entity/coverage)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dao-entity'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dao-entity
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'dao/entity'
|
27
|
+
|
28
|
+
class CommentEntity < Dao::Entity::Base
|
29
|
+
attribute :body, String
|
30
|
+
end
|
31
|
+
|
32
|
+
class PostEntity < Dao::Entity::Base
|
33
|
+
attribute :id, Integer
|
34
|
+
attribute :body, String
|
35
|
+
|
36
|
+
attribute :comments, Array[CommentEntity]
|
37
|
+
end
|
38
|
+
|
39
|
+
post = PostEntity.new(id: 1, body: 'Post body', comments: [{ id: 1, body: 'Comment body' }])
|
40
|
+
|
41
|
+
post.id # => 1
|
42
|
+
post.body # => "Post body"
|
43
|
+
post.comments # => [#<CommentEntity:0x007ffdcb923a30>]
|
44
|
+
|
45
|
+
post.body = 'New body'
|
46
|
+
post.entity_state.body_changed? # => true
|
47
|
+
post.entity_state.body_was # => "Post body"
|
48
|
+
```
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
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.
|
53
|
+
|
54
|
+
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).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dao-rb/dao-entity.
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dao/entity"
|
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
|
data/bin/setup
ADDED
data/dao-entity.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'dao/entity/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'dao-entity'
|
7
|
+
spec.version = Dao::Entity::VERSION
|
8
|
+
spec.authors = ['llxff', 'ssnikolay']
|
9
|
+
spec.email = ['ll.wg.bin@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Base entity'
|
12
|
+
spec.description = 'Base entity'
|
13
|
+
spec.homepage = 'https://github.com/dao-rb/dao-entity'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.2.2'
|
22
|
+
|
23
|
+
spec.add_dependency 'virtus', '~> 1.0'
|
24
|
+
spec.add_dependency 'activemodel', '>= 4.2'
|
25
|
+
spec.add_dependency 'activesupport', '>= 4.2'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
30
|
+
spec.add_development_dependency 'rspec-its'
|
31
|
+
end
|
data/lib/dao/entity.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Dao
|
2
|
+
module Entity
|
3
|
+
class Base
|
4
|
+
include Virtus.model
|
5
|
+
|
6
|
+
attr_accessor :initialized_with
|
7
|
+
|
8
|
+
def initialize(attrs = {})
|
9
|
+
super
|
10
|
+
@initialized_with = []
|
11
|
+
@entity_state = self.class.state_class.new(attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialized_with=(attrs)
|
15
|
+
@initialized_with = attrs.presence || []
|
16
|
+
end
|
17
|
+
|
18
|
+
def persisted?
|
19
|
+
id.present? if respond_to?(:id)
|
20
|
+
end
|
21
|
+
|
22
|
+
def entity_state
|
23
|
+
@entity_state.tap { |s| s.assign_attributes(attributes) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_key
|
27
|
+
key = respond_to?(:id) && id
|
28
|
+
key ? [key] : nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def hash
|
32
|
+
"#{ self.class.name }/#{ identity }".hash
|
33
|
+
end
|
34
|
+
|
35
|
+
def ==(other)
|
36
|
+
hash == other.hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def <=>(other)
|
40
|
+
id <=> other.id
|
41
|
+
end
|
42
|
+
|
43
|
+
alias eql? ==
|
44
|
+
alias equal? eql?
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def self.state_class
|
49
|
+
@state_class ||= State.build_with_attrs(attribute_set.collect(&:name))
|
50
|
+
end
|
51
|
+
|
52
|
+
def identity
|
53
|
+
if respond_to?(:id)
|
54
|
+
id
|
55
|
+
else
|
56
|
+
object_id
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Dao
|
2
|
+
module Entity
|
3
|
+
class State
|
4
|
+
include ActiveModel::Dirty
|
5
|
+
|
6
|
+
def self.build_with_attrs(attrs)
|
7
|
+
state = Class.new(self)
|
8
|
+
|
9
|
+
state.define_attribute_methods *attrs
|
10
|
+
|
11
|
+
state.send(:attr_accessor, *attrs)
|
12
|
+
attrs.each do |attr|
|
13
|
+
state.send(:define_method, "#{ attr }=") do |val|
|
14
|
+
if public_send(attr) != val
|
15
|
+
attribute_will_change!(attr)
|
16
|
+
instance_variable_set("@#{ attr}", val)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
state
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(params={})
|
25
|
+
assign_attributes(params)
|
26
|
+
changes_applied
|
27
|
+
end
|
28
|
+
|
29
|
+
def assign_attributes(params)
|
30
|
+
params.each do |attr, value|
|
31
|
+
self.public_send("#{attr}=", value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def reload!
|
36
|
+
clear_changes_information
|
37
|
+
end
|
38
|
+
|
39
|
+
def rollback!
|
40
|
+
restore_attributes
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dao-entity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- llxff
|
8
|
+
- ssnikolay
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-06-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: virtus
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activemodel
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '4.2'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '4.2'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: activesupport
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '4.2'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '4.2'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bundler
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1.11'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.11'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '10.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '10.0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '3.4'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '3.4'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rspec-its
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
description: Base entity
|
113
|
+
email:
|
114
|
+
- ll.wg.bin@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- dao-entity.gemspec
|
129
|
+
- lib/dao/entity.rb
|
130
|
+
- lib/dao/entity/base.rb
|
131
|
+
- lib/dao/entity/state.rb
|
132
|
+
- lib/dao/entity/version.rb
|
133
|
+
homepage: https://github.com/dao-rb/dao-entity
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 2.2.2
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.5.1
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Base entity
|
157
|
+
test_files: []
|