holotype 0.15.1 → 0.16.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 +4 -4
- data/lib/holotype.rb +134 -132
- data/lib/holotype/attribute.rb +1 -1
- data/lib/holotype/attribute/definition.rb +1 -1
- data/lib/holotype/attribute/definition/default_conflict_error.rb +1 -1
- data/lib/holotype/attribute/definition/no_collection_class_error.rb +1 -1
- data/lib/holotype/attribute/definition/no_value_class_error.rb +1 -1
- data/lib/holotype/attribute/definition/required_conflict_error.rb +1 -1
- data/lib/holotype/attribute/frozen_modification_error.rb +1 -1
- data/lib/holotype/attribute/immutable_value_error.rb +1 -1
- data/lib/holotype/attribute/read_only_error.rb +1 -1
- data/lib/holotype/attributes_already_defined_error.rb +1 -1
- data/lib/holotype/collection_normalizer.rb +1 -1
- data/lib/holotype/collection_normalizer/expected_array_like_collection_error.rb +1 -1
- data/lib/holotype/collection_normalizer/expected_hash_like_collection_error.rb +1 -1
- data/lib/holotype/inheritance_disallowed_error.rb +1 -1
- data/lib/holotype/missing_required_attributes_error.rb +1 -1
- data/lib/holotype/value_normalizer.rb +1 -1
- data/lib/holotype/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d54a2791610a762b82681d5430e8929972bff81
|
4
|
+
data.tar.gz: ef327df4073a1606b166594a25a286d5b3d47bc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 286f0bce02c86eb051673ec89c9f3d2baf8558edf80a91236a6815b09c6fcf339b88282f5650ab6ebf31a745d390b981b0ea200aca065388543cbfe563a30035
|
7
|
+
data.tar.gz: 54045ed8c97431213da07a8fccf7e23daee257607b592ba136416a363caa15bd37383d3a88294cce73ad703f1e2c6a0dbc663bb3f2b378276447d8972b3a55e2
|
data/lib/holotype.rb
CHANGED
@@ -12,171 +12,173 @@
|
|
12
12
|
version
|
13
13
|
].each { |name| require_relative "holotype/#{name}.rb" }
|
14
14
|
|
15
|
-
|
16
|
-
# Singleton Definition
|
17
|
-
|
15
|
+
module Holotype
|
18
16
|
class << self
|
19
|
-
def
|
20
|
-
|
21
|
-
name = name.to_sym
|
22
|
-
|
23
|
-
# prepare options
|
24
|
-
processed_options = if immutable?
|
25
|
-
[
|
26
|
-
__default_attribute_options,
|
27
|
-
options,
|
28
|
-
IMMUTABLE_OPTION,
|
29
|
-
].reduce :merge
|
30
|
-
else
|
31
|
-
[
|
32
|
-
__default_attribute_options,
|
33
|
-
options,
|
34
|
-
].reduce :merge
|
35
|
-
end
|
36
|
-
|
37
|
-
# create attribute definition
|
38
|
-
attribute = Attribute::Definition.new name,
|
39
|
-
**processed_options,
|
40
|
-
&default
|
41
|
-
|
42
|
-
# store the attribute definition
|
43
|
-
attributes[name] = attribute
|
44
|
-
|
45
|
-
# create an attribute reader
|
46
|
-
define_method name do
|
47
|
-
self.attributes[name].value
|
48
|
-
end
|
49
|
-
|
50
|
-
# create an attribute writer
|
51
|
-
define_method "#{name}=" do |value|
|
52
|
-
self.attributes[name].value = value
|
53
|
-
end
|
17
|
+
def extended base
|
18
|
+
base.send :include, InstanceMethods
|
54
19
|
end
|
20
|
+
end
|
55
21
|
|
56
|
-
|
57
|
-
|
22
|
+
def attribute name, **options, &default
|
23
|
+
# symbolize name
|
24
|
+
name = name.to_sym
|
25
|
+
|
26
|
+
# prepare options
|
27
|
+
processed_options = if immutable?
|
28
|
+
[
|
29
|
+
__default_attribute_options,
|
30
|
+
options,
|
31
|
+
IMMUTABLE_OPTION,
|
32
|
+
].reduce :merge
|
33
|
+
else
|
34
|
+
[
|
35
|
+
__default_attribute_options,
|
36
|
+
options,
|
37
|
+
].reduce :merge
|
38
|
+
end
|
39
|
+
|
40
|
+
# create attribute definition
|
41
|
+
attribute = Attribute::Definition.new name,
|
42
|
+
**processed_options,
|
43
|
+
&default
|
44
|
+
|
45
|
+
# store the attribute definition
|
46
|
+
attributes[name] = attribute
|
47
|
+
|
48
|
+
# create an attribute reader
|
49
|
+
define_method name do
|
50
|
+
self.attributes[name].value
|
58
51
|
end
|
59
52
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
define_singleton_method :inherited do |_|
|
64
|
-
raise InheritanceDisallowedError.new
|
65
|
-
end
|
66
|
-
|
67
|
-
@immutable = true
|
53
|
+
# create an attribute writer
|
54
|
+
define_method "#{name}=" do |value|
|
55
|
+
self.attributes[name].value = value
|
68
56
|
end
|
57
|
+
end
|
69
58
|
|
70
|
-
|
71
|
-
|
72
|
-
|
59
|
+
def attributes
|
60
|
+
@attributes ||= Hash[]
|
61
|
+
end
|
73
62
|
|
74
|
-
|
75
|
-
|
63
|
+
def make_immutable
|
64
|
+
raise AttributesAlreadyDefinedError.new if attributes.count != 0
|
65
|
+
|
66
|
+
define_singleton_method :inherited do |_|
|
67
|
+
raise InheritanceDisallowedError.new
|
76
68
|
end
|
77
69
|
|
78
|
-
|
70
|
+
@immutable = true
|
71
|
+
end
|
79
72
|
|
80
|
-
|
73
|
+
def immutable?
|
74
|
+
!!@immutable
|
75
|
+
end
|
81
76
|
|
82
|
-
|
83
|
-
|
84
|
-
end
|
77
|
+
def default_attribute_options **options
|
78
|
+
@default_attribute_options = options.freeze
|
85
79
|
end
|
86
80
|
|
87
|
-
|
81
|
+
private
|
88
82
|
|
89
|
-
|
83
|
+
IMMUTABLE_OPTION = Hash[immutable: true].freeze
|
90
84
|
|
91
|
-
def
|
92
|
-
|
93
|
-
__holotype_store attributes
|
85
|
+
def __default_attribute_options
|
86
|
+
@default_attribute_options || Hash[]
|
94
87
|
end
|
95
88
|
|
96
|
-
|
97
|
-
|
98
|
-
end
|
89
|
+
module InstanceMethods
|
90
|
+
attr_reader :attributes
|
99
91
|
|
100
|
-
|
101
|
-
|
102
|
-
attributes
|
103
|
-
|
104
|
-
definition = attribute.definition
|
92
|
+
def initialize **attributes
|
93
|
+
__holotype_check_for_missing_required attributes
|
94
|
+
__holotype_store attributes
|
95
|
+
end
|
105
96
|
|
106
|
-
|
97
|
+
def frozen?
|
98
|
+
true
|
99
|
+
end
|
107
100
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
101
|
+
def to_hash
|
102
|
+
Hash[
|
103
|
+
attributes
|
104
|
+
.map do |key, attribute|
|
105
|
+
definition = attribute.definition
|
112
106
|
|
113
|
-
|
114
|
-
return false unless self.class == other.class
|
107
|
+
value = __holotype_hashify attribute.value
|
115
108
|
|
116
|
-
|
117
|
-
|
109
|
+
[key, value]
|
110
|
+
end
|
111
|
+
]
|
118
112
|
end
|
119
|
-
end
|
120
113
|
|
121
|
-
|
122
|
-
|
123
|
-
end
|
114
|
+
def == other
|
115
|
+
return false unless self.class == other.class
|
124
116
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
117
|
+
attributes.all? do |name, attribute|
|
118
|
+
attribute.value == other.attributes[name].value
|
119
|
+
end
|
120
|
+
end
|
129
121
|
|
130
|
-
|
131
|
-
|
122
|
+
def with **attributes
|
123
|
+
self.class.new to_hash.merge attributes
|
124
|
+
end
|
132
125
|
|
133
|
-
|
126
|
+
def inspect
|
127
|
+
data = to_hash
|
128
|
+
.map { |attribute, value| "#{attribute}: #{value.inspect}" }
|
129
|
+
.join(', ')
|
134
130
|
|
135
|
-
|
136
|
-
if value.respond_to? :to_hash
|
137
|
-
value.to_hash
|
138
|
-
elsif value.kind_of? Enumerable
|
139
|
-
value.map { |value| __holotype_hashify value }
|
140
|
-
else
|
141
|
-
value
|
131
|
+
"#{self.class.name}(#{data})"
|
142
132
|
end
|
143
|
-
end
|
144
133
|
|
145
|
-
|
146
|
-
self
|
147
|
-
.class
|
148
|
-
.attributes
|
149
|
-
.flat_map do |name, attribute|
|
150
|
-
# skip non-required attributes
|
151
|
-
next [] unless attribute.required?
|
152
|
-
|
153
|
-
# skip attributes with provided values
|
154
|
-
next [] if attributes.key? name
|
134
|
+
private
|
155
135
|
|
156
|
-
|
136
|
+
def __holotype_hashify value
|
137
|
+
if value.respond_to? :to_hash
|
138
|
+
value.to_hash
|
139
|
+
elsif value.kind_of? Enumerable
|
140
|
+
value.map { |value| __holotype_hashify value }
|
141
|
+
else
|
142
|
+
value
|
157
143
|
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
144
|
+
end
|
145
|
+
|
146
|
+
def __holotype_check_for_missing_required attributes
|
147
|
+
self
|
148
|
+
.class
|
149
|
+
.attributes
|
150
|
+
.flat_map do |name, attribute|
|
151
|
+
# skip non-required attributes
|
152
|
+
next [] unless attribute.required?
|
153
|
+
|
154
|
+
# skip attributes with provided values
|
155
|
+
next [] if attributes.key? name
|
163
156
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
157
|
+
[name]
|
158
|
+
end
|
159
|
+
.tap do |missing_attributes|
|
160
|
+
next if missing_attributes.empty?
|
161
|
+
raise MissingRequiredAttributesError.new self.class, missing_attributes
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def __holotype_store attributes
|
166
|
+
@attributes = Hash[
|
167
|
+
self
|
168
|
+
.class
|
169
|
+
.attributes
|
170
|
+
.map do |name, definition|
|
171
|
+
options = if attributes.key? name
|
172
|
+
Hash value: attributes[name]
|
173
|
+
else
|
174
|
+
Hash[]
|
175
|
+
end
|
176
|
+
|
177
|
+
attribute = Attribute.new self, definition, **options
|
178
|
+
|
179
|
+
[name, attribute]
|
180
|
+
end
|
181
|
+
].freeze
|
182
|
+
end
|
181
183
|
end
|
182
184
|
end
|
data/lib/holotype/attribute.rb
CHANGED
data/lib/holotype/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = '0.
|
1
|
+
module Holotype
|
2
|
+
VERSION = '0.16.0'.freeze
|
3
3
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: holotype
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Lude
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.2.5
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Simple models
|