class Lutaml::Model::Choice
def ==(other)
def ==(other) @attributes == other.attributes && @min == other.min && @max == other.max && @model == other.model end
def attribute(name, type, options = {})
def attribute(name, type, options = {}) options[:choice] = self @attributes << @model.attribute(name, type, options) end
def choice(min: 1, max: 1, &block)
def choice(min: 1, max: 1, &block) @attributes << Choice.new(@model, min, max).tap do |c| c.instance_eval(&block) end end
def initialize(model, min, max)
def initialize(model, min, max) @attributes = [] @model = model @min = min @max = max raise Lutaml::Model::InvalidChoiceRangeError.new(@min, @max) if @min.negative? || @max.negative? end
def valid_attributes(object, validated_attributes)
def valid_attributes(object, validated_attributes) @attributes.each do |attribute| if attribute.is_a?(Choice) begin attribute.validate_content!(object) validated_attributes << attribute rescue Lutaml::Model::ChoiceLowerBoundError end elsif Utils.present?(object.public_send(attribute.name)) validated_attributes << attribute.name end end validated_attributes end
def validate_content!(object)
def validate_content!(object) validated_attributes = [] valid = valid_attributes(object, validated_attributes) raise Lutaml::Model::ChoiceUpperBoundError.new(validated_attributes, @max) if valid.count > @max raise Lutaml::Model::ChoiceLowerBoundError.new(validated_attributes, @min) if valid.count < @min end