thunderfoundation 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d0c47a21e2e2a590ccfd8c762a2c86239d3f367b11f336f6e963a1174f50febb
4
+ data.tar.gz: 819a40b3a22f11fd0deaf501f2968765b8615d81cac3caa3c4b15334e7c17037
5
+ SHA512:
6
+ metadata.gz: 25f37ac0b014b8ffd22e1a38410d6b6749ead37cedfa8420ebb2797e03f579e5e8b4c9cc3aedebac4bc65d57ae84e7f63aae25251fee1ae5ae2d0c88b3f354e0
7
+ data.tar.gz: a49315b065f61364e6ea4da61d202ebef507d2ea05beaca3181a33ac8b911c569e2e088aa0439a63a60c5ac379ef2ded8f2a98078d8a8b5b538058c96b10b2a5
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # thunderfoundation
2
+
3
+ General helpers and types shared by `thunderchunk` and `thunderbolt`. Depends on neither.
@@ -0,0 +1,12 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ module ThunderFoundation
5
+ module Types
6
+ class Boolean
7
+ def self.===(value)
8
+ value == true || value == false
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+ require 'date'
5
+ require 'bigdecimal'
6
+
7
+ module ThunderFoundation
8
+ module Types
9
+ Codec = Data.define(:name, :check, :serialize, :deserialize) do
10
+ def valid?(value)
11
+ value.nil? || check.call(value)
12
+ end
13
+
14
+ def dump(value)
15
+ value.nil? ? nil : serialize.call(value)
16
+ end
17
+
18
+ def load(value)
19
+ value.nil? ? nil : deserialize.call(value)
20
+ end
21
+
22
+ def coerce(value)
23
+ return value if valid?(value)
24
+
25
+ coerced = load(value)
26
+ raise ArgumentError, "not a #{name}" unless valid?(coerced)
27
+
28
+ coerced
29
+ rescue TypeError, NoMethodError => e
30
+ raise ArgumentError, e.message
31
+ end
32
+ end
33
+
34
+ REGISTRY = {
35
+ String => Codec.new('String', ->(v) { v.is_a?(String) }, :to_s.to_proc, :to_s.to_proc),
36
+ Integer => Codec.new('Integer', ->(v) { v.is_a?(Integer) }, :itself.to_proc, ->(v) { Integer(v) }),
37
+ Float => Codec.new('Float', ->(v) { v.is_a?(Numeric) }, :to_f.to_proc, ->(v) { Float(v) }),
38
+ Symbol => Codec.new('Symbol', ->(v) { v.is_a?(Symbol) }, :to_s.to_proc, :to_sym.to_proc),
39
+ Boolean => Codec.new('Boolean', ->(v) { Boolean === v }, :itself.to_proc, :itself.to_proc),
40
+ Time => Codec.new('Time', ->(v) { v.is_a?(Time) }, ->(v) { v.utc.iso8601(6) }, ->(v) { Time.iso8601(v) }),
41
+ Date => Codec.new('Date', ->(v) { v.is_a?(Date) && !v.is_a?(DateTime) }, :iso8601.to_proc, ->(v) { Date.iso8601(v) }),
42
+ BigDecimal => Codec.new('BigDecimal', ->(v) { v.is_a?(BigDecimal) }, ->(v) { v.to_s('F') }, ->(v) { BigDecimal(v) }),
43
+ # WARN: AI MADE
44
+ # A Hash with no declared fields (extras, metadata): passed as it comes, keys and values unchecked.
45
+ Hash => Codec.new('Hash', ->(v) { v.is_a?(Hash) }, :itself.to_proc, :itself.to_proc)
46
+ }.freeze
47
+
48
+ def self.fetch(type)
49
+ REGISTRY.fetch(type) do
50
+ raise Error, "unknown type #{type.inspect} (#{REGISTRY.keys.map(&:inspect).join(', ')})"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ThunderFoundation
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'thunderfoundation/version'
4
+
5
+ # General helpers and the framework's types (ThunderFoundation::Types). No dependency on thunderchunk or thunderbolt.
6
+ module ThunderFoundation
7
+ class Error < StandardError; end
8
+ end
9
+
10
+ require_relative 'thunderfoundation/types/boolean'
11
+ require_relative 'thunderfoundation/types'
metadata ADDED
@@ -0,0 +1,41 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thunderfoundation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Albert Alef
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ executables: []
13
+ extensions: []
14
+ extra_rdoc_files: []
15
+ files:
16
+ - README.md
17
+ - lib/thunderfoundation.rb
18
+ - lib/thunderfoundation/types.rb
19
+ - lib/thunderfoundation/types/boolean.rb
20
+ - lib/thunderfoundation/version.rb
21
+ licenses: []
22
+ metadata:
23
+ rubygems_mfa_required: 'true'
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '3.4'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 3.6.9
39
+ specification_version: 4
40
+ summary: General helpers and types shared by thunderchunk and thunderbolt
41
+ test_files: []