Sha256: 4d0a7406b359357e596db0534850cc89423301611a9e394da3f9bc83b2482dee

Contents?: true

Size: 902 Bytes

Versions: 4

Compression:

Stored size: 902 Bytes

Contents

require 'dry/core/deprecations'
require 'dry/core/inflector'

require 'rom/registry'

module ROM
  class AssociationSet < ROM::Registry
    # @api private
    def try(name, &block)
      key = name.to_sym

      if key?(key) || key?(singularize(key))
        yield(self[key])
      else
        msg = <<-STR
          Key inference will be removed in rom 4.0. You need to define :#{key} association.
            => Called at:
               #{caller.join("\n")}
          STR

        Dry::Core::Deprecations.warn(msg)

        false
      end
    end

    # @api private
    def [](name)
      key = name.to_sym

      if key?(key)
        super
      else
        sk = singularize(key)

        if key?(sk)
          super(sk)
        else
          super
        end
      end
    end

    # @api private
    def singularize(key)
      Dry::Core::Inflector.singularize(key).to_sym
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-3.3.3 lib/rom/association_set.rb
rom-3.3.2 lib/rom/association_set.rb
rom-3.3.1 lib/rom/association_set.rb
rom-3.3.0 lib/rom/association_set.rb