Class: Orthoses::ActiveRecord::Persistence
- Inherits:
-
Object
- Object
- Orthoses::ActiveRecord::Persistence
- Defined in:
- lib/orthoses/active_record/persistence.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(loader) ⇒ Persistence
constructor
A new instance of Persistence.
Constructor Details
#initialize(loader) ⇒ Persistence
Returns a new instance of Persistence.
6 7 8 |
# File 'lib/orthoses/active_record/persistence.rb', line 6 def initialize(loader) @loader = loader end |
Instance Method Details
#call ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/orthoses/active_record/persistence.rb', line 10 def call @loader.call.tap do |store| ::ActiveRecord::Base.descendants.each do |klass| next if klass.abstract_class? next unless klass.table_exists? base_name = Utils.module_name(klass) or next attributes = klass.columns_hash.map do |key, col| req = ActiveRecord.sql_type_to_rbs(col.type) opt = "#{req}?" [key.to_s, col.null ? opt : req] end.to_h klass.attribute_aliases.each do |alias_name, column_name| alias_value = attributes[column_name.to_s] or next attributes[alias_name.to_s] = alias_value end optional_definitions = attributes.map do |name, type| "?#{name}: #{type}" end.join(", ") class_methods_name = "#{base_name}::ActiveRecord_Persistence_ClassMethods" store[base_name] << "extend #{class_methods_name}" store[class_methods_name].header = "module #{class_methods_name}" %i[create create! build].each do |method| method = <<~RBS def #{method}: (#{optional_definitions}, **untyped) ?{ (#{base_name}) -> void } -> #{base_name} | (::Array[Hash[Symbol, untyped]]) ?{ (#{base_name}) -> void } -> ::Array[#{base_name}] RBS store[class_methods_name] << method end end end end |