structish 0.1.0 → 0.2.4
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/.github/workflows/{structable-rspec.yml → structish-rspec.yml} +1 -1
- data/.gitignore +1 -0
- data/lib/structish.rb +2 -0
- data/lib/structish/config.rb +21 -0
- data/lib/structish/validation_error.rb +3 -0
- data/lib/structish/validations.rb +25 -2
- data/lib/structish/version.rb +1 -1
- data/lib/structish_object_extensions.rb +0 -8
- data/structish.gemspec +2 -2
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe83835231060abe23b9427547d10a67fb5f6c5575f1f0ba3650346902dd5385
|
4
|
+
data.tar.gz: 9fdb570ed7d576fea0b453b1f3c81c08285c75493816e8dc689e8949b4b6ec4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2877855214b7f57d302f3c8c3fd6f4594b49e35077c00a48108c948f6ba2b0be0ad40e332a6766978bf511c6a3bb71c58969e762506ed7dd31f7c015de9c5007
|
7
|
+
data.tar.gz: 4076538cb93f7fe0aeaf39a94f84da386023a40060a936bd13cdbf75848580e94ecc6925f2d1f66430658d50556e7b4e3888de08d43b57097fd45c9ecc5e715d
|
data/.gitignore
CHANGED
data/lib/structish.rb
CHANGED
@@ -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
|
@@ -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,30 @@ 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 attribute_keys
|
187
|
+
attributes.map { |attribute| attribute[:key] }
|
188
|
+
end
|
189
|
+
|
167
190
|
def structish?
|
168
191
|
true
|
169
192
|
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)
|
data/structish.gemspec
CHANGED
@@ -23,8 +23,8 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
|
-
spec.add_runtime_dependency "activesupport"
|
26
|
+
spec.add_runtime_dependency "activesupport"
|
27
27
|
|
28
28
|
spec.add_development_dependency "bundler"
|
29
|
-
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rake"
|
30
30
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
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-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
description: |-
|
56
56
|
Adds validations, function creation, function delegation,
|
57
57
|
and key restrictions to arrays and hashes so that they may
|
@@ -62,7 +62,7 @@ executables: []
|
|
62
62
|
extensions: []
|
63
63
|
extra_rdoc_files: []
|
64
64
|
files:
|
65
|
-
- ".github/workflows/
|
65
|
+
- ".github/workflows/structish-rspec.yml"
|
66
66
|
- ".gitignore"
|
67
67
|
- Gemfile
|
68
68
|
- README.md
|
@@ -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
|