vident 0.3.1 → 0.4.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: 24963e09fb8ecd09d22f1254eecc8f2d8bf255123e92976f12d224531003b42a
4
- data.tar.gz: 3b280628d9aa0ee62ad7b92b5ea58e628d3817e74567cfafb4ef22eadedd707e
3
+ metadata.gz: eb36bbcf0d3a46ec51c1a68e80dbf0ef4b85076412591d30215779d1ff2816e8
4
+ data.tar.gz: 6c4b6f98a4ac9bcbebaea4003a6532231a2e188aa94ec34889f4c1c624c9ae46
5
5
  SHA512:
6
- metadata.gz: fe03e658c1d0ca0e537b66cf465c9628566507053aaf7fbd2d03125a98c1f5f829a1a7c51fb2751314aa38648f3119337ebfa2df932d462dd4e2ab31a235b730
7
- data.tar.gz: 100fb3cbe9fce6cb5083ebd95237796f57d73f44a02385e274d0e433763c0037cbad8d257994aa059e5ab6e137fb781309ea24f2cf277e22ec59f79d3f370343
6
+ metadata.gz: 6ccf024bee94cccffccc5b8cf7999b0b1b2e47c1a4185ab2f5a2f5129b7494d66cdde1d7d8ee8364123386dedd3f1f15ae60c27798db73cea3be4f22f68b24f6
7
+ data.tar.gz: f09643a2d324a23012b5466b6bd7ad4a205f21238441891557a8d60d5a71860f5a64e866366bdb63744a62f6d7efed53bdfcb17f5711eab54e35675cefd60f88
@@ -13,6 +13,8 @@ if Gem.loaded_specs.has_key? "dry-struct"
13
13
  module Typed
14
14
  extend ActiveSupport::Concern
15
15
 
16
+ # TODO: better handling of when either class.schema is undefined (as no attributes configured) or when
17
+ # other methods ar called before prepare_attributes is called
16
18
  def prepare_attributes(attributes)
17
19
  @__attributes = self.class.schema.new(**attributes)
18
20
  end
@@ -58,32 +60,34 @@ if Gem.loaded_specs.has_key? "dry-struct"
58
60
 
59
61
  attr_reader :schema, :attribute_ivar_names
60
62
 
61
- def attribute(name, type = :any, **options)
62
- type_info = map_primitive_to_dry_type(type, !options[:convert])
63
- type_info = set_constraints(type_info, type, options)
63
+ def attribute(name, signature = :any, **options, &converter)
64
+ type_info = map_primitive_to_dry_type(signature, options, converter)
65
+ type_info = set_constraints(type_info, options)
66
+ type_info = set_metadata(type_info, signature, options)
64
67
  define_on_schema(name, type_info, options)
65
68
  end
66
69
 
67
70
  private
68
71
 
69
- def set_constraints(type_info, specified_type, options)
70
- member_klass = options[:type] || options[:sub_type]
71
- if member_klass && type_info.respond_to?(:of)
72
- # Sub types of collections currently can be nil - this should be an option
73
- type_info = type_info.of(
74
- map_primitive_to_dry_type(member_klass, !options[:convert]).optional.meta(required: false)
75
- )
72
+ def set_constraints(type_info, options)
73
+ if allows_nil?(options)
74
+ type_info = type_info.optional.meta(required: false)
75
+ end
76
+ unless allows_blank?(options)
77
+ type_info = type_info.constrained(filled: true)
76
78
  end
77
- type_info = type_info.optional.meta(required: false) if allows_nil?(options)
78
- type_info = type_info.constrained(filled: true) unless allows_blank?(options)
79
79
  if options[:default]&.is_a?(Proc)
80
80
  type_info = type_info.default(options[:default].freeze)
81
81
  elsif !options[:default].nil?
82
82
  type_info = type_info.default(->(_) { options[:default] }.freeze)
83
83
  end
84
- type_info = type_info.constrained(included_in: options[:in].freeze) if options[:in]
84
+ if options[:in]
85
+ type_info.constrained(included_in: options[:in].freeze)
86
+ end
87
+ type_info
88
+ end
85
89
 
86
- # Store adapter type info in the schema for use by typed form
90
+ def set_metadata(type_info, specified_type, options)
87
91
  metadata = {typed_attribute_type: specified_type, typed_attribute_options: options}
88
92
  type_info.meta(**metadata)
89
93
  end
@@ -92,12 +96,12 @@ if Gem.loaded_specs.has_key? "dry-struct"
92
96
  options[:delegates] != false
93
97
  end
94
98
 
95
- def define_on_schema(name, type_info, options)
99
+ def define_on_schema(attribute_name, type_info, options)
96
100
  @attribute_ivar_names ||= {}
97
- @attribute_ivar_names[name] = :"@#{name}"
98
- define_attribute_delegate(name) if delegates?(options)
99
- @schema ||= Class.new(Vident::Attributes::TypedNilingStruct)
100
- @schema.attribute name, type_info
101
+ @attribute_ivar_names[attribute_name] = :"@#{attribute_name}"
102
+ define_attribute_delegate(attribute_name) if delegates?(options)
103
+ @schema ||= const_set("TypedSchema", Class.new(Vident::Attributes::TypedNilingStruct))
104
+ @schema.attribute attribute_name, type_info
101
105
  end
102
106
 
103
107
  def define_attribute_delegate(attr_name)
@@ -106,7 +110,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
106
110
  def #{attr_name}
107
111
  @__attributes.attributes[:#{attr_name}]
108
112
  end
109
-
113
+
110
114
  def #{attr_name}?
111
115
  send(:#{attr_name}).present?
112
116
  end
@@ -126,7 +130,28 @@ if Gem.loaded_specs.has_key? "dry-struct"
126
130
  allow_blank.nil? ? true : allow_blank
127
131
  end
128
132
 
129
- def map_primitive_to_dry_type(type, strict)
133
+ def map_primitive_to_dry_type(signature, options, converter)
134
+ strict = !options[:convert]
135
+ type, subtype = extract_member_type_and_subclass(signature, options)
136
+ dry_type = dry_type_from_primary_type(type, strict, converter)
137
+ if subtype && dry_type.respond_to?(:of)
138
+ subtype_info = dry_type_from_primary_type(subtype, strict, converter)
139
+ # Sub types of collections currently can be nil - this should be an option
140
+ dry_type.of(subtype_info.optional.meta(required: false))
141
+ else
142
+ dry_type
143
+ end
144
+ end
145
+
146
+ def extract_member_type_and_subclass(signature, options)
147
+ if signature.is_a?(Array)
148
+ [Array, signature.first]
149
+ else
150
+ [signature, options[:type] || options[:sub_type]]
151
+ end
152
+ end
153
+
154
+ def dry_type_from_primary_type(type, strict, converter)
130
155
  if type == :any
131
156
  Types::Nominal::Any
132
157
  elsif type == Integer
@@ -160,7 +185,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
160
185
  # values using the default constructor, `new`.
161
186
  Types.Instance(type)
162
187
  else
163
- Types.Constructor(type) { |values| type.new(**values) }
188
+ Types.Constructor(type) { |values| converter ? converter.call(values) : type.new(**values) }
164
189
  end
165
190
  end
166
191
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vident
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-11 00:00:00.000000000 Z
11
+ date: 2023-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport