Sha256: 306387753be923f8577cc7c2f2c0a854a7587730a77cc9164de6fa553eb8590b

Contents?: true

Size: 809 Bytes

Versions: 1

Compression:

Stored size: 809 Bytes

Contents

# frozen_string_literal: true

module Pragma
  module Contract
    module ModelFinder
      include Adaptor::Loader
      register ActiveRecord

      FINDER_OPTIONS = [:by].freeze

      def self.included(klass)
        klass.extend ClassMethods
      end

      module ClassMethods
        def property(name, options = {})
          return super if !options[:type] || options[:type].is_a?(Dry::Types::Type)

          property(name, Hash[options.reject { |k, _| k == :type }])

          define_method("#{name}=") do |value|
            finder = Pragma::Contract::ModelFinder.load_adaptor!(
              options[:type],
              Hash[options.select { |k, _| FINDER_OPTIONS.include?(k.to_sym) }]
            )

            super finder.find(value)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pragma-contract-2.1.1 lib/pragma/contract/model_finder.rb