acrylic 0.3.0 → 0.4.1

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: ee2e747e395ee52575a3fd089b3c0aa64002e8c14b973c61723506a832836627
4
+ data.tar.gz: 52df65734c90c0e8005fa2d196389e2166773ff5f08e448079568ed438999d84
5
5
  SHA512:
6
- metadata.gz: f38adf01be3856e7fa39112497f57cb2ac7bbdd560a0739fd50d72bca25fa3c84a570dc116db0362c21955eec1f8b7cf2e907698e315e5cd17d1a9acb6c9a329
7
- data.tar.gz: 8f8c37880764223637dc5a31913a29323d912b8dfa0e41a83612ff2d13bc43b4721f8ca7ebcfe4b8846b885802e8d9ab08ef5e146265a968a67588eca0424a67
6
+ metadata.gz: d03527ed2f8565f2f38f5078fe1c975015c7c343b548c9787ce185fdfa9c1f1f5fc7cb94c624fdd8e06de32838e32468d8e1f1a17ad938fd8ef189bc0ee2a130
7
+ data.tar.gz: 5c5fa37519ccf78fbed930950a2e9e29f0bd1a7c6adc5752f96457eaa29a9d85b4cbc5c2c31901be9f4392264fa81e3448047f5d502f2f2eea554e317580526f
data/lib/acrylic.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rack'
2
2
  require_relative 'bits'
3
3
  require_relative 'cascade'
4
- require_relative 'kronos'
4
+ require_relative 'validator'
5
5
 
6
6
  require 'yaml'
7
7
 
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
 
@@ -6,6 +6,7 @@
6
6
  # use a similar route definition system to regular Cascade routes to create error pages.
7
7
  require 'markaby'
8
8
  require 'rack/utils'
9
+ require_relative '../version'
9
10
 
10
11
  Rack::Utils::HTTP_STATUS_CODES[418] = "I'm a Teapot"
11
12
 
@@ -28,7 +29,7 @@ module Acrylic
28
29
  class Cascade
29
30
  def self.default_error_body code, req
30
31
  mab = Markaby::Builder.new
31
-
32
+
32
33
  mab.html do
33
34
  style <<-CSS
34
35
  body,html { font-family:"arial"; background-color:#fed; height:100%; display:flex; justify-content:center; align-items:center }
data/lib/cascade.rb CHANGED
@@ -118,4 +118,4 @@ def route *wargs, **kwargs, &route
118
118
  Acrylic::ROUTES << (Acrylic::Route.new args, route)
119
119
  end
120
120
 
121
- require 'cascade/errors'
121
+ require_relative 'cascade/errors'
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,8 +1,8 @@
1
1
  module Acrylic
2
2
  class Version
3
3
  MAJOR = 0
4
- MINOR = 3
5
- HOTFIX = 0
4
+ MINOR = 4
5
+ HOTFIX = 1
6
6
 
7
7
  def self.semver
8
8
  "#{MAJOR}.#{MINOR}.#{HOTFIX}"
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.1
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,10 @@ 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
- - lib/kronos.rb
68
+ - lib/validator.rb
70
69
  - lib/version.rb
71
70
  homepage: https://codeberg.org/fmixolydian/acrylic
72
71
  licenses:
data/lib/anubis.rb DELETED
File without changes
data/lib/kronos.rb DELETED
@@ -1,10 +0,0 @@
1
-
2
- module Acrylic
3
- module Kronos
4
- class Record
5
- class << self
6
-
7
- end
8
- end
9
- end
10
- end