Class: RailroadDiagrams::Except

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

Overview

Draws an item with an exclusion label.

Examples:

Except.new(CharClass.new('[a-z]'), 'x')

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, excluded, label: :auto, id: nil, cls: nil, attrs: {}) ⇒ Except

Returns a new instance of Except.

Parameters:

  • item (Object)
  • excluded (Object)
  • label: (Object) (defaults to: :auto)
  • id: (Object) (defaults to: nil)
  • cls: (Object) (defaults to: nil)
  • attrs: (Object) (defaults to: {})

Raises:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/railroad_diagrams/except.rb', line 11

def initialize(item, excluded, label: :auto, id: nil, cls: nil, attrs: {})
  super('g', id: id, cls: cls, data_attrs: attrs)
  raise InvalidArgument, 'excluded must be a String or diagram node' unless excluded.is_a?(String) || excluded.is_a?(DiagramItem)
  raise InvalidArgument, 'label must be :auto, nil, or a String' unless label == :auto || label.nil? || label.is_a?(String)

  @item = wrap_string(item)
  @excluded = excluded
  @label = label
  adopt_expansion(build_group(:en))
  @japanese = build_group(:ja) if label == :auto
end

Instance Method Details

#build_group(locale) ⇒ Object

Parameters:

  • locale (Object)

Returns:

  • (Object)


45
46
47
48
49
50
51
52
53
# File 'lib/railroad_diagrams/except.rb', line 45

def build_group(locale)
  label = if @label == :auto
            value = @excluded.is_a?(DiagramItem) ? A11y::Describer.call(@excluded, locale: locale) : @excluded
            locale == :ja ? "#{value}を除く" : "except #{value}"
          else
            @label
          end
  Group.new(@item, label: label)
end

#child_nodes ⇒ Object

Returns:

  • (Object)


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

def child_nodes
  [@item, *(@excluded.is_a?(DiagramItem) ? [@excluded] : [])]
end

#expanded(context = nil) ⇒ Object

Parameters:

  • context (Object) (defaults to: nil)

Returns:

  • (Object)


41
42
43
# File 'lib/railroad_diagrams/except.rb', line 41

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

#to_h ⇒ Object

Returns:

  • (Object)


31
32
33
34
35
36
37
# File 'lib/railroad_diagrams/except.rb', line 31

def to_h
  excluded = @excluded.is_a?(DiagramItem) ? @excluded.to_h : @excluded
  Serialization.add_attributes(
    { 'type' => 'except', 'item' => @item.to_h, 'excluded' => excluded,
      'label' => @label == :auto ? { 'auto' => true } : @label }, self
  )
end

#to_s ⇒ Object

Returns:

  • (Object)


23
24
25
# File 'lib/railroad_diagrams/except.rb', line 23

def to_s
  "Except(#{@item}, #{@excluded}, label=#{@label.inspect})"
end