Sha256: 31e368a891a9cc3ae6f4f989e1e8a0be6f84b9094fe9ba605d3cacbc2deab340

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class Shaped::Shapes::Class < Shaped::Shape
  def initialize(expected_klass, validations = {})
    if !expected_klass.is_a?(Class)
      raise(Shaped::InvalidShapeDescription, "A #{self.class} description must be a Class.")
    end

    @expected_klass = expected_klass
    @validations = validations
    @validator_klass = validator_klass(validations)
  end

  def matched_by?(object)
    object.is_a?(@expected_klass) && validations_satisfied?(object)
  end

  def to_s
    if @validations.empty?
      @expected_klass.name
    else
      "#{@expected_klass} validating #{@validations}"
    end
  end

  private

  def validator_klass(validations)
    return nil if validations.empty?

    Class.new do
      include ActiveModel::Validations

      attr_accessor :value

      validates :value, validations

      class << self
        # ActiveModel requires the class to have a `name`
        def name
          'Shaped::Shapes::Class::AnonymousValidator'
        end
      end
    end
  end

  def validations_satisfied?(object)
    return true if @validator_klass.nil?

    validator_instance = @validator_klass.new
    validator_instance.value = object
    validator_instance.valid?
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shaped-0.7.2 lib/shaped/shapes/class.rb
shaped-0.7.1 lib/shaped/shapes/class.rb
shaped-0.7.0 lib/shaped/shapes/class.rb
shaped-0.6.4 lib/shaped/shapes/class.rb
shaped-0.6.3 lib/shaped/shapes/class.rb
shaped-0.6.2 lib/shaped/shapes/class.rb
shaped-0.6.1 lib/shaped/shapes/class.rb