Class: SvelteOnRails::Lib::ToSvelteValues
- Inherits:
-
Object
- Object
- SvelteOnRails::Lib::ToSvelteValues
- Defined in:
- lib/svelte_on_rails/lib/to_svelte_values.rb
Class Method Summary collapse
- .active_record_collection?(obj) ⇒ Boolean
- .apply_pagination(coll, associations) ⇒ Object
- .collection_like?(obj) ⇒ Boolean
- .extract_single_record(record, attributes, associations, next_stack) ⇒ Object
-
.extract_values(record, attributes = [], associations = {}) ⇒ Object
Public API – returns Hash for single record, Array<Hash> for collections.
- .extract_values_recursive(thing, attributes, associations, call_stack:) ⇒ Object
- .looks_like_collection?(obj) ⇒ Boolean
-
.looks_like_single?(obj) ⇒ Boolean
Helpers ────────────────────────────────────────────────.
- .normalize_to_enumerable(record, associations) ⇒ Object
- .validate_attribute!(record, attr) ⇒ Object
Class Method Details
.active_record_collection?(obj) ⇒ Boolean
117 118 119 120 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 117 def self.active_record_collection?(obj) (obj.is_a?(ActiveRecord::Relation) || obj.is_a?(ActiveRecord::Associations::CollectionProxy)) rescue false end |
.apply_pagination(coll, associations) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 25 def self.apply_pagination(coll, associations) if associations.key?(:offset) || associations.key?(:limit) if active_record_collection?(coll) scope = coll scope = scope.offset(associations[:offset]) if associations.key?(:offset) scope = scope.limit(associations[:limit]) if associations.key?(:limit) scope.to_a elsif coll.respond_to?(:drop) && coll.respond_to?(:take) res = coll res = res.drop(associations[:offset]) if associations.key?(:offset) res = res.take(associations[:limit]) if associations.key?(:limit) res else raise "[SOR] to_svelte Cannot paginate #{coll.class}" end else coll.respond_to?(:to_a) ? coll.to_a : coll end end |
.collection_like?(obj) ⇒ Boolean
113 114 115 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 113 def self.collection_like?(obj) looks_like_collection?(obj) || obj.is_a?(Array) end |
.extract_single_record(record, attributes, associations, next_stack) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 57 def self.extract_single_record(record, attributes, associations, next_stack) return {} unless record # or raise – depending on your preference values = {} enums = record.class.try(:defined_enums) || {} attributes.each do |attr| validate_attribute!(record, attr) key_s = attr.to_s value = record.send(attr) values[key_s] = enums.key?(key_s) ? record.send(key_s) : value end associations.each do |key, val| key_s = key.to_s next if %w[offset limit].include?(key_s) reflection = record.class.reflect_on_association(key_s) raise ArgumentError, "No association #{key_s.inspect} on #{record.class}" unless reflection = record.send(key_s) if .respond_to?(:each) && .empty? values[key_s] = [] elsif ! values[key_s] = nil else sub_attrs, sub_assocs = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(val) values[key_s] = extract_values_recursive( , sub_attrs, sub_assocs, call_stack: next_stack ) end end values end |
.extract_values(record, attributes = [], associations = {}) ⇒ Object
Public API – returns Hash for single record, Array<Hash> for collections
5 6 7 8 9 10 11 12 13 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 5 def self.extract_values(record, attributes = [], associations = {}) records = normalize_to_enumerable(record, associations) result = extract_values_recursive(records, attributes, associations, call_stack: 0) # Mirror input shape for the top-level return value looks_like_single?(record) ? (result.first || {}) : result end |
.extract_values_recursive(thing, attributes, associations, call_stack:) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 45 def self.extract_values_recursive(thing, attributes, associations, call_stack:) next_stack = call_stack + 1 if collection_like?(thing) thing.map do |record| extract_single_record(record, attributes, associations, next_stack) end else extract_single_record(thing, attributes, associations, next_stack) end end |
.looks_like_collection?(obj) ⇒ Boolean
108 109 110 111 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 108 def self.looks_like_collection?(obj) return false if obj.nil? || obj.is_a?(Hash) || obj.is_a?(String) obj.respond_to?(:each) end |
.looks_like_single?(obj) ⇒ Boolean
Helpers ────────────────────────────────────────────────
104 105 106 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 104 def self.looks_like_single?(obj) !looks_like_collection?(obj) end |
.normalize_to_enumerable(record, associations) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 17 def self.normalize_to_enumerable(record, associations) if looks_like_collection?(record) apply_pagination(record, associations) else [record] # single → wrap → recursive code can be uniform end end |
.validate_attribute!(record, attr) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 122 def self.validate_attribute!(record, attr) unless attr.is_a?(Symbol) || attr.is_a?(String) raise ArgumentError, "Attribute must be Symbol or String, got #{attr.inspect}" end unless record.respond_to?(attr) raise "[SOR] to_svelte: No method #{attr.inspect} on #{record.class}" end end |