require 'dry/logic'
require 'dry/logic/predicates'

include Dry::Logic

class Preview
  attr_reader :size
  def initialize(size)
    @size = size
  end
end

class Record
  def initialize(new_:)
    @new_record = new_
  end
  def new_record?
    @new_record
  end
end


preview_present = Rule::Predicate.new(
  Predicates[:key?]).curry(:preview)

new_record = Operations::Key.new(
  Rule::Predicate.new(Predicates[:true?]), name: :new_record)

has_valid_size = Operations::Key.new(
  Rule::Predicate.new(Predicates[:lt?]).curry(5*1024), name: :preview_size)

rule = ((new_record & preview_present) | preview_present) > has_valid_size 


rec1 = Record.new(new_: true)
preview1 = Preview.new(3*1024)
puts rule.(preview: preview1, preview_size: preview1.size, new_record: rec1.new_record?).success?
# => true

rec2 = Record.new(new_: false)
# preview2 = Preview.new(3*1024)
puts rule.(new_record: rec2.new_record?).success?
# => true

rec3 = Record.new(new_: true)
preview3 = Preview.new(6*1024)
puts rule.(preview: preview3, preview_size: preview3.size, new_record: rec3.new_record?).success?
# => false

rec4 = Record.new(new_: false)
preview4 = Preview.new(6*1024)
puts rule.(preview: preview4, preview_size: preview4.size, new_record: rec4.new_record?).success?
# => false

Изменить пасту