Sha256: 4059438a76c31d8549ded5a27faa0895cce02bd4e0d65d228f856f659ef1a495

Contents?: true

Size: 1.55 KB

Versions: 19

Compression:

Stored size: 1.55 KB

Contents

module Dry
  module Mutations
    module Predicates # :nodoc:
      include ::Dry::Logic::Predicates

      RAILS_4_RELATION = 'ActiveRecord_Associations_CollectionProxy'.freeze

      predicate(:relation?) do |expected, current|
        if expected.const_defined?(RAILS_4_RELATION)
          current.is_a?(expected.const_get(RAILS_4_RELATION))
        else
          # Gracefull fallback for Rails3
          current.is_a?(ActiveRecord::Relation) && current.name == expected.name
        end
      end

      predicate(:duck?) do |expected, current|
        expected.empty? || expected.all?(&current.method(:respond_to?))
      end

      predicate(:default?) do |_expected, _current|
        true
        # fail Errors::TypeError, "“default” guard is not implemented yet in dry-mutations, sorry for that."
      end

      # FIXME: at the moment this is an exact equivalent of :type? => User
      predicate(:model?) do |expected, current|
        return true if expected.nil?
        expected = begin
                     ::Kernel.const_get(expected)
                   rescue TypeError => e
                     raise Errors::TypeError, "Bad “model” type. Error: [#{e.message}]"
                   rescue NameError => e
                     raise Errors::TypeError, "Bad “model” class. Error: [#{e.message}]"
                   end unless expected.is_a? Module
        current.is_a?(expected)
      end

      predicate(:discard_empty) do |_expected, _current|
        true
      end

      predicate(:class) do |_expected, _current|
        true
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dry-mutations-1.6.0 lib/dry/mutations/predicates.rb
dry-mutations-1.5.7 lib/dry/mutations/predicates.rb
dry-mutations-1.5.6 lib/dry/mutations/predicates.rb
dry-mutations-1.5.5 lib/dry/mutations/predicates.rb
dry-mutations-1.5.4 lib/dry/mutations/predicates.rb
dry-mutations-1.5.3 lib/dry/mutations/predicates.rb
dry-mutations-1.5.2 lib/dry/mutations/predicates.rb
dry-mutations-1.5.1 lib/dry/mutations/predicates.rb
dry-mutations-1.5.0 lib/dry/mutations/predicates.rb
dry-mutations-1.3.6 lib/dry/mutations/predicates.rb
dry-mutations-1.3.5 lib/dry/mutations/predicates.rb
dry-mutations-1.3.4 lib/dry/mutations/predicates.rb
dry-mutations-1.3.3 lib/dry/mutations/predicates.rb
dry-mutations-1.3.2 lib/dry/mutations/predicates.rb
dry-mutations-1.3.1 lib/dry/mutations/predicates.rb
dry-mutations-1.1.3 lib/dry/mutations/predicates.rb
dry-mutations-1.1.1 lib/dry/mutations/predicates.rb
dry-mutations-1.1.0 lib/dry/mutations/predicates.rb
dry-mutations-1.0.0 lib/dry/mutations/predicates.rb