Class: RailroadDiagrams::Block

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

Overview

A blank, fixed-size block in a diagram.

Examples:

Block.new(width: 40, height: 20)

Instance Attribute Summary

Attributes inherited from DiagramItem

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

Instance Method Summary collapse

Methods inherited from DiagramItem

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

Constructor Details

#initialize(width: 50, up: 15, height: 25, down: 15, needs_space: true, id: nil, cls: nil, attrs: {}) ⇒ Object

rubocop:disable-next Metrics/ParameterLists

Parameters:

  • width: (Object) (defaults to: 50)
  • up: (Object) (defaults to: 15)
  • height: (Object) (defaults to: 25)
  • down: (Object) (defaults to: 15)
  • needs_space: (Object) (defaults to: true)
  • id: (Object) (defaults to: nil)
  • cls: (Object) (defaults to: nil)
  • attrs: (Object) (defaults to: {})


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

def initialize(width: 50, up: 15, height: 25, down: 15, needs_space: true, id: nil, cls: nil, attrs: {})
  super('g', id: id, cls: cls, data_attrs: attrs)
  { width: width, up: up, height: height, down: down }.each do |name, value|
    unless (value.is_a?(Integer) || value.is_a?(Float)) && value.finite? && value >= 0
      raise InvalidArgument, "#{name} must be a finite non-negative number"
    end
  end
  raise InvalidArgument, 'needs_space must be a boolean' unless [true, false].include?(needs_space)

  @width = width
  @up = up
  @height = height
  @down = down
  @needs_space = needs_space
end

Instance Method Details

#child_nodes ⇒ Object

Returns:

  • (Object)


61
62
63
# File 'lib/railroad_diagrams/block.rb', line 61

def child_nodes
  []
end

#format(x, y, width) ⇒ Object

Parameters:

  • x (Object)
  • y (Object)
  • width (Object)

Returns:

  • (Object)


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

def format(x, y, width)
  left_gap, right_gap = determine_gaps(width, @width)
  Path.new(x, y).h(left_gap).add(self)
  x += left_gap
  DiagramItem.new('rect', attrs: rectangle(x, y, @width)).add(self)
  Path.new(x + @width, y + @height).h(right_gap).add(self)
  self
end

#measure(_context) ⇒ Object

Parameters:

  • _context (Object)

Returns:

  • (Object)


43
44
45
# File 'lib/railroad_diagrams/block.rb', line 43

def measure(_context)
  Metrics.new(width: @width, up: @up, height: @height, down: @down, needs_space: @needs_space)
end

#rectangle(x, y, width) ⇒ Object

Parameters:

  • x (Object)
  • y (Object)
  • width (Object)

Returns:

  • (Object)


74
75
76
# File 'lib/railroad_diagrams/block.rb', line 74

def rectangle(x, y, width)
  { 'x' => x, 'y' => y - @up, 'width' => width, 'height' => @up + @height + @down }
end

#render_svg(context, x, y, width) ⇒ Object

Parameters:

  • context (Object)
  • x (Object)
  • y (Object)
  • width (Object)

Returns:

  • (Object)


47
48
49
50
51
52
53
54
55
# File 'lib/railroad_diagrams/block.rb', line 47

def render_svg(context, x, y, width)
  left_gap, right_gap = context.gaps(width, @width)
  group = Svg::Element.new('g', @attrs.dup)
  group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x, y).h(left_gap) }, self_closing: true)
  x += left_gap
  group << Svg::Element.new('rect', rectangle(x, y, @width))
  group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x + @width, y + @height).h(right_gap) }, self_closing: true)
  group
end

#render_text(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


57
58
59
# File 'lib/railroad_diagrams/block.rb', line 57

def render_text(context)
  TextDiagram.rect('', parts: context.parts)
end

#text_diagram ⇒ Object

Returns:

  • (Object)


39
40
41
# File 'lib/railroad_diagrams/block.rb', line 39

def text_diagram
  TextDiagram.rect('')
end

#to_h ⇒ Object

Returns:

  • (Object)


65
66
67
68
69
70
# File 'lib/railroad_diagrams/block.rb', line 65

def to_h
  Serialization.add_attributes(
    { 'type' => 'block', 'width' => @width, 'up' => @up, 'height' => @height,
      'down' => @down, 'needs_space' => @needs_space }, self
  )
end

#to_s ⇒ Object

Returns:

  • (Object)


26
27
28
# File 'lib/railroad_diagrams/block.rb', line 26

def to_s
  "Block(width=#{@width}, up=#{@up}, height=#{@height}, down=#{@down}, needs_space=#{@needs_space})"
end