svelte-on-rails 9.3.3 → 9.3.4

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: 6acd26e8d04eaa789b173da6fefb7d0746a5594e3924bca9329c2deeedc66d7f
4
- data.tar.gz: e518a366fc727de81797f9159371f3d917926956cf3046439944baa1aa641fd6
3
+ metadata.gz: 4351ebf2a7a0cb7a957bf9593eaa07f2eb158d5e29c3d28ceca46a45ab4ba664
4
+ data.tar.gz: 406e23ce8765f4ac0387b26cf0384bf8edbd5666bf2049dcc689ea55592230f0
5
5
  SHA512:
6
- metadata.gz: 0f4c20a508c14810fff8570bca050f23b1e12babe1d6939f90046d43ebcbe14c85ce237f441b83227c9888dcd4d3c4518562bf4e8f73f11eba6b077fd21dc5fd
7
- data.tar.gz: 1ddf6e95118890b7c46849ff2ed97f4953fe164fff42ea6bb4cbaa84973528c9c1209be1393f6d30bda5b5d12a89a999ad91ceb7f31b5787aa40fec2945807b2
6
+ metadata.gz: dd17c528584d0f2cca9ee940e17ef7079c371c47c3ecb8d8c7881efa0321922ff1281703f6603c1c44455e3eed9720621e4e20903179f29a0332b7983f5c0fae
7
+ data.tar.gz: 575032b254b1d6cfb09f0d669564f55581436936377ed15938fe816d2acb8e782594027caef777d5b0bfda213aaabf837dd8cc5492dff832d700976b98fda547
@@ -8,7 +8,7 @@ module SvelteOnRails
8
8
  def to_svelte(*attributes, **associations)
9
9
  @to_svelte ||= begin
10
10
 
11
- tsv = SvelteOnRails::Lib::ToSvelteValues.new
11
+ tsv = SvelteOnRails::Lib::ToSvelteValues
12
12
  tsl = SvelteOnRails::Lib::ToSvelteLabels.new
13
13
  res = tsl.extract_labels(self.class, attributes, associations)
14
14
 
@@ -42,7 +42,7 @@ module SvelteOnRails
42
42
  def to_svelte(*attributes, **associations)
43
43
  @to_svelte ||= begin
44
44
  cl = SvelteOnRails::Lib::ToSvelteValues
45
- val = cl.new.extract_values(self, attributes, associations)
45
+ val = cl.extract_values(self, attributes, associations)
46
46
 
47
47
  tsl = SvelteOnRails::Lib::ToSvelteLabels.new
48
48
  res = tsl.extract_labels(self.klass, attributes, associations)
@@ -1,75 +1,125 @@
1
1
  module SvelteOnRails
2
2
  module Lib
3
3
  class ToSvelteValues
4
- def extract_values(record, attributes, associations, call_stack: 0)
4
+ # Public API – returns Hash for single record, Array<Hash> for collections
5
+ def self.extract_values(record, attributes = [], associations = {})
6
+ records = normalize_to_enumerable(record, associations)
5
7
 
6
- next_stack = call_stack + 1
8
+ result = extract_values_recursive(records, attributes, associations, call_stack: 0)
9
+
10
+ # Mirror input shape for the top-level return value
11
+ looks_like_single?(record) ? (result.first || {}) : result
12
+ end
7
13
 
8
- if record.respond_to?(:each)
9
-
10
- recs2 = if associations.include?(:offset) || associations.include?(:limit)
11
- if (record.is_a?(ActiveRecord::Relation) || record.is_a?(ActiveRecord::Associations::CollectionProxy) rescue false)
12
- _recs = (associations[:offset] ? record.offset(associations[:offset]) : record)
13
- (associations[:limit] ? _recs.limit(associations[:limit]) : _recs)
14
- elsif record.respond_to?(:drop) && record.respond_to?(:take) # that might be a array
15
- _recs = associations[:offset] ? record.drop(associations[:offset]) : record
16
- associations[:limit] ? _recs.take(associations[:limit]) : _recs
17
- else
18
- raise "[svelte-on-rails:to_svelte] unknown class for records: #{record}"
19
- end
20
- else
21
- record
22
- end
23
-
24
- values = recs2.map do |rec|
25
- extract_values(rec, attributes, associations, call_stack: next_stack)
14
+ private
15
+
16
+ def self.normalize_to_enumerable(record, associations)
17
+ if looks_like_collection?(record)
18
+ apply_pagination(record, associations)
19
+ else
20
+ [record] # single wrap recursive code can be uniform
21
+ end
22
+ end
23
+
24
+ def self.apply_pagination(coll, associations)
25
+ if associations.key?(:offset) || associations.key?(:limit)
26
+ if active_record_collection?(coll)
27
+ scope = coll
28
+ scope = scope.offset(associations[:offset]) if associations.key?(:offset)
29
+ scope = scope.limit(associations[:limit]) if associations.key?(:limit)
30
+ scope.to_a
31
+ elsif coll.respond_to?(:drop) && coll.respond_to?(:take)
32
+ res = coll
33
+ res = res.drop(associations[:offset]) if associations.key?(:offset)
34
+ res = res.take(associations[:limit]) if associations.key?(:limit)
35
+ res
36
+ else
37
+ raise "[svelte-on-rails:to_svelte] Cannot paginate #{coll.class}"
26
38
  end
39
+ else
40
+ coll.respond_to?(:to_a) ? coll.to_a : coll
41
+ end
42
+ end
43
+
44
+ def self.extract_values_recursive(thing, attributes, associations, call_stack:)
45
+ next_stack = call_stack + 1
27
46
 
47
+ if collection_like?(thing)
48
+ thing.map do |record|
49
+ extract_single_record(record, attributes, associations, next_stack)
50
+ end
28
51
  else
52
+ extract_single_record(thing, attributes, associations, next_stack)
53
+ end
54
+ end
29
55
 
30
- # we have a single record
56
+ def self.extract_single_record(record, attributes, associations, next_stack)
57
+ return {} unless record # or raise – depending on your preference
31
58
 
32
- values = {}
33
- enums = record.class.defined_enums
59
+ values = {}
34
60
 
35
- attributes.each do |attr|
36
- raise "Invalid attribute «#{attr}» on «#{record.class}»" unless [Symbol, String].include?(attr.class)
37
- raise "[svelte-on-rails:to_svelte] #{record.class} does not respond to: #{attr}" unless record.respond_to?(attr)
38
- key_s = attr.to_s
61
+ enums = record.class.try(:defined_enums) || {}
39
62
 
40
- if enums.key?(key_s)
41
- values[key_s] = record.send(key_s)
42
- else
43
- values[key_s] = record.send(key_s)
44
- end
63
+ attributes.each do |attr|
64
+ validate_attribute!(record, attr)
45
65
 
46
- end
66
+ key_s = attr.to_s
67
+ value = record.send(attr)
47
68
 
48
- associations.each do |key, val|
49
- key_s = key.to_s
50
- next if ['offset', 'limit'].include?(key_s)
51
- _attrs, _assocs = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(val)
52
- reflect = record.class.reflect_on_association(key_s)
53
- raise "invalid association: «#{key_s}» for «#{record.class}»" unless reflect
54
-
55
- recs = record.send(key_s)
56
- if recs.present?
57
- values[key_s] = extract_values(
58
- recs,
59
- _attrs,
60
- _assocs,
61
- call_stack: next_stack
62
- )
63
- end
69
+ values[key_s] = enums.key?(key_s) ? record.send(key_s) : value
70
+ end
64
71
 
65
- end
72
+ associations.each do |key, val|
73
+ key_s = key.to_s
74
+ next if %w[offset limit].include?(key_s)
75
+
76
+ reflection = record.class.reflect_on_association(key_s)
77
+ raise ArgumentError, "No association #{key_s.inspect} on #{record.class}" unless reflection
78
+
79
+ related = record.send(key_s)
80
+ next if related.nil? || (related.respond_to?(:empty?) && related.empty?)
81
+
82
+ sub_attrs, sub_assocs = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(val)
83
+
84
+ values[key_s] = extract_values_recursive(
85
+ related,
86
+ sub_attrs,
87
+ sub_assocs,
88
+ call_stack: next_stack
89
+ )
66
90
  end
67
91
 
68
92
  values
93
+ end
94
+
95
+ # Helpers ────────────────────────────────────────────────
69
96
 
97
+ def self.looks_like_single?(obj)
98
+ !looks_like_collection?(obj)
70
99
  end
71
100
 
101
+ def self.looks_like_collection?(obj)
102
+ return false if obj.nil? || obj.is_a?(Hash) || obj.is_a?(String)
103
+ obj.respond_to?(:each)
104
+ end
105
+
106
+ def self.collection_like?(obj)
107
+ looks_like_collection?(obj) || obj.is_a?(Array)
108
+ end
109
+
110
+ def self.active_record_collection?(obj)
111
+ (obj.is_a?(ActiveRecord::Relation) ||
112
+ obj.is_a?(ActiveRecord::Associations::CollectionProxy)) rescue false
113
+ end
72
114
 
115
+ def self.validate_attribute!(record, attr)
116
+ unless attr.is_a?(Symbol) || attr.is_a?(String)
117
+ raise ArgumentError, "Attribute must be Symbol or String, got #{attr.inspect}"
118
+ end
119
+ unless record.respond_to?(attr)
120
+ raise "[svelte-on-rails:to_svelte] No method #{attr.inspect} on #{record.class}"
121
+ end
122
+ end
73
123
  end
74
124
  end
75
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.3.3
4
+ version: 9.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair