vident 0.4.1 → 0.5.1

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: eb36bbcf0d3a46ec51c1a68e80dbf0ef4b85076412591d30215779d1ff2816e8
4
- data.tar.gz: 6c4b6f98a4ac9bcbebaea4003a6532231a2e188aa94ec34889f4c1c624c9ae46
3
+ metadata.gz: a81f442775cc3413603ea1770059483978615f37809502245cca2f1ac8ddd868
4
+ data.tar.gz: 37ce0a04d8a8e37cfdc10f5b5174f771e93ecb2c8dd8909eb58ef842866d0ae2
5
5
  SHA512:
6
- metadata.gz: 6ccf024bee94cccffccc5b8cf7999b0b1b2e47c1a4185ab2f5a2f5129b7494d66cdde1d7d8ee8364123386dedd3f1f15ae60c27798db73cea3be4f22f68b24f6
7
- data.tar.gz: f09643a2d324a23012b5466b6bd7ad4a205f21238441891557a8d60d5a71860f5a64e866366bdb63744a62f6d7efed53bdfcb17f5711eab54e35675cefd60f88
6
+ metadata.gz: ec38a1d6550386705dd6de2374d650ee890c147c9cf97cca299b01bf27616f2becccdebe273efa51153ad74ace43faef8006464a5a2418e9991ca3642fdb8dca
7
+ data.tar.gz: a1b01b01507a70e276ec14b6a358c520f1bc11aca8c713fe32bb5a0091230caf9fd38e8f4ef22beed50ed8a1f72c0bb48c40cd5e10cdfaa546dafe80780af337
@@ -61,9 +61,11 @@ if Gem.loaded_specs.has_key? "dry-struct"
61
61
  attr_reader :schema, :attribute_ivar_names
62
62
 
63
63
  def attribute(name, signature = :any, **options, &converter)
64
- type_info = map_primitive_to_dry_type(signature, options, converter)
64
+ strict = !options[:convert]
65
+ signatures = extract_member_type_and_subclass(signature, options)
66
+ type_info = map_primitive_to_dry_type(signatures, strict, converter)
65
67
  type_info = set_constraints(type_info, options)
66
- type_info = set_metadata(type_info, signature, options)
68
+ type_info = set_metadata(type_info, signatures, options)
67
69
  define_on_schema(name, type_info, options)
68
70
  end
69
71
 
@@ -87,8 +89,8 @@ if Gem.loaded_specs.has_key? "dry-struct"
87
89
  type_info
88
90
  end
89
91
 
90
- def set_metadata(type_info, specified_type, options)
91
- metadata = {typed_attribute_type: specified_type, typed_attribute_options: options}
92
+ def set_metadata(type_info, signatures, options)
93
+ metadata = {typed_attribute_type: signatures, typed_attribute_options: options}
92
94
  type_info.meta(**metadata)
93
95
  end
94
96
 
@@ -100,7 +102,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
100
102
  @attribute_ivar_names ||= {}
101
103
  @attribute_ivar_names[attribute_name] = :"@#{attribute_name}"
102
104
  define_attribute_delegate(attribute_name) if delegates?(options)
103
- @schema ||= const_set("TypedSchema", Class.new(Vident::Attributes::TypedNilingStruct))
105
+ @schema ||= const_set(:TypedSchema, Class.new(Vident::Attributes::TypedNilingStruct))
104
106
  @schema.attribute attribute_name, type_info
105
107
  end
106
108
 
@@ -130,28 +132,45 @@ if Gem.loaded_specs.has_key? "dry-struct"
130
132
  allow_blank.nil? ? true : allow_blank
131
133
  end
132
134
 
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
135
+ def map_primitive_to_dry_type(signatures, strict, converter)
136
+ types = signatures.map do |type, subtype|
137
+ dry_type = dry_type_from_primary_type(type, strict, converter)
138
+ if subtype && dry_type.respond_to?(:of)
139
+ subtype_info = dry_type_from_primary_type(subtype, strict, converter)
140
+ # Sub types of collections currently can be nil - this should be an option
141
+ dry_type.of(subtype_info.optional.meta(required: false))
142
+ else
143
+ dry_type
144
+ end
143
145
  end
146
+ types.reduce(:|)
144
147
  end
145
148
 
146
149
  def extract_member_type_and_subclass(signature, options)
147
- if signature.is_a?(Array)
148
- [Array, signature.first]
150
+ case signature
151
+ when Set
152
+ signature.flat_map { |s| extract_member_type_and_subclass(s, options) }
153
+ when Array
154
+ [[Array, signature.first]]
149
155
  else
150
- [signature, options[:type] || options[:sub_type]]
156
+ [[signature, options[:type] || options[:sub_type]]]
151
157
  end
152
158
  end
153
159
 
154
160
  def dry_type_from_primary_type(type, strict, converter)
161
+ # If a converter is provided, we should use it to coerce the value
162
+ if converter && !strict && !type.is_a?(Symbol)
163
+ return Types.Constructor(type) do |value|
164
+ next value if value.is_a?(type)
165
+
166
+ converter.call(value).tap do |new_value|
167
+ unless new_value.is_a?(type)
168
+ raise ArgumentError, "Type conversion proc did not convert #{value} to #{type}"
169
+ end
170
+ end
171
+ end
172
+ end
173
+
155
174
  if type == :any
156
175
  Types::Nominal::Any
157
176
  elsif type == Integer
@@ -185,7 +204,12 @@ if Gem.loaded_specs.has_key? "dry-struct"
185
204
  # values using the default constructor, `new`.
186
205
  Types.Instance(type)
187
206
  else
188
- Types.Constructor(type) { |values| converter ? converter.call(values) : type.new(**values) }
207
+ # dry calls this when initialising the Type. Check if type of input is correct or create new instance
208
+ Types.Constructor(type) do |value|
209
+ next value if value.is_a?(type)
210
+
211
+ type.new(**value)
212
+ end
189
213
  end
190
214
  end
191
215
  end
data/lib/vident/base.rb CHANGED
@@ -104,7 +104,12 @@ module Vident
104
104
  else
105
105
  RootComponent::UsingViewComponent
106
106
  end
107
- klass.new(**stimulus_options_for_component(options))
107
+ element_attrs = options
108
+ .except(:id, :element_tag, :html_options, :controller, :controllers, :actions, :targets, :named_classes, :data_maps)
109
+ .merge(
110
+ stimulus_options_for_component(options)
111
+ )
112
+ klass.new(**element_attrs)
108
113
  end
109
114
  end
110
115
  alias_method :root, :parent_element
@@ -148,12 +153,11 @@ module Vident
148
153
  end
149
154
  module_function :stimulus_identifier_from_path
150
155
 
151
- protected
156
+ private
152
157
 
153
158
  # Prepare the stimulus attributes for a StimulusComponent
154
159
  def stimulus_options_for_component(options)
155
160
  {
156
- **options.except(:id, :element_tag, :html_options, :controller, :controllers, :actions, :targets, :named_classes, :data_maps),
157
161
  id: respond_to?(:id) ? id : (attribute(:id) || options[:id]),
158
162
  element_tag: attribute(:element_tag) || options[:element_tag] || :div,
159
163
  html_options: prepare_html_options(options[:html_options]),
@@ -167,8 +171,6 @@ module Vident
167
171
  }
168
172
  end
169
173
 
170
- private
171
-
172
174
  def prepare_html_options(erb_options)
173
175
  # Options should override in this order:
174
176
  # - defined on component class methods (lowest priority)
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "benchmark"
4
-
5
3
  # Rails fragment caching works by either expecting the cached key object to respond to `cache_key` or for that object
6
4
  # to be an array or hash. In our case the object maybe an instance of Core::Presenter so here we add a default
7
5
  # `cache_key` implementation.
@@ -26,10 +24,6 @@ module Vident
26
24
  named_cache_key_includes(name, *attrs)
27
25
  end
28
26
 
29
- def enable_cache_key_benchmarking
30
- @enable_cache_key_benchmarking = true
31
- end
32
-
33
27
  attr_reader :named_cache_key_attributes
34
28
 
35
29
  # TypedComponents can be used with fragment caching, but you need to be careful! Read on...
@@ -100,17 +94,7 @@ module Vident
100
94
  else
101
95
  @cache_key ||= {}
102
96
  end
103
-
104
- # TODO: remove the benchmarking code here
105
-
106
- if @enable_cache_key_benchmarking
107
- time = ::Benchmark.measure { generate_cache_key(n) }
108
- ::Logging::Log.debug "Cache key #{self.class.name}: #{time.real}"
109
- ::Thread.current[:total_key_generation_time] ||= 0
110
- ::Thread.current[:total_key_generation_time] += time.real
111
- else
112
- generate_cache_key(n)
113
- end
97
+ generate_cache_key(n)
114
98
  @cache_key[n]
115
99
  end
116
100
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.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.4.1
4
+ version: 0.5.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-26 00:00:00.000000000 Z
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport