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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bc52b972b081a69479e5406a733c7374a725b275e8db0dad081d30eabd0fba5
4
- data.tar.gz: f244b3bfb2e1d4b59ab985240e61e64f8f1730091de57e9e0f219c2d8e1ee236
3
+ metadata.gz: b6d2ba1a7e8fe1d6239b90627d5be7a5ffabae2ec8ed55f075d6794d5bcc387c
4
+ data.tar.gz: 11bcd0b789e9cf61dea899a3d2eb397e36eee39474054da8580ea4fe1c19a261
5
5
  SHA512:
6
- metadata.gz: eff36ee49e66cc34660a29d4ffe82e1561c29de8adb7b98ab896fcc15c99c236758463289725336c63236ba1abba5a155ec988ed7511e42235e99c86bef5ba74
7
- data.tar.gz: 30086ab0aa74750fbf08079b8e8e5f0ea1611036301577474b5aaf656bbf8512a3633e893e9e694032843617cfc0f6b816c4b9874051e668b4d5361b28d1ec31
6
+ metadata.gz: c8445eb56faec25e23b09ab4ad2040b3906f313b3e948f27554ba209a200f1e15de6e4fe7a6d32ca5ba2aed0adbaa7a395263ea4a50c964125e4d85b6273d0bb
7
+ data.tar.gz: 16abf2097b0755ad04fd0783cf9a420cd384cfe82ec7336c182952cc5a272c047e3546923a6ceac2878f470c9ccbf2e4a96d001e9c304f514e467730935e5d82
data/lib/structish.rb CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
2
  require 'active_support/core_ext/hash'
3
3
 
4
+ require 'structish/config'
5
+
4
6
  require "structish/version"
5
7
  require "structish_object_extensions"
6
8
  require "structish/validation_error"
@@ -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
- super(constructor)
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
@@ -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
@@ -3,6 +3,9 @@ module Structish
3
3
  def initialize(message, klass)
4
4
  super("#{message} in class #{klass.to_s}")
5
5
  set_backtrace(caller)
6
+ if Config.show_full_trace?
7
+ puts self.backtrace
8
+ end
6
9
  end
7
10
  end
8
11
  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.keys - allowed_keys).empty?
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.keys
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
@@ -1,3 +1,3 @@
1
1
  module Structish
2
- VERSION = "0.1.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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.1.1
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-01-29 00:00:00.000000000 Z
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