hexx-entities 0.0.1 → 0.1.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 +4 -4
- data/README.md +6 -0
- data/lib/hexx-entities.rb +1 -0
- data/lib/hexx/entities/base.rb +3 -19
- data/lib/hexx/entities/part.rb +57 -0
- data/lib/hexx/entities/version.rb +1 -1
- data/spec/tests/base_spec.rb +2 -16
- data/spec/tests/part_spec.rb +55 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c93ee8a3d4271e91a696427b35b8a6ba05da2a0
|
4
|
+
data.tar.gz: 6f69ca96cbb85cbafbf36a9b8946552f3cdc69b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10b1cdf1f6695351854f948df670eb34104ae9e6bce235619847d559d4a15454ad58085039fbd565d428f0ce6b0633e53252c718f9bc6d425ae19fa122d9cb3b
|
7
|
+
data.tar.gz: 417fe608dfa733f29eb8a198698cc5e2b730c07fb0af8a91bffa757a285d85d5ed7f259073a724cff8f0f9276d27254018af1c5cfd48e3985d247bba2940a6ee
|
data/README.md
CHANGED
@@ -52,12 +52,18 @@ class Item < Hexx::Entities::Base
|
|
52
52
|
end # class Item
|
53
53
|
|
54
54
|
item = Item.new foo: 1
|
55
|
+
|
56
|
+
item.frozen?
|
57
|
+
# => true
|
55
58
|
item.attributes
|
56
59
|
# => { uuids: ["31a840f4-71e2-4de1-3a6f-04d2103aa2e8"], foo: "1", bar: nil }
|
57
60
|
item.validate!
|
58
61
|
# <Attestor::InvalidError> because bar is nil
|
59
62
|
```
|
60
63
|
|
64
|
+
To model parts of aggregate entities, that have no identity by themselves,
|
65
|
+
use `Hexx::Entitites::Part`. The part is just the entity without `uuids`.
|
66
|
+
|
61
67
|
Installation
|
62
68
|
------------
|
63
69
|
|
data/lib/hexx-entities.rb
CHANGED
@@ -10,6 +10,7 @@ require "securerandom" # for uuids
|
|
10
10
|
module Hexx
|
11
11
|
|
12
12
|
require_relative "hexx/entities" # for namespace
|
13
|
+
require_relative "hexx/entities/part"
|
13
14
|
require_relative "hexx/entities/uuid"
|
14
15
|
require_relative "hexx/entities/uuids"
|
15
16
|
require_relative "hexx/entities/base"
|
data/lib/hexx/entities/base.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Hexx::Entities
|
4
4
|
|
5
|
-
# Class Base provides a common interface for entities
|
5
|
+
# Class Base provides a common interface for entities, that have identity
|
6
6
|
#
|
7
7
|
# @example
|
8
8
|
# require "hexx-entities"
|
@@ -20,10 +20,7 @@ module Hexx::Entities
|
|
20
20
|
# @author Andrew Kozin <Andrew.Kozin@gmail.com>
|
21
21
|
#
|
22
22
|
# @api public
|
23
|
-
class Base
|
24
|
-
include Attestor::Validations
|
25
|
-
include Comparable
|
26
|
-
include Eigindir
|
23
|
+
class Base < Part
|
27
24
|
|
28
25
|
# @!scope class
|
29
26
|
# @!method new(attributes)
|
@@ -36,7 +33,7 @@ module Hexx::Entities
|
|
36
33
|
# @private
|
37
34
|
def initialize(uuids: nil, **attributes)
|
38
35
|
@uuids = UUIDs.build Array(uuids)
|
39
|
-
|
36
|
+
super attributes
|
40
37
|
end
|
41
38
|
|
42
39
|
# @!attribute [rw] uuids
|
@@ -58,19 +55,6 @@ module Hexx::Entities
|
|
58
55
|
(other.uuids & uuids).any?
|
59
56
|
end
|
60
57
|
|
61
|
-
# Recursively serializes the entity to the hash
|
62
|
-
#
|
63
|
-
# Returns the hash of entity attributes, where every attribute
|
64
|
-
# that responds to `serialize` is serialized in its turn.
|
65
|
-
#
|
66
|
-
# @return [Hash]
|
67
|
-
def serialize
|
68
|
-
values = attributes.values.map do |item|
|
69
|
-
item.respond_to?(:serialize) ? item.serialize : item
|
70
|
-
end
|
71
|
-
attributes.keys.zip(values).to_h
|
72
|
-
end
|
73
|
-
|
74
58
|
end # class Base
|
75
59
|
|
76
60
|
end # module Hexx::Entities
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Hexx::Entities
|
4
|
+
|
5
|
+
# Class Part provides a common interface for parts of aggregate entities
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# require "hexx-entities"
|
9
|
+
#
|
10
|
+
# class Item < Hexx::Entities::Part
|
11
|
+
#
|
12
|
+
# # Defines attributes with their coercers
|
13
|
+
# attribute :foo, coerce: ->(value) { value.to_s }
|
14
|
+
#
|
15
|
+
# # Defines validation for the entity
|
16
|
+
# validate { invalid :blank_foo unless foo }
|
17
|
+
#
|
18
|
+
# end # class Item
|
19
|
+
#
|
20
|
+
# @author Andrew Kozin <Andrew.Kozin@gmail.com>
|
21
|
+
#
|
22
|
+
# @api public
|
23
|
+
class Part
|
24
|
+
include Attestor::Validations
|
25
|
+
include Comparable
|
26
|
+
include Eigindir
|
27
|
+
|
28
|
+
# @!scope class
|
29
|
+
# @!method new(attributes)
|
30
|
+
# Creates the immutable object
|
31
|
+
#
|
32
|
+
# @param [Hash] attributes
|
33
|
+
#
|
34
|
+
# @return [Hexx::Entities::Part]
|
35
|
+
|
36
|
+
# @private
|
37
|
+
def initialize(**attributes)
|
38
|
+
self.attributes = attributes
|
39
|
+
freeze
|
40
|
+
end
|
41
|
+
|
42
|
+
# Recursively serializes the part to the hash
|
43
|
+
#
|
44
|
+
# Returns the hash of attributes, where every attribute
|
45
|
+
# that responds to `serialize` is serialized in its turn.
|
46
|
+
#
|
47
|
+
# @return [Hash]
|
48
|
+
def serialize
|
49
|
+
values = attributes.values.map do |item|
|
50
|
+
item.respond_to?(:serialize) ? item.serialize : item
|
51
|
+
end
|
52
|
+
attributes.keys.zip(values).to_h
|
53
|
+
end
|
54
|
+
|
55
|
+
end # class Part
|
56
|
+
|
57
|
+
end # module Hexx::Entities
|
data/spec/tests/base_spec.rb
CHANGED
@@ -11,22 +11,8 @@ describe Hexx::Entities::Base do
|
|
11
11
|
|
12
12
|
describe ".new" do
|
13
13
|
|
14
|
-
it "creates
|
15
|
-
expect(subject).to be_kind_of
|
16
|
-
end
|
17
|
-
|
18
|
-
it "creates attributable object" do
|
19
|
-
expect(subject).to be_kind_of Eigindir
|
20
|
-
end
|
21
|
-
|
22
|
-
it "creates comparable object" do
|
23
|
-
expect(subject).to be_kind_of Comparable
|
24
|
-
end
|
25
|
-
|
26
|
-
it "initializes attributes from hash" do
|
27
|
-
klass.attribute :foo
|
28
|
-
subject = klass.new foo: :bar
|
29
|
-
expect(subject.foo).to eq :bar
|
14
|
+
it "creates a part" do
|
15
|
+
expect(subject).to be_kind_of Hexx::Entities::Part
|
30
16
|
end
|
31
17
|
|
32
18
|
end # describe .new
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "attestor/rspec"
|
4
|
+
|
5
|
+
describe Hexx::Entities::Part do
|
6
|
+
include Attestor::RSpec # helpers and matchers for validations
|
7
|
+
|
8
|
+
let(:klass) { Class.new(described_class) }
|
9
|
+
|
10
|
+
subject(:entity) { klass.new }
|
11
|
+
|
12
|
+
describe ".new" do
|
13
|
+
|
14
|
+
it "creates validatable object" do
|
15
|
+
expect(subject).to be_kind_of Attestor::Validations
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates attributable object" do
|
19
|
+
expect(subject).to be_kind_of Eigindir
|
20
|
+
end
|
21
|
+
|
22
|
+
it "creates comparable object" do
|
23
|
+
expect(subject).to be_kind_of Comparable
|
24
|
+
end
|
25
|
+
|
26
|
+
it "initializes attributes from hash" do
|
27
|
+
klass.attribute :foo
|
28
|
+
subject = klass.new foo: :bar
|
29
|
+
expect(subject.foo).to eq :bar
|
30
|
+
end
|
31
|
+
|
32
|
+
it "creates immutable object" do
|
33
|
+
expect(subject).to be_frozen
|
34
|
+
end
|
35
|
+
|
36
|
+
end # describe .new
|
37
|
+
|
38
|
+
describe "#serialize" do
|
39
|
+
|
40
|
+
before { klass.attribute :foo }
|
41
|
+
before { klass.attribute :bar }
|
42
|
+
|
43
|
+
let(:bar) { double }
|
44
|
+
let(:baz) { double serialize: { baz: :baz } }
|
45
|
+
let(:uuids) { [SecureRandom.uuid] }
|
46
|
+
|
47
|
+
subject(:entity) { klass.new(foo: bar, bar: baz) }
|
48
|
+
|
49
|
+
it "returns a nested hash" do
|
50
|
+
expect(entity.serialize).to eq(foo: bar, bar: { baz: :baz })
|
51
|
+
end
|
52
|
+
|
53
|
+
end # describe #to_hash
|
54
|
+
|
55
|
+
end # describe Hexx::Entities::Part
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hexx-entities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: attestor
|
@@ -87,11 +87,13 @@ files:
|
|
87
87
|
- lib/hexx-entities.rb
|
88
88
|
- lib/hexx/entities.rb
|
89
89
|
- lib/hexx/entities/base.rb
|
90
|
+
- lib/hexx/entities/part.rb
|
90
91
|
- lib/hexx/entities/uuid.rb
|
91
92
|
- lib/hexx/entities/uuids.rb
|
92
93
|
- lib/hexx/entities/version.rb
|
93
94
|
- spec/spec_helper.rb
|
94
95
|
- spec/tests/base_spec.rb
|
96
|
+
- spec/tests/part_spec.rb
|
95
97
|
- spec/tests/uuid_spec.rb
|
96
98
|
- spec/tests/uuids_spec.rb
|
97
99
|
homepage: https://github.com/nepalez/hexx-entities
|
@@ -120,6 +122,7 @@ specification_version: 4
|
|
120
122
|
summary: Base class for domain entities
|
121
123
|
test_files:
|
122
124
|
- spec/spec_helper.rb
|
125
|
+
- spec/tests/part_spec.rb
|
123
126
|
- spec/tests/uuid_spec.rb
|
124
127
|
- spec/tests/uuids_spec.rb
|
125
128
|
- spec/tests/base_spec.rb
|