Class: Lutaml::Model::Choice

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/choice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, min, max) ⇒ Choice

Returns a new instance of Choice.



9
10
11
12
13
14
15
16
# File 'lib/lutaml/model/choice.rb', line 9

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/lutaml/model/choice.rb', line 4

def attributes
  @attributes
end

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/lutaml/model/choice.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/lutaml/model/choice.rb', line 4

def min
  @min
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/lutaml/model/choice.rb', line 4

def model
  @model
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
21
22
23
# File 'lib/lutaml/model/choice.rb', line 18

def ==(other)
  @attributes == other.attributes &&
    @min == other.min &&
    @max == other.max &&
    @model == other.model
end

#attribute(name, type, options = {}) ⇒ Object



25
26
27
28
# File 'lib/lutaml/model/choice.rb', line 25

def attribute(name, type, options = {})
  options[:choice] = self
  @attributes << @model.attribute(name, type, options)
end

#choice(min: 1, max: 1, &block) ⇒ Object



30
31
32
33
34
# File 'lib/lutaml/model/choice.rb', line 30

def choice(min: 1, max: 1, &block)
  @attributes << Choice.new(@model, min, max).tap do |c|
    c.instance_eval(&block)
  end
end

#import_model_attributes(model) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lutaml/model/choice.rb', line 44

def import_model_attributes(model)
  return import_model_later(model, :import_model_attributes) if later_importable?(model)

  root_model_error(model)
  imported_attributes = Utils.deep_dup(model.attributes.values)
  imported_attributes.each do |attr|
    attr.options[:choice] = self
    @model.define_attribute_methods(attr)
  end
  @attributes.concat(imported_attributes)
  attrs_hash = imported_attributes.to_h { |attr| [attr.name, attr] }
  @model.attributes.merge!(attrs_hash)
end

#validate_content!(object) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/lutaml/model/choice.rb', line 36

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