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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc10cd9d71e3f255a6e3e520acb9fa39e307308a97b227107a0deca3d97b5cb6
4
- data.tar.gz: 96d8127169a6f4bef13b1f14128c599b78038c151547521ca5faf668f6d18975
3
+ metadata.gz: 15305efec69e9366ea2169a4abf832a1619cc87e5aeef2be0e0d324e4fe49db7
4
+ data.tar.gz: 50cc4a0a8ba539dcb28ff31f29dfc0e487633753542b30ca5af8222ada5d57c0
5
5
  SHA512:
6
- metadata.gz: 41554f77f48de3d91bc3fbb63662db1a3064f6e2c1fe44a7913c3e14658501b7de908a230e0f67410779ce4403d116157f64e687b16090d7be0e0acb04e5c9b0
7
- data.tar.gz: 9d0c18ec90d9f283a13754f1dc78c43c117afe0c688bd321a4aa26101c0396eda9bb0a46061ceae2ee99288b8d4733e5981e000faeea176a7d487cf609aaf897
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
+
@@ -64,7 +64,6 @@ module BloodContracts
64
64
  end
65
65
 
66
66
  def inspect
67
- require'pry';binding.pry
68
67
  "#<pipe #{self.class.name} = #{steps_with_names.join(' > ')} (value=#{@value})>"
69
68
  end
70
69
  end
@@ -59,11 +59,6 @@ module BloodContracts
59
59
  )
60
60
  end
61
61
 
62
- def inspect
63
- require'pry';binding.pry
64
- super
65
- end
66
-
67
62
  protected
68
63
 
69
64
  def refined?(object)
@@ -4,49 +4,74 @@ module BloodContracts
4
4
  class << self
5
5
  attr_reader :attributes, :names, :finalized
6
6
 
7
- def new(*args, **kwargs)
8
- return super(*args, **kwargs) if finalized
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
- pipe = Class.new(Tuple) { def inspect; super; end }
12
- pipe.instance_variable_set(:@attributes, args)
13
- pipe.instance_variable_set(:@names, kwargs[:names].to_a)
14
- pipe.instance_variable_set(:@finalized, true)
15
- pipe
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(*args)
21
- super
22
- @values = args
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 unless (failure = matches.find?(&:invalid?)).nil?
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| match.values.map(&method(:unpack_refined)) }
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(attributes).map { |k, v| [k, v].join('=') }
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
@@ -1,5 +1,5 @@
1
1
  module BloodContracts
2
2
  module Core
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  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.0
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-25 00:00:00.000000000 Z
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