blood_contracts-core 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/tuple.rb +45 -0
- data/lib/blood_contracts/core/pipe.rb +0 -1
- data/lib/blood_contracts/core/refined.rb +0 -5
- data/lib/blood_contracts/core/tuple.rb +40 -15
- data/lib/blood_contracts/core/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15305efec69e9366ea2169a4abf832a1619cc87e5aeef2be0e0d324e4fe49db7
|
4
|
+
data.tar.gz: 50cc4a0a8ba539dcb28ff31f29dfc0e487633753542b30ca5af8222ada5d57c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94b4bc3793c8a4259f0111daca712e3949361df4b72ce2e95cac9b5e5d64754bf1694e717eb134cb83249b7b028211995c000344afa64fc2b198d11f33817a1d
|
7
|
+
data.tar.gz: 2b43b3fa1b14b0166bfab0aee21406075179de2d58bf68fbce54eece179cc8334b07b5246c2864383f0fdbbf356b5cc018ec0f3b987b5937ef3c34558c5b3f0b
|
data/examples/tuple.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "json"
|
3
|
+
require 'blood_contracts/core'
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
module Types
|
7
|
+
class JSON < BC::Refined
|
8
|
+
def match
|
9
|
+
super do
|
10
|
+
begin
|
11
|
+
context[:parsed] = ::JSON.parse(unpack_refined(@value))
|
12
|
+
self
|
13
|
+
rescue StandardError => error
|
14
|
+
failure(error)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def unpack
|
20
|
+
super { |match| match.context[:parsed] }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Symbol < BC::Refined
|
25
|
+
def match
|
26
|
+
super do
|
27
|
+
begin
|
28
|
+
context[:as_symbol] = unpack_refined(@value).to_sym
|
29
|
+
self
|
30
|
+
rescue StandardError => error
|
31
|
+
failure(error)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def unpack
|
37
|
+
super { |match| match.context[:as_symbol] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Config = BC::Tuple.new(Types::Symbol, Types::JSON, names: %i(name config))
|
43
|
+
c = Config.new("test", '{"some": "value"}')
|
44
|
+
binding.pry
|
45
|
+
|
@@ -4,49 +4,74 @@ module BloodContracts
|
|
4
4
|
class << self
|
5
5
|
attr_reader :attributes, :names, :finalized
|
6
6
|
|
7
|
-
def new(*args
|
8
|
-
return super(*args
|
7
|
+
def new(*args)
|
8
|
+
return super(*args) if finalized
|
9
|
+
if args.last.is_a?(Hash)
|
10
|
+
names = args.pop.delete(:names)
|
11
|
+
end
|
9
12
|
|
10
13
|
raise ArgumentError unless args.all?(Class)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
tuple = Class.new(Tuple) { def inspect; super; end }
|
15
|
+
tuple.instance_variable_set(:@attributes, args)
|
16
|
+
tuple.instance_variable_set(:@names, names.to_a)
|
17
|
+
tuple.instance_variable_set(:@finalized, true)
|
18
|
+
tuple
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
19
22
|
attr_reader :values
|
20
|
-
def initialize(*
|
21
|
-
|
22
|
-
@
|
23
|
+
def initialize(*values, context: Hash.new { |h,k| h[k] = Hash.new }, **)
|
24
|
+
_validate_args!(values)
|
25
|
+
@errors = []
|
26
|
+
@context = context
|
27
|
+
@values = values
|
23
28
|
end
|
24
29
|
|
25
30
|
def match
|
26
31
|
super do
|
27
|
-
matches = self.class.attributes.zip(values).map do |(type, value)|
|
32
|
+
@matches = self.class.attributes.zip(values).map do |(type, value)|
|
28
33
|
type.match(value, context: @context)
|
29
34
|
end
|
30
|
-
next self
|
35
|
+
next self if (failure = @matches.find(&:invalid?)).nil?
|
31
36
|
failure
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
35
40
|
def unpack
|
36
|
-
super { |match|
|
41
|
+
super { |match| @matches.map(&:unpack) }
|
37
42
|
end
|
38
43
|
alias :to_ary :unpack
|
44
|
+
alias :to_a :unpack
|
45
|
+
|
46
|
+
def unpack_h
|
47
|
+
@unpack_h ||= Hash[
|
48
|
+
unpack.map.with_index do |unpacked, index|
|
49
|
+
key = self.class.names[index] || index
|
50
|
+
[key, unpacked]
|
51
|
+
end
|
52
|
+
]
|
53
|
+
end
|
54
|
+
alias :to_hash :unpack_h
|
55
|
+
alias :to_h :unpack_h
|
39
56
|
|
40
57
|
private def values_by_names
|
41
58
|
if self.class.names.empty?
|
42
59
|
self.values
|
43
60
|
else
|
44
|
-
self.class.names.zip(
|
61
|
+
self.class.names.zip(values).map { |k, v| [k, v].join('=') }
|
45
62
|
end
|
46
63
|
end
|
47
64
|
|
65
|
+
private def _validate_args!(values)
|
66
|
+
return if values.size == self.class.attributes.size
|
67
|
+
raise ArgumentError, <<~MESSAGE
|
68
|
+
wrong number of arguments (given #{values.size}, \
|
69
|
+
expected #{self.class.attributes.size})
|
70
|
+
MESSAGE
|
71
|
+
end
|
72
|
+
|
48
73
|
private def inspect
|
49
|
-
"#<tuple #{self.class.name} (#{values_by_names.join(',')}>"
|
74
|
+
"#<tuple #{self.class.name} of (#{values_by_names.join(',')})>"
|
50
75
|
end
|
51
76
|
end
|
52
77
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blood_contracts-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Dolganov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin/
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- blood_contracts-core.gemspec
|
87
87
|
- examples/json_response.rb
|
88
|
+
- examples/tuple.rb
|
88
89
|
- lib/blood_contracts-core.rb
|
89
90
|
- lib/blood_contracts/core.rb
|
90
91
|
- lib/blood_contracts/core/contract.rb
|