acrylic 0.3.0 → 0.4.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: 87316921f14a552574ac7299c3fcd42f4d565b269ab2fc4dd773f02fb2eafae1
4
- data.tar.gz: b1292abed88054f9ff4193da0356c7916decfb5a6546cc666fc271eb5e6f1947
3
+ metadata.gz: 3c867a949c6d7b81cf0e358387cf9ee06bb2cb460901eb089492f589e5c1e31f
4
+ data.tar.gz: c66069736873cab0183f99cb2832c8339eabb4540af63b8b99208678eaa81cc9
5
5
  SHA512:
6
- metadata.gz: f38adf01be3856e7fa39112497f57cb2ac7bbdd560a0739fd50d72bca25fa3c84a570dc116db0362c21955eec1f8b7cf2e907698e315e5cd17d1a9acb6c9a329
7
- data.tar.gz: 8f8c37880764223637dc5a31913a29323d912b8dfa0e41a83612ff2d13bc43b4721f8ca7ebcfe4b8846b885802e8d9ab08ef5e146265a968a67588eca0424a67
6
+ metadata.gz: '08c812a7ab32c4672692ad5989fc04a32467e0bab0ec6a4174b3d54f0bfd85d011e0ffe822f52ea889366156469d62e4bd9d30147ef680082f3ae2bbd5a40734'
7
+ data.tar.gz: f4f0f3326cba037385f1618733ae41f40ed7bd1e80bea879c9a368d940faeabcf7a245fed7ff5bc4a350570d5982828dfd4b14c048e618f0427ea3f91bec67d6
data/lib/bits.rb CHANGED
@@ -31,7 +31,12 @@ class Hash
31
31
 
32
32
  # recursively converts hash to ostruct.
33
33
  def structize
34
- OpenStruct.new map do |k, v| end
34
+ OpenStruct.new (map do |k, v| [k, if v.is_a? Hash then v.structize else v end] end.to_h)
35
+ end
36
+
37
+ # recursively convert keys to symbols
38
+ def with_sym_keys
39
+ map do |k, v| [k.to_sym, if v.is_a? Hash then v.with_sym_keys else v end] end.to_h
35
40
  end
36
41
  end
37
42
 
data/lib/validator.rb ADDED
@@ -0,0 +1,76 @@
1
+ module Acrylic
2
+
3
+ # a schema is a hash that validates another hash.
4
+ # values in the schema represent conditions to validate the object against.
5
+ # e.g.
6
+ # {
7
+ # email: [:email, [:length, (..32)]]
8
+ # username: [:not_null, [:length, (..32)]]
9
+ # password: [:not_null, [:length, (8..32)]]
10
+ # }
11
+ #
12
+ # string keys in the object are automatically converted to symbols.
13
+
14
+ module Validators
15
+ def email val
16
+ # shamelessly stolen from https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
17
+ email_address = begin
18
+ letter = 'a-zA-Z'
19
+ digit = '0-9'
20
+ atext = "[#{letter}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
21
+ dot_atom_text = "#{atext}+([.]#{atext}*)+"
22
+ dot_atom = dot_atom_text
23
+ no_ws_ctl = '\x01-\x08\x11\x12\x14-\x1f\x7f'
24
+ qtext = "[^#{no_ws_ctl}\\x0d\\x22\\x5c]" # Non-whitespace, non-control character except for \ and "
25
+ text = '[\x01-\x09\x11\x12\x14-\x7f]'
26
+ quoted_pair = "(\\x5c#{text})"
27
+ qcontent = "(?:#{qtext}|#{quoted_pair})"
28
+ quoted_string = "[\"]#{qcontent}+[\"]"
29
+ atom = "#{atext}+"
30
+ word = "(?:#{atom}|#{quoted_string})"
31
+ obs_local_part = "#{word}([.]#{word})*"
32
+ local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
33
+ dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
34
+ dcontent = "(?:#{dtext}|#{quoted_pair})"
35
+ domain_literal = "\\[#{dcontent}+\\]"
36
+ obs_domain = "#{atom}([.]#{atom})+"
37
+ domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
38
+ addr_spec = "#{local_part}\@#{domain}"
39
+ pattern = /\A#{addr_spec}\z/u
40
+ end
41
+
42
+ !!email_address.match(val)
43
+ end
44
+
45
+ def not_null val; not val.nil? end
46
+ def length val, range; range.include? val.length end
47
+ def range val, range; range.include? val end
48
+ def regex val, expr; expr.match? val end
49
+ end
50
+
51
+ def valid? object, schema
52
+ object = object.with_sym_keys
53
+ schema.each do |key, conditions|
54
+ value = object[key]
55
+
56
+ # apply conditions
57
+ if conditions.is_a? Hash
58
+ return false unless value.is_a? Hash
59
+ return false unless valid? value, conditions
60
+ else
61
+ conditions = conditions.containerize
62
+
63
+ # check every condition (in order)
64
+ conditions.each do |condition|
65
+ condition = condition.containerize
66
+ opcode, *args = condition
67
+ if opcode == :default then value = args[0] if value.nil?
68
+ else return false unless Validators.instance_method(opcode).bind(nil).call value, *args
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ return true
75
+ end
76
+ end
data/lib/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Acrylic
2
2
  class Version
3
3
  MAJOR = 0
4
- MINOR = 3
4
+ MINOR = 4
5
5
  HOTFIX = 0
6
6
 
7
7
  def self.semver
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acrylic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fmixolydian
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-01-03 00:00:00.000000000 Z
10
+ date: 2026-01-04 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rack
@@ -62,11 +62,11 @@ files:
62
62
  - README.md
63
63
  - VERSION.md
64
64
  - lib/acrylic.rb
65
- - lib/anubis.rb
66
65
  - lib/bits.rb
67
66
  - lib/cascade.rb
68
67
  - lib/cascade/errors.rb
69
68
  - lib/kronos.rb
69
+ - lib/validator.rb
70
70
  - lib/version.rb
71
71
  homepage: https://codeberg.org/fmixolydian/acrylic
72
72
  licenses:
data/lib/anubis.rb DELETED
File without changes