yarrow 0.7.4 → 0.7.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c19632ef74b70bca618fa332e555bc93a277ccb71791df1e43f871b2d6d54e7
4
- data.tar.gz: e1c4be606929e0d1c3a07e21bcf43b9d89847b45ccc59ff3a78ee9713a64bb55
3
+ metadata.gz: b067dcc24e059e6d5cfe506e6a3fc30b5679eb05e494ce842e515710ff60d170
4
+ data.tar.gz: c185b1f52f5b7aa173b26b179d47457f7705ef91460abd046ce3c81fa4733930
5
5
  SHA512:
6
- metadata.gz: 592a83f0ae68084ad789353f8319a290b02e2268c00fd84fed70cba5e1469f5cbd215ea17732801622c8b10867513bdd37fd02dc8bda95d75c3a70014230fb46
7
- data.tar.gz: f1eab1e023451b5bbd6a88aecf610a887db96b37fff19c6c4cf500dacfadeb0461865d4ffad118f4b4d254889e52bbbc4583b8d74e5299efe7940308a1eeba07
6
+ metadata.gz: 33c522da6809b8ffbcd0457082a20a3222b877d086856413b815e141004769fdfc37998903d5f8b5a711dba21614323381fb2021f702082136b3f073b885abbf
7
+ data.tar.gz: a9b2a1ae9d015093709c7f64073809a470b058003ea98984ad1f8d6700cc65281e3e8aafc9e03ce9a669adf4e3800385f1727fe57cf5d54e95ac31862b603516
@@ -2,10 +2,12 @@ module Yarrow
2
2
  module Schema
3
3
  module Definitions
4
4
  DEFINED_TYPES = {
5
- string: Type::Raw[String],
6
- integer: Type::Raw[Integer],
7
- path: Type::Raw[Pathname],
8
- any: Type::Any
5
+ #string: Types::String,
6
+ string: Types::Instance.of(String),
7
+ #integer: Types::Integer,
8
+ integer: Types::Instance.of(Integer),
9
+ path: Types::Instance.of(Pathname),
10
+ any: Types::Any.new
9
11
  }
10
12
 
11
13
  def self.register(identifier, type_class)
@@ -23,6 +23,25 @@ module Yarrow
23
23
  @attrs_spec.keys
24
24
  end
25
25
 
26
+ def cast(input)
27
+ missing_attrs = @attrs_spec.keys.difference(input.keys)
28
+
29
+ if missing_attrs.any?
30
+ missing_attrs.each do |name|
31
+ raise "wrong number of attributes" unless @attrs_spec[name].is_a?(Types::Any)
32
+ end
33
+ end
34
+
35
+ mismatching_attrs = input.keys.difference(@attrs_spec.keys)
36
+
37
+ raise "attribute does not exist" if mismatching_attrs.any?
38
+
39
+ input.reduce({}) do |converted, (name, value)|
40
+ converted[name] = @attrs_spec[name].cast(value)
41
+ converted
42
+ end
43
+ end
44
+
26
45
  def check(input)
27
46
  missing_attrs = @attrs_spec.keys.difference(input.keys)
28
47
 
@@ -16,12 +16,9 @@ module Yarrow
16
16
  end
17
17
 
18
18
  def initialize(config)
19
- dictionary.check(config)
20
- # dictionary.each_key do |name|
21
- # raise "missing declared attribute #{name}" unless config.key?(name)
22
- # end
23
- #
24
- config.each_pair do |key, value|
19
+ converted = dictionary.cast(config)
20
+
21
+ converted.each_pair do |key, value|
25
22
  # raise "#{key} not a declared attribute" unless dictionary.key?(key)
26
23
  #
27
24
  # defined_type = dictionary[key]
@@ -24,10 +24,25 @@ module Yarrow
24
24
  new(unit_type)
25
25
  end
26
26
 
27
- attr_reader :unit
27
+ attr_reader :unit, :accepts
28
28
 
29
29
  def initialize(unit_type=nil)
30
30
  @unit = unit_type
31
+ @accepts = {}
32
+ end
33
+
34
+ def accept(type, constructor)
35
+ accepts[type] = constructor
36
+ self
37
+ end
38
+
39
+ def should_coerce?(input)
40
+ accepts.key?(input.class)
41
+ end
42
+
43
+ def coerce(input)
44
+ constructor = accepts[input.class]
45
+ unit.send(constructor, input)
31
46
  end
32
47
 
33
48
  def check_instance_of!(input)
@@ -54,7 +69,10 @@ module Yarrow
54
69
  end
55
70
  end
56
71
 
57
- def cast(input); end
72
+ def cast(input)
73
+ return coerce(input) if should_coerce?(input)
74
+ check(input)
75
+ end
58
76
  end
59
77
 
60
78
  class Any < TypeClass
@@ -64,14 +82,14 @@ module Yarrow
64
82
  end
65
83
 
66
84
  class Instance < TypeClass
67
- def cast(input)
85
+ def check(input)
68
86
  check_instance_of!(input)
69
87
  input
70
88
  end
71
89
  end
72
90
 
73
91
  class Kind < TypeClass
74
- def cast(input)
92
+ def check(input)
75
93
  check_kind_of!(input)
76
94
  input
77
95
  end
@@ -94,7 +112,7 @@ module Yarrow
94
112
 
95
113
  alias members unit
96
114
 
97
- def cast(input)
115
+ def check(input)
98
116
  case implementation
99
117
  when :any then check_respond_to_any!(input, members)
100
118
  when :all then check_respond_to_all!(input, members)
@@ -43,9 +43,9 @@ module Yarrow
43
43
  kwargs
44
44
  end
45
45
 
46
- validator.check(attr_values)
47
- # TODO: type coercion or mapping decision goes here
48
- super(**attr_values)
46
+ converted_values = validator.cast(attr_values)
47
+
48
+ super(**converted_values)
49
49
 
50
50
  freeze
51
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module Yarrow
3
3
  APP_NAME = 'Yarrow'
4
- VERSION = '0.7.4'
4
+ VERSION = '0.7.5'
5
5
  end
data/lib/yarrow.rb CHANGED
@@ -5,7 +5,6 @@ require 'yarrow/version'
5
5
  require 'yarrow/extensions'
6
6
  require 'yarrow/symbols'
7
7
  require 'yarrow/logging'
8
- require 'yarrow/schema'
9
8
  require 'yarrow/schema/types'
10
9
  require 'yarrow/schema/definitions'
11
10
  require 'yarrow/schema/dictionary'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yarrow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
@@ -242,7 +242,6 @@ files:
242
242
  - lib/yarrow/process/project_manifest.rb
243
243
  - lib/yarrow/process/step_processor.rb
244
244
  - lib/yarrow/process/workflow.rb
245
- - lib/yarrow/schema.rb
246
245
  - lib/yarrow/schema/definitions.rb
247
246
  - lib/yarrow/schema/dictionary.rb
248
247
  - lib/yarrow/schema/entity.rb
data/lib/yarrow/schema.rb DELETED
@@ -1,41 +0,0 @@
1
- require "delegate"
2
-
3
- module Yarrow
4
- module Schema
5
- module Type
6
- class Raw
7
- class << self
8
- def [](primitive)
9
- @primitive = primitive
10
- end
11
-
12
- def new(input)
13
- input
14
- end
15
- end
16
- end
17
-
18
- class Any
19
- end
20
- # class Attribute
21
- # class << self
22
- # def accepts(attr_type)
23
- # @accepts = attr_type
24
- # end
25
- # end
26
- #
27
- # attr_accessor :value
28
- # alias_method :__getobj__, :value
29
- #
30
- # def initialize(value)
31
- # raise "Invalid type" unless @accepts.is_a?(value.class)
32
- # @value = value
33
- # end
34
- # end
35
- #
36
- # class Text < Attribute
37
- # accepts String
38
- # end
39
- end
40
- end
41
- end