Sha256: 38ef49e9c9b4f15cfe75ef3b8222d0ddd13d1fe20339890b41affb5c37346f6e

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Orthoses
  module ActiveRecord
    class HasOne
      def initialize(loader)
        @loader = loader
      end

      def call
        @loader.call.tap do |store|
          ::ActiveRecord::Base.descendants.each do |base|
            next if base.abstract_class?
            base_name = Utils.module_name(base) || next

            lines = base.reflect_on_all_associations(:has_one).flat_map do |ref|
              type = ref.klass.to_s
              opt = "#{type}?"

              [
                "def #{ref.name}: () -> #{opt}",
                "def #{ref.name}=: (#{opt}) -> #{opt}",
                "def build_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
                "def create_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
                "def create_#{ref.name}!: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
                "def reload_#{ref.name}: () -> #{opt}",
              ]
            end

            generated_association_methods = "#{base_name}::GeneratedAssociationMethods"
            store[generated_association_methods].header = "module #{generated_association_methods}"
            store[generated_association_methods].concat(lines)

            store[base_name] << "include #{generated_association_methods}"
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
orthoses-rails-0.9.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.8.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.7.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.6.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.5.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.4.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.3.0 lib/orthoses/active_record/has_one.rb
orthoses-rails-0.2.0 lib/orthoses/active_record/has_one.rb