Class: RailroadDiagrams::Repeat

Inherits:
DiagramItem show all
Includes:
ExpandedNode
Defined in:
lib/railroad_diagrams/repeat.rb,
sig/generated/railroad_diagrams/repeat.rbs

Overview

Draws a repetition with minimum and optional maximum counts.

Examples:

Repeat.new('digit', min: 2, max: 4)

Instance Attribute Summary

Attributes inherited from DiagramItem

#attrs, #children, #down, #height, #needs_space, #up, #width

Instance Method Summary collapse

Methods included from ExpandedNode

#adopt_expansion, #format, #measure, #render_svg, #render_text, #text_diagram, #walk

Methods inherited from DiagramItem

#==, #add, #apply_user_attributes, #container_element?, #determine_gaps, #each_node, #format, #hash, inherited, #inspect, #structural_state, #text_diagram, #to_str, #walk, #wrap_string, #write_children, #write_opening_tag, #write_standalone, #write_svg

Constructor Details

#initialize(item, min: 1, max: nil, separator: nil, label: :auto, id: nil, cls: nil, attrs: {}) ⇒ Object

rubocop:disable-next Metrics/ParameterLists

Parameters:

  • item (Object)
  • min: (Object) (defaults to: 1)
  • max: (Object) (defaults to: nil)
  • separator: (Object) (defaults to: nil)
  • label: (Object) (defaults to: :auto)
  • id: (Object) (defaults to: nil)
  • cls: (Object) (defaults to: nil)
  • attrs: (Object) (defaults to: {})


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/railroad_diagrams/repeat.rb', line 12

def initialize(item, min: 1, max: nil, separator: nil, label: :auto, id: nil, cls: nil, attrs: {})
  super('g', id: id, cls: cls, data_attrs: attrs)
  raise InvalidArgument, 'min must be a non-negative integer' unless min.is_a?(Integer) && min >= 0
  raise InvalidArgument, 'max must be an integer greater than or equal to min' unless max.nil? || (max.is_a?(Integer) && max >= min)
  raise InvalidArgument, 'label must be :auto, nil, or a String' unless label == :auto || label.nil? || label.is_a?(String)

  @item = wrap_string(item)
  @separator = separator.nil? ? nil : wrap_string(separator)
  @min = min
  @max = max
  @label = label
  adopt_expansion(build_repeat(:en))
  @japanese = build_repeat(:ja) if label == :auto && count_label(:en)
end

Instance Method Details

#build_repeat(locale) ⇒ Object

Parameters:

  • locale (Object)

Returns:

  • (Object)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/railroad_diagrams/repeat.rb', line 48

def build_repeat(locale)
  return Skip.new if @max&.zero?
  return @min.zero? ? Optional.new(@item) : @item if @max == 1

  label = @label == :auto ? count_label(locale) : @label
  repeat = if label
             Sequence.new(Comment.new(label), @separator || Skip.new)
           else
             @separator
           end
  node = OneOrMore.new(@item, repeat)
  @min.zero? ? Optional.new(node) : node
end

#child_nodes ⇒ Object

Returns:

  • (Object)


31
32
33
# File 'lib/railroad_diagrams/repeat.rb', line 31

def child_nodes
  @separator ? [@item, @separator] : [@item]
end

#count_label(locale) ⇒ Object

Parameters:

  • locale (Object)

Returns:

  • (Object)


62
63
64
65
66
67
68
69
70
# File 'lib/railroad_diagrams/repeat.rb', line 62

def count_label(locale)
  if @max && @min == @max
    I18n.t(:repeat_exact, locale: locale, count: @min)
  elsif @max
    I18n.t(:repeat_range, locale: locale, min: @min, max: @max)
  elsif @min > 1
    I18n.t(:repeat_min, locale: locale, min: @min)
  end
end

#expanded(context = nil) ⇒ Object

Parameters:

  • context (Object) (defaults to: nil)

Returns:

  • (Object)


44
45
46
# File 'lib/railroad_diagrams/repeat.rb', line 44

def expanded(context = nil)
  context && context.options.locale == :ja && @japanese ? @japanese : @expanded
end

#to_h ⇒ Object

Returns:

  • (Object)


35
36
37
38
39
40
# File 'lib/railroad_diagrams/repeat.rb', line 35

def to_h
  Serialization.add_attributes(
    { 'type' => 'repeat', 'item' => @item.to_h, 'min' => @min, 'max' => @max,
      'separator' => @separator&.to_h, 'label' => @label == :auto ? { 'auto' => true } : @label }, self
  )
end

#to_s ⇒ Object

Returns:

  • (Object)


27
28
29
# File 'lib/railroad_diagrams/repeat.rb', line 27

def to_s
  "Repeat(#{@item}, min=#{@min}, max=#{@max}, separator=#{@separator}, label=#{@label.inspect})"
end