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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8edee95c617e99bb439af1073b4b79697d301c487106320c2b075b2246a5bab
4
- data.tar.gz: eb452debefdf9b4d6348bb11e6b5c812bdffbe97272d33651be399a4b7d12e4b
3
+ metadata.gz: fe83835231060abe23b9427547d10a67fb5f6c5575f1f0ba3650346902dd5385
4
+ data.tar.gz: 9fdb570ed7d576fea0b453b1f3c81c08285c75493816e8dc689e8949b4b6ec4c
5
5
  SHA512:
6
- metadata.gz: f3ebf97ad136e4f175a5a4fbfb0bb06a8a31d3b63e01df5db44edbee439f8f43eacf4805fd262cb05cc20b51d884d5b6864b4e8b794f3d9c9a3c017b2c21c21d
7
- data.tar.gz: 633f3b2c776edc67e49bc8084683e4eaf5e628c1e434631f1e2950636d941b1849c3987dd3e2a5854ff492a98b970c5e932a2b9e3e53447748515e1931a06221
6
+ metadata.gz: 2877855214b7f57d302f3c8c3fd6f4594b49e35077c00a48108c948f6ba2b0be0ad40e332a6766978bf511c6a3bb71c58969e762506ed7dd31f7c015de9c5007
7
+ data.tar.gz: 4076538cb93f7fe0aeaf39a94f84da386023a40060a936bd13cdbf75848580e94ecc6925f2d1f66430658d50556e7b4e3888de08d43b57097fd45c9ecc5e715d
@@ -1,5 +1,5 @@
1
1
  env:
2
- RUBY_VERSION: '2.6.6'
2
+ RUBY_VERSION: '2.7.2'
3
3
 
4
4
  name: Tests
5
5
  on: [push, pull_request]
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /tmp/
9
9
  /out/
10
10
  /Gemfile.lock
11
+ *.gem
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"
@@ -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
@@ -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,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
@@ -1,3 +1,3 @@
1
1
  module Structish
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.4"
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)
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", "~> 5.0"
26
+ spec.add_runtime_dependency "activesupport"
27
27
 
28
28
  spec.add_development_dependency "bundler"
29
- spec.add_development_dependency "rake", "~> 10.0"
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.1.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-01-12 00:00:00.000000000 Z
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: '5.0'
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: '5.0'
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: '10.0'
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: '10.0'
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/structable-rspec.yml"
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