Class: Lutaml::Yamls::Adapter::YamlsSequenceRule

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/yamls/adapter/yamls_sequence_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, to:, type:, collection: false) ⇒ YamlsSequenceRule

Returns a new instance of YamlsSequenceRule.



9
10
11
12
13
14
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 9

def initialize(position, to:, type:, collection: false)
  @position = position
  @to = to
  @type = type
  @collection = collection
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 7

def collection
  @collection
end

#positionObject (readonly)

Returns the value of attribute position.



7
8
9
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 7

def position
  @position
end

#toObject (readonly)

Returns the value of attribute to.



7
8
9
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 7

def to
  @to
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 7

def type
  @type
end

Instance Method Details

#assign_value(instance, value) ⇒ Object



38
39
40
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 38

def assign_value(instance, value)
  instance.public_send(:"#{to}=", value)
end

#read_value(instance) ⇒ Object



42
43
44
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 42

def read_value(instance)
  instance.public_send(to)
end

#resolve_range(doc_count) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 16

def resolve_range(doc_count)
  return nil unless doc_count.positive?

  case position
  when Integer
    idx = position.negative? ? position + doc_count : position
    idx..idx
  when Range
    start_idx = position.begin.negative? ? position.begin + doc_count : position.begin
    end_idx = position.end
    end_idx = doc_count - 1 if end_idx.nil?
    end_idx = end_idx + doc_count if end_idx.negative?
    end_idx = doc_count - 1 if end_idx > doc_count - 1
    start_idx = 0 if start_idx.negative?
    start_idx..end_idx
  end
end

#singular?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/lutaml/yamls/adapter/yamls_sequence_rule.rb', line 34

def singular?
  !collection
end