dry-interface 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35ad221861d5f292e3b7161879eee7b19ce7153dbda2db6f99623881978e1779
4
+ data.tar.gz: 5d7e84b70f23f0b898292ebc9fcecb825f3c602644e393e58cb3a4f12e618c27
5
+ SHA512:
6
+ metadata.gz: ca69ef2474fc1c3aaa39f0e52183dc938dcfc3f2f7b4154a58e5e25972c807d32cd4f1b7ef69708ea7a5f6b40e7b73a9b3badbc026de9912d73e9606edc5e2fb
7
+ data.tar.gz: 205d5ad822c71d32561ed6875a0aeee5045278af6e8cc6cda33fe063a4712e0f3df6e4146f5c5e78d58a0cbae52317cb6240e10afdf837a2d33576ae70494896
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class Interface
5
+ module Interfaces
6
+ module Abstract
7
+ extend ActiveSupport::Concern
8
+ include Common
9
+
10
+ class_methods do
11
+ def type
12
+ direct_descendants.map(&:type).reduce(&:|) or raise "No types defined for [#{self}]"
13
+ end
14
+
15
+ def named
16
+ format "%<name>s<[%<names>s]>", { name: name, names: direct_descendants.map(&:named).join(" | ") }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class Interface
5
+ module Interfaces
6
+ module Common
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def name
11
+ demodulize(super)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class Interface
5
+ module Interfaces
6
+ module Concrete
7
+ extend ActiveSupport::Concern
8
+ include Common
9
+
10
+ included do
11
+ class << self
12
+ alias_method :new, :_new
13
+ end
14
+ end
15
+
16
+ class_methods do
17
+ def type
18
+ direct_descendants.first or self
19
+ end
20
+
21
+ def named
22
+ direct_descendants.first&.name or name
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class Interface
5
+ module Interfaces
6
+ module Value
7
+ extend ActiveSupport::Concern
8
+ include Concrete
9
+
10
+ included do |child|
11
+ otherwise do |value, type, &error|
12
+ names = child.type.attribute_names
13
+
14
+ unless names.one?
15
+ raise ArgumentError, "Value classes must have exactly one attribute, got [#{names.join(', ')}] (#{names.count}) for [#{child}]"
16
+ end
17
+
18
+ type[names.first => value, &error]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class Interface
5
+ module Interfaces
6
+ autoload :Value, "dry/interface/interfaces/value"
7
+ autoload :Common, "dry/interface/interfaces/common"
8
+ autoload :Abstract, "dry/interface/interfaces/abstract"
9
+ autoload :Concrete, "dry/interface/interfaces/concrete"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/concerning"
4
+ require "active_support/core_ext/module/delegation"
5
+ require "active_support/descendants_tracker"
6
+ require "active_support/configurable"
7
+ require "active_support/inflector"
8
+ require "active_support/concern"
9
+ require "dry/struct"
10
+ require "dry/types"
11
+
12
+ module Dry
13
+ class Interface < Dry::Struct
14
+ autoload :Interfaces, "dry/interface/interfaces"
15
+ autoload :VERSION, "dry/interface/version"
16
+
17
+ extend ActiveSupport::DescendantsTracker
18
+ extend ActiveSupport::Inflector
19
+
20
+ schema schema.strict(true)
21
+
22
+ concerning :New, prepend: true do
23
+ prepended do
24
+ class << self
25
+ alias_method :_new, :new
26
+ end
27
+ end
28
+ end
29
+
30
+ module Types
31
+ include Dry.Types()
32
+ end
33
+
34
+ def self.Value(...)
35
+ Types.Value(...)
36
+ end
37
+
38
+ def self.type
39
+ direct_descendants.map(&:type).reduce(&:|)
40
+ end
41
+
42
+ def self.named
43
+ format "%<name>s<[%<names>s]>", { name: name, names: direct_descendants.map(&:named).join(" | ") }
44
+ end
45
+
46
+ def self.new(...)
47
+ type.call(...)
48
+ end
49
+
50
+ def self.initializer(owner, &block)
51
+ owner.schema owner.schema.constructor(&block)
52
+ end
53
+
54
+ def self.otherwise(&block)
55
+ initializer(self) do |input, type, &error|
56
+ type[input] { block[input, type, &error] }
57
+ end
58
+ end
59
+
60
+ def self.const_missing(name)
61
+ case name
62
+ when :Abstract
63
+ return Class.new(self) do
64
+ include Interfaces::Abstract
65
+ end
66
+ when :Concrete
67
+ return Class.new(self) do
68
+ include Interfaces::Concrete
69
+ end
70
+ when :Value
71
+ return Class.new(self) do
72
+ include Interfaces::Value
73
+ end
74
+ end
75
+
76
+ super
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-interface
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Oleander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-struct
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - oleander@users.noreply.github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/dry/interface.rb
63
+ - lib/dry/interface/interfaces.rb
64
+ - lib/dry/interface/interfaces/abstract.rb
65
+ - lib/dry/interface/interfaces/common.rb
66
+ - lib/dry/interface/interfaces/concrete.rb
67
+ - lib/dry/interface/interfaces/value.rb
68
+ homepage: https://github.com/oleander/dry-interface
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ rubygems_mfa_required: 'true'
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.0.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.2.22
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Dry::Interface
92
+ test_files: []