jet-core 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 60bd1e16ecc78dec3ea5ccce6397a052eb68ebd21c7996fb863b11e75fb6b81c
4
+ data.tar.gz: da10b1f9a5a7634780c7626a0c8cd023a8b7ba08fe57ff18a7ab5206b050884d
5
+ SHA512:
6
+ metadata.gz: 8affba60f3847abfca07ebc27aef52ece3727a33687f602a973f81e93842760a02101baa9a9fe9f0f8c4434efa382f53e55109f8851fd3b4dfbcd35d46dbc711
7
+ data.tar.gz: f4125430abc33b869e00d5c5fc79f326135e5f9b7cbf5be429854865b6cd8665c774bf5684d140fc549a109eae82cfa0272fa9e0a4341a2b609668108fcac134
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2019 Joshua Hansen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ # Jet: A toolkit aimed at web apps.
2
+
3
+ TODO: Write.
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jet/core"
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jet/core/version"
4
+
5
+ module Jet
6
+ class << self
7
+ def all_are?(obj, *types)
8
+ obj.all? { |v| is_a?(v, *types) }
9
+ end
10
+
11
+ def context(obj, additional_context = nil)
12
+ if obj.respond_to?(:context)
13
+ obj.context
14
+ elsif obj.is_a?(Hash)
15
+ obj
16
+ else
17
+ {}
18
+ end.yield_self { |ctx| additional_context ? ctx.merge(additional_context) : ctx }
19
+ end
20
+
21
+ def failure?(obj)
22
+ obj.respond_to?(:failure?) ? obj.failure? : !obj
23
+ end
24
+
25
+ def is_a?(obj, *types)
26
+ types.any? { |t| obj.is_a?(t) }
27
+ end
28
+
29
+ def success?(obj)
30
+ obj.respond_to?(:success?) ? obj.success? : obj
31
+ end
32
+
33
+ def type_check!(desc, obj, *types)
34
+ raise TypeError, "#{desc} must be #{type_error_desc(*types)}" unless
35
+ is_a?(obj, *types)
36
+ obj
37
+ end
38
+
39
+ def type_check_each!(desc, obj, *types)
40
+ type_check!(desc, obj, Array)
41
+
42
+ raise TypeError, "elements of #{desc} must be #{type_error_desc(*types)}" unless
43
+ all_are?(obj, *types)
44
+
45
+ obj
46
+ end
47
+
48
+ def type_check_hash!(desc, obj, *types, key_type: nil)
49
+ type_check!(desc, obj, Hash)
50
+
51
+ raise TypeError, "#{desc} keys must all be #{type_error_desc(*key_type)}" unless
52
+ !key_type || all_are?(obj.keys, *key_type)
53
+
54
+ raise TypeError, "#{desc} values must all be #{type_error_desc(*types)}" unless
55
+ all_are?(obj.values, *types)
56
+
57
+ obj
58
+ end
59
+
60
+ def type_error_desc(*types)
61
+ return types.join(" or ") if types.size < 3
62
+ "#{types[0..-2].join(', ')} or #{types.last}"
63
+ end
64
+
65
+ def output(obj)
66
+ obj.respond_to?(:output) ? obj.output : obj
67
+ end
68
+ end
69
+
70
+ module Core
71
+ class << self
72
+ def block_and_callables!(*callables, &blk)
73
+ raise ArgumentError, "`callables` must respond to `call`" unless
74
+ callables.all? { |obj| obj.respond_to?(:call) }
75
+
76
+ ([blk] + callables).compact
77
+ end
78
+
79
+ def block_or_callable!(callable, &blk)
80
+ raise ArgumentError, "both block and callable given" if blk && callable
81
+ raise ArgumentError, "`callable` must respond to `call`" if
82
+ callable && !callable.respond_to?(:call)
83
+
84
+ blk || callable
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ require "jet/core/instance_registry"
91
+ require "jet/result"
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jet
4
+ module Core
5
+ module InstanceRegistry
6
+ def self.extended(base)
7
+ super
8
+ base.instance_variable_set(:@registry, {})
9
+ end
10
+
11
+ def [](obj)
12
+ @registry[obj]
13
+ end
14
+
15
+ def []=(key, obj)
16
+ raise "no `type` for registry has been set yet" unless @type
17
+ @registry[Jet.type_check!("`key`", key, Symbol)] = Jet.type_check!("`obj`", obj, @type)
18
+ end
19
+
20
+ def fetch(*args)
21
+ @registry.fetch(*args)
22
+ end
23
+
24
+ def freeze
25
+ @registry.freeze
26
+ super
27
+ end
28
+
29
+ def register(hash)
30
+ hash.each { |key, obj| self[key] = obj }
31
+ self
32
+ end
33
+
34
+ def to_h
35
+ @registry.dup
36
+ end
37
+
38
+ def type(type = nil)
39
+ return @type unless type
40
+ raise "`type` cannot be changed once set" if @type
41
+ Jet.type_check!("`type`", type, Class, Module)
42
+ @type = type
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jet
4
+ module Core
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ TINY = 0
8
+ VERSION = [MAJOR, MINOR, TINY].join(".").freeze
9
+
10
+ def self.version
11
+ VERSION
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jet
4
+ class Result
5
+ class << self
6
+ def failure(output = nil, context = {})
7
+ new(false, output, context)
8
+ end
9
+
10
+ def success(output = nil, context = {})
11
+ new(true, output, context)
12
+ end
13
+ end
14
+
15
+ attr_reader :context, :output
16
+
17
+ def initialize(success, output = nil, context = {})
18
+ @context = _context(context)
19
+ @success = success ? true : false
20
+ @output = output
21
+ end
22
+
23
+ def !=(other)
24
+ !(self == other)
25
+ end
26
+
27
+ def ==(other)
28
+ return output == other.output if other.is_a?(self.class)
29
+ output == other
30
+ end
31
+
32
+ def [](key)
33
+ @context[key]
34
+ end
35
+
36
+ def at
37
+ context.fetch(:at, [])
38
+ end
39
+
40
+ def errors
41
+ context.fetch(:errors, [])
42
+ end
43
+
44
+ def errors_at(*at)
45
+ at.flatten.yield_self { |a| errors.select { |e| e.at[0...a.size] == a } }
46
+ end
47
+
48
+ def failure?
49
+ !@success
50
+ end
51
+
52
+ def success?
53
+ @success
54
+ end
55
+
56
+ def with(**context)
57
+ self.class.new(@success, @output, @context.merge(context))
58
+ end
59
+
60
+ private
61
+
62
+ def _errors(errors)
63
+ case errors
64
+ when Hash
65
+ errors.map { |at, error| error.with(at: error.at + Array(at)) }
66
+ else
67
+ Array(errors)
68
+ end
69
+ end
70
+
71
+ def _context(context)
72
+ context.dup.tap do |ctx|
73
+ ctx[:at] &&= Array(ctx[:at])
74
+ ctx[:errors] &&= _errors(ctx[:errors])
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jet-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua
8
+ - Hansen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-11-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '5.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '5.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.56'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.56'
70
+ description: Core classes for the Jet toolkit.
71
+ email:
72
+ - joshua@epicbanality.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - LICENSE.txt
78
+ - README.md
79
+ - lib/jet-core.rb
80
+ - lib/jet/core.rb
81
+ - lib/jet/core/instance_registry.rb
82
+ - lib/jet/core/version.rb
83
+ - lib/jet/result.rb
84
+ homepage: https://github.com/binarypaladin/jet-core
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.5.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.7.6.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Core classes for the Jet toolkit.
108
+ test_files: []