yarrow 0.7.4 → 0.7.6

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: 0c19632ef74b70bca618fa332e555bc93a277ccb71791df1e43f871b2d6d54e7
4
- data.tar.gz: e1c4be606929e0d1c3a07e21bcf43b9d89847b45ccc59ff3a78ee9713a64bb55
3
+ metadata.gz: 0dba4f651517ceb22a806d2ac97637327983742ce4d6837ec0cc85cb4b9864c3
4
+ data.tar.gz: d7b2db29b33539da364b386023a662db2e4f5bcc009869444de005ae74f919fb
5
5
  SHA512:
6
- metadata.gz: 592a83f0ae68084ad789353f8319a290b02e2268c00fd84fed70cba5e1469f5cbd215ea17732801622c8b10867513bdd37fd02dc8bda95d75c3a70014230fb46
7
- data.tar.gz: f1eab1e023451b5bbd6a88aecf610a887db96b37fff19c6c4cf500dacfadeb0461865d4ffad118f4b4d254889e52bbbc4583b8d74e5299efe7940308a1eeba07
6
+ metadata.gz: 0b6b0550245ef7ac0f752c602bcc1a191a8260cf8e4a3553b77f1a600d82ebf773e97adcaffa2bc1ed1d913af9a32d5d183472fdabcabd9ba3e61a52cc9155eb
7
+ data.tar.gz: '099eeb8f8f90867144ad3ca2586d353b1b69e38af9534923c0856410b944592e48847b49650d0526af724f74851ad8f93205fc2032b864eb1a854c0d3f59475a'
data/lib/yarrow/config.rb CHANGED
@@ -42,6 +42,11 @@ module Yarrow
42
42
  #:root_dir
43
43
  )
44
44
 
45
+ # Content policy
46
+ # Model = Yarrow::Schema::Value.new(
47
+
48
+ # )
49
+
45
50
  # Top level root config namespace.
46
51
  #
47
52
  # `content_dir` and `output_dir` are placeholders and should be overriden
@@ -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)
@@ -104,7 +122,73 @@ module Yarrow
104
122
  end
105
123
  end
106
124
 
107
- class Union
125
+ module CompoundType
126
+ def instance(unit_type)
127
+ @unit = Instance.of(unit_type)
128
+ self
129
+ end
130
+
131
+ def kind(unit_type)
132
+ @unit = Kind.of(unit_type)
133
+ self
134
+ end
135
+
136
+ def interface(*args)
137
+ @unit = Interface.of(args)
138
+ self
139
+ end
140
+ end
141
+
142
+ class List < TypeClass
143
+ include CompoundType
144
+
145
+ def self.of(unit_type)
146
+ new(Instance.of(unit_type))
147
+ end
148
+
149
+ def cast(input)
150
+ input.map do |item|
151
+ unit.cast(item)
152
+ end
153
+ end
154
+ end
155
+
156
+ class Map < TypeClass
157
+ include CompoundType
158
+
159
+ def self.of(map_spec)
160
+ if map_spec.is_a?(Hash)
161
+ if map_spec.size == 1
162
+ key_type, value_type = map_spec.first
163
+ else
164
+ raise "map requires a single key => value type"
165
+ end
166
+ else
167
+ key_type = Symbol
168
+ value_type = map_spec
169
+ end
170
+
171
+ new(Instance.of(key_type), Instance.of(value_type))
172
+ end
173
+
174
+ attr_reader :key_type
175
+ alias value_type unit
176
+
177
+ def initialize(key_type, value_type)
178
+ @key_type = key_type
179
+ super(value_type)
180
+ end
181
+
182
+ def check(input)
183
+ keys = input.keys.map do |key|
184
+ key_type.cast(key)
185
+ end
186
+ values = input.values.map do |value|
187
+ value_type.cast(value)
188
+ end
189
+
190
+ [keys, values].transpose.to_h
191
+ end
108
192
  end
109
193
  end
110
194
  end
@@ -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.6'
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,14 +1,14 @@
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.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-09 00:00:00.000000000 Z
11
+ date: 2022-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mementus
@@ -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