Class: RailroadDiagrams::Repeat
Overview
Draws a repetition with minimum and optional maximum counts.
Instance Attribute Summary
Attributes inherited from DiagramItem
#attrs, #children, #down, #height, #needs_space, #up, #width
Instance Method Summary
collapse
#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
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
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
31
32
33
|
# File 'lib/railroad_diagrams/repeat.rb', line 31
def child_nodes
@separator ? [@item, @separator] : [@item]
end
|
#count_label(locale) ⇒ 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
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
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
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
|