vident 0.3.0 → 0.4.0

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: c2f2d014f440bdd33f9910c04ba79846825afb4c8a61bd6e5079079fd07c0473
4
- data.tar.gz: fb730836ba6c63a582f97afcaceed12467a1b8ac92f2f5d9a0d871a4791c3de4
3
+ metadata.gz: d1ead61d39ae1b70058a4002d6b3ed7ad4baade0d03ccaa6f2501ba20ac028b5
4
+ data.tar.gz: bbebb2b863ee0e1b734dc50c6a03015e0375a28a43004f9f4954c681acd954ca
5
5
  SHA512:
6
- metadata.gz: da7e90adb10d5301803cde48f06685983478c01eec44bfe0c8db9a0909734572ccc1ee72490850655bc915f4c2de17d27b1191d3d649c53d7a23d2d1e40b83a2
7
- data.tar.gz: 2626db1242b53e3abe20c75a8269125e564e1bc89a178f6e76feccda869ef2c41d671d0ece1a48f55441d6e4483e4962c76118ba48beba991529fe2e0ed322cc
6
+ metadata.gz: 1ac922f2933bd655a2aa69ca5fbeb4bb05bb6ac8277a7bb0c7e6a85c6329aebe0dcc63b0fcdf5a03c29b775348044c536d9f460c6006a43b5190418cc47d23f2
7
+ data.tar.gz: 26f4db0079c735b6df0fa26aca20e96a564e24d07dca71b6a1396ea95a86993ef8a99209df6bdfd1be618dfa8f6933281e1506318ad2fb592ce42e47b0456ba7
@@ -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
@@ -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,29 @@ 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
+ # Sub types of collections currently can be nil - this should be an option
139
+ dry_type.of(
140
+ map_primitive_to_dry_type(subtype, options, converter).optional.meta(required: false)
141
+ )
142
+ else
143
+ dry_type
144
+ end
145
+ end
146
+
147
+ def extract_member_type_and_subclass(signature, options)
148
+ if signature.is_a?(Array)
149
+ [Array, signature.first]
150
+ else
151
+ [signature, options[:type] || options[:sub_type]]
152
+ end
153
+ end
154
+
155
+ def dry_type_from_primary_type(type, strict, converter)
130
156
  if type == :any
131
157
  Types::Nominal::Any
132
158
  elsif type == Integer
@@ -160,7 +186,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
160
186
  # values using the default constructor, `new`.
161
187
  Types.Instance(type)
162
188
  else
163
- Types.Constructor(type) { |values| type.new(**values) }
189
+ Types.Constructor(type) { |values| converter ? converter.call(values) : type.new(**values) }
164
190
  end
165
191
  end
166
192
  end
data/lib/vident/base.rb CHANGED
@@ -29,7 +29,7 @@ module Vident
29
29
 
30
30
  # stimulus controller identifier
31
31
  def stimulus_identifier
32
- stimulus_identifier_from_path(identifier_name_path)
32
+ ::Vident::Base.stimulus_identifier_from_path(identifier_name_path)
33
33
  end
34
34
 
35
35
  def identifier_name_path
@@ -40,10 +40,6 @@ module Vident
40
40
  end
41
41
  end
42
42
 
43
- def stimulus_identifier_from_path(path)
44
- path.split("/").map { |p| p.to_s.dasherize }.join("--")
45
- end
46
-
47
43
  def phlex_component?
48
44
  @phlex_component ||= ancestors.map(&:name).include?("Phlex::HTML")
49
45
  end
@@ -147,6 +143,11 @@ module Vident
147
143
  self.class.identifier_name_path
148
144
  end
149
145
 
146
+ def stimulus_identifier_from_path(path)
147
+ path.split("/").map { |p| p.to_s.dasherize }.join("--")
148
+ end
149
+ module_function :stimulus_identifier_from_path
150
+
150
151
  protected
151
152
 
152
153
  # Prepare the stimulus attributes for a StimulusComponent
@@ -100,6 +100,9 @@ module Vident
100
100
  else
101
101
  @cache_key ||= {}
102
102
  end
103
+
104
+ # TODO: remove the benchmarking code here
105
+
103
106
  if @enable_cache_key_benchmarking
104
107
  time = ::Benchmark.measure { generate_cache_key(n) }
105
108
  ::Logging::Log.debug "Cache key #{self.class.name}: #{time.real}"
@@ -45,7 +45,7 @@ module Vident
45
45
  def test_renders_with_valid_attrs_#{index}
46
46
  test_attrs = #{test}
47
47
  begin
48
- render_inline(#{class_under_test}.new(**test_attrs))
48
+ @results_content << render_inline(#{class_under_test}.new(**test_attrs))
49
49
  rescue => error
50
50
  assert(false, "Should not raise with #{test.to_s.tr("\"", "'")} but did raise \#{error}")
51
51
  end
@@ -58,7 +58,7 @@ module Vident
58
58
  def test_raises_with_invalid_attrs_#{index}
59
59
  test_attrs = #{test}
60
60
  assert_raises(StandardError, "Should raise with #{test.to_s.tr("\"", "'")}") do
61
- render_inline(#{class_under_test}.new(**test_attrs))
61
+ @results_content << render_inline(#{class_under_test}.new(**test_attrs))
62
62
  end
63
63
  end
64
64
  RUBY
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
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.0
4
+ version: 0.4.0
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-05 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport