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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8de676ea732eeaafa954422a48c138e2e0fb6a8
4
- data.tar.gz: d52556331331d5b80e9d79635895f1364e4115a4
3
+ metadata.gz: 5d54a2791610a762b82681d5430e8929972bff81
4
+ data.tar.gz: ef327df4073a1606b166594a25a286d5b3d47bc7
5
5
  SHA512:
6
- metadata.gz: d615c9697c7d70431b710fe7882f03867f2949503ecb85fc6465b2e0051ce85b91b886ea3de9a30cc4f3382ca5e9302e97094ecb319f768a18da01d6687364fe
7
- data.tar.gz: 17e20c875c8fd863a3536753228a258aed88b0556120a1af750c5b8b2066507fa182f8cb6a6830fbc174dac7f52e62a81b90ee74d1a64a1d9a997f2283a40807
6
+ metadata.gz: 286f0bce02c86eb051673ec89c9f3d2baf8558edf80a91236a6815b09c6fcf339b88282f5650ab6ebf31a745d390b981b0ea200aca065388543cbfe563a30035
7
+ data.tar.gz: 54045ed8c97431213da07a8fccf7e23daee257607b592ba136416a363caa15bd37383d3a88294cce73ad703f1e2c6a0dbc663bb3f2b378276447d8972b3a55e2
@@ -12,171 +12,173 @@
12
12
  version
13
13
  ].each { |name| require_relative "holotype/#{name}.rb" }
14
14
 
15
- class Holotype
16
- # Singleton Definition
17
-
15
+ module Holotype
18
16
  class << self
19
- def attribute name, **options, &default
20
- # symbolize name
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
- def attributes
57
- @attributes ||= Hash[]
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
- def make_immutable
61
- raise AttributesAlreadyDefinedError.new if attributes.count != 0
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
- def immutable?
71
- !!@immutable
72
- end
59
+ def attributes
60
+ @attributes ||= Hash[]
61
+ end
73
62
 
74
- def default_attribute_options **options
75
- @default_attribute_options = options.freeze
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
- private
70
+ @immutable = true
71
+ end
79
72
 
80
- IMMUTABLE_OPTION = Hash[immutable: true].freeze
73
+ def immutable?
74
+ !!@immutable
75
+ end
81
76
 
82
- def __default_attribute_options
83
- @default_attribute_options || Hash[]
84
- end
77
+ def default_attribute_options **options
78
+ @default_attribute_options = options.freeze
85
79
  end
86
80
 
87
- # Instance Definition
81
+ private
88
82
 
89
- attr_reader :attributes
83
+ IMMUTABLE_OPTION = Hash[immutable: true].freeze
90
84
 
91
- def initialize **attributes
92
- __holotype_check_for_missing_required attributes
93
- __holotype_store attributes
85
+ def __default_attribute_options
86
+ @default_attribute_options || Hash[]
94
87
  end
95
88
 
96
- def frozen?
97
- true
98
- end
89
+ module InstanceMethods
90
+ attr_reader :attributes
99
91
 
100
- def to_hash
101
- Hash[
102
- attributes
103
- .map do |key, attribute|
104
- definition = attribute.definition
92
+ def initialize **attributes
93
+ __holotype_check_for_missing_required attributes
94
+ __holotype_store attributes
95
+ end
105
96
 
106
- value = __holotype_hashify attribute.value
97
+ def frozen?
98
+ true
99
+ end
107
100
 
108
- [key, value]
109
- end
110
- ]
111
- end
101
+ def to_hash
102
+ Hash[
103
+ attributes
104
+ .map do |key, attribute|
105
+ definition = attribute.definition
112
106
 
113
- def == other
114
- return false unless self.class == other.class
107
+ value = __holotype_hashify attribute.value
115
108
 
116
- attributes.all? do |name, attribute|
117
- attribute.value == other.attributes[name].value
109
+ [key, value]
110
+ end
111
+ ]
118
112
  end
119
- end
120
113
 
121
- def with **attributes
122
- self.class.new to_hash.merge attributes
123
- end
114
+ def == other
115
+ return false unless self.class == other.class
124
116
 
125
- def inspect
126
- data = to_hash
127
- .map { |attribute, value| "#{attribute}: #{value.inspect}" }
128
- .join(', ')
117
+ attributes.all? do |name, attribute|
118
+ attribute.value == other.attributes[name].value
119
+ end
120
+ end
129
121
 
130
- "#{self.class.name}(#{data})"
131
- end
122
+ def with **attributes
123
+ self.class.new to_hash.merge attributes
124
+ end
132
125
 
133
- private
126
+ def inspect
127
+ data = to_hash
128
+ .map { |attribute, value| "#{attribute}: #{value.inspect}" }
129
+ .join(', ')
134
130
 
135
- def __holotype_hashify value
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
- def __holotype_check_for_missing_required attributes
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
- [name]
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
- .tap do |missing_attributes|
159
- next if missing_attributes.empty?
160
- raise MissingRequiredAttributesError.new self.class, missing_attributes
161
- end
162
- end
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
- def __holotype_store attributes
165
- @attributes = Hash[
166
- self
167
- .class
168
- .attributes
169
- .map do |name, definition|
170
- options = if attributes.key? name
171
- Hash value: attributes[name]
172
- else
173
- Hash[]
174
- end
175
-
176
- attribute = Attribute.new self, definition, **options
177
-
178
- [name, attribute]
179
- end
180
- ].freeze
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
@@ -5,7 +5,7 @@
5
5
  read_only_error
6
6
  ].each { |file| require_relative "attribute/#{file}" }
7
7
 
8
- class Holotype
8
+ module Holotype
9
9
  class Attribute
10
10
  attr_reader :definition, :owner
11
11
 
@@ -7,7 +7,7 @@
7
7
 
8
8
  require_relative 'definition/default_conflict_error.rb'
9
9
 
10
- class Holotype
10
+ module Holotype
11
11
  class Attribute
12
12
  class Definition
13
13
  attr_reader :name
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class Definition
4
4
  class DefaultConflictError < StandardError
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class Definition
4
4
  class NoCollectionClassError < StandardError
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class Definition
4
4
  class NoValueClassError < StandardError
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class Definition
4
4
  class RequiredConflictError < StandardError
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class FrozenModificationError < StandardError
4
4
  attr_reader :attribute_name
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class ImmutableValueError < StandardError
4
4
  attr_reader :name
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class Attribute
3
3
  class ReadOnlyError < StandardError
4
4
  attr_reader :name
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class AttributesAlreadyDefinedError < StandardError
3
3
  def message; MESSAGE; end
4
4
 
@@ -3,7 +3,7 @@
3
3
  expected_hash_like_collection_error
4
4
  ].each { |file| require_relative "collection_normalizer/#{file}" }
5
5
 
6
- class Holotype
6
+ module Holotype
7
7
  class CollectionNormalizer
8
8
  extend Memorandum
9
9
 
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class CollectionNormalizer
3
3
  class ExpectedArrayLikeCollectionError < StandardError
4
4
  attr_reader :attribute
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class CollectionNormalizer
3
3
  class ExpectedHashLikeCollectionError < StandardError
4
4
  attr_reader :attribute
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class InheritanceDisallowedError < StandardError
3
3
  def message; MESSAGE; end
4
4
 
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class MissingRequiredAttributesError < StandardError
3
3
  attr_reader :attributes, :original_class
4
4
 
@@ -1,4 +1,4 @@
1
- class Holotype
1
+ module Holotype
2
2
  class ValueNormalizer
3
3
  attr_reader :definition
4
4
 
@@ -1,3 +1,3 @@
1
- class Holotype
2
- VERSION = '0.15.1'.freeze
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.15.1
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.6.11
113
+ rubygems_version: 2.2.5
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Simple models