structish 0.1.1 → 0.3.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/structish.rb +2 -0
- data/lib/structish/array.rb +5 -1
- data/lib/structish/config.rb +21 -0
- data/lib/structish/hash.rb +1 -0
- data/lib/structish/validation_error.rb +3 -0
- data/lib/structish/validations.rb +33 -2
- data/lib/structish/version.rb +1 -1
- data/lib/structish_object_extensions.rb +0 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6d2ba1a7e8fe1d6239b90627d5be7a5ffabae2ec8ed55f075d6794d5bcc387c
|
4
|
+
data.tar.gz: 11bcd0b789e9cf61dea899a3d2eb397e36eee39474054da8580ea4fe1c19a261
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8445eb56faec25e23b09ab4ad2040b3906f313b3e948f27554ba209a200f1e15de6e4fe7a6d32ca5ba2aed0adbaa7a395263ea4a50c964125e4d85b6273d0bb
|
7
|
+
data.tar.gz: 16abf2097b0755ad04fd0783cf9a420cd384cfe82ec7336c182952cc5a272c047e3546923a6ceac2878f470c9ccbf2e4a96d001e9c304f514e467730935e5d82
|
data/lib/structish.rb
CHANGED
data/lib/structish/array.rb
CHANGED
@@ -6,7 +6,11 @@ module Structish
|
|
6
6
|
def initialize(constructor)
|
7
7
|
raise(ArgumentError, "Only array-like objects can be used as constructors for Structish::Array") unless constructor.class <= ::Array
|
8
8
|
validate_structish(constructor)
|
9
|
-
|
9
|
+
if self.class.compact?
|
10
|
+
super(constructor.compact)
|
11
|
+
else
|
12
|
+
super(constructor)
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def <<(entry)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Structish
|
2
|
+
class Config
|
3
|
+
|
4
|
+
def self.config
|
5
|
+
@config ||= {}.with_indifferent_access
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.config=(config_hash)
|
9
|
+
@config = config_hash.with_indifferent_access
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.show_full_trace=(show_full)
|
13
|
+
config["show_full_trace"] = show_full
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.show_full_trace?
|
17
|
+
config["show_full_trace"]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/structish/hash.rb
CHANGED
@@ -9,6 +9,7 @@ module Structish
|
|
9
9
|
constructor = self.class.symbolize? ? raw_constructor.symbolize_keys : raw_constructor
|
10
10
|
hash = constructor.to_h
|
11
11
|
validate_structish(hash)
|
12
|
+
hash = hash.compact if self.class.compact?
|
12
13
|
super()
|
13
14
|
update(hash)
|
14
15
|
self.default = hash.default if hash.default
|
@@ -26,7 +26,7 @@ module Structish
|
|
26
26
|
def validate_key_restriction(constructor)
|
27
27
|
if self.class.restrict?
|
28
28
|
allowed_keys = validations.map { |attribute| attribute[:key] }
|
29
|
-
valid = (constructor
|
29
|
+
valid = (keys_for(constructor) - allowed_keys).empty?
|
30
30
|
raise(Structish::ValidationError.new("Keys are restricted to #{allowed_keys.join(", ")}", self.class)) unless valid
|
31
31
|
end
|
32
32
|
end
|
@@ -48,6 +48,9 @@ module Structish
|
|
48
48
|
key = attribute[:key]
|
49
49
|
if attribute[:cast] && constructor[key]
|
50
50
|
if attribute[:klass] == ::Array && attribute[:of]
|
51
|
+
unless constructor[key].class <= ::Array
|
52
|
+
raise(Structish::ValidationError.new("Class mismatch for #{attribute[:key]} -> #{constructor[key].class}. Should be a Array", self.class))
|
53
|
+
end
|
51
54
|
constructor[key] = constructor[key].map { |v| cast_single(v, attribute[:of]) }
|
52
55
|
else
|
53
56
|
constructor[key] = cast_single(constructor[key], attribute[:klass])
|
@@ -93,7 +96,7 @@ module Structish
|
|
93
96
|
|
94
97
|
def global_attributes_for(constructor)
|
95
98
|
global_attributes_hash[constructor] = begin
|
96
|
-
constructor_keys = constructor
|
99
|
+
constructor_keys = keys_for(constructor)
|
97
100
|
global_validations.each_with_object([]) do |validation, arr|
|
98
101
|
constructor_keys.each { |key| arr << validation.merge(key: key) }
|
99
102
|
end
|
@@ -160,10 +163,38 @@ module Structish
|
|
160
163
|
end || []
|
161
164
|
end
|
162
165
|
|
166
|
+
def attribute_values
|
167
|
+
if self.class < Array
|
168
|
+
self.to_a.values_at(*self.class.attribute_keys)
|
169
|
+
elsif self.class < Hash
|
170
|
+
self.to_h.slice(*self.class.attribute_keys)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def keys_for(constructor)
|
175
|
+
if constructor.class <= ::Array
|
176
|
+
[*0..constructor.size-1]
|
177
|
+
elsif constructor.class <= ::Hash
|
178
|
+
constructor.keys
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
163
182
|
end
|
164
183
|
|
165
184
|
module ClassMethods
|
166
185
|
|
186
|
+
def compact(do_compact)
|
187
|
+
@compact = do_compact
|
188
|
+
end
|
189
|
+
|
190
|
+
def compact?
|
191
|
+
@compact
|
192
|
+
end
|
193
|
+
|
194
|
+
def attribute_keys
|
195
|
+
attributes.map { |attribute| attribute[:key] }
|
196
|
+
end
|
197
|
+
|
167
198
|
def structish?
|
168
199
|
true
|
169
200
|
end
|
data/lib/structish/version.rb
CHANGED
@@ -6,14 +6,6 @@ class ::Hash
|
|
6
6
|
end
|
7
7
|
|
8
8
|
class ::Array
|
9
|
-
def values
|
10
|
-
self.to_a
|
11
|
-
end
|
12
|
-
|
13
|
-
def keys
|
14
|
-
[*0..self.size-1]
|
15
|
-
end
|
16
|
-
|
17
9
|
def to_structish(structish_klass)
|
18
10
|
raise(ArgumentError, "Class is not a child of Structish::Array") unless structish_klass < Structish::Array
|
19
11
|
structish_klass.new(self)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dylan Blakemore
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- lib/structish.rb
|
73
73
|
- lib/structish/array.rb
|
74
|
+
- lib/structish/config.rb
|
74
75
|
- lib/structish/hash.rb
|
75
76
|
- lib/structish/validation.rb
|
76
77
|
- lib/structish/validation_error.rb
|