Class: Graphomaton::Label

Inherits:
Data
  • Object
show all
Defined in:
lib/graphomaton/model.rb,
sig/graphomaton.rbs

Constant Summary collapse

KINDS =
%i[text symbols epsilon uml].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, value: nil) ⇒ Label

Returns a new instance of Label.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphomaton/model.rb', line 42

def initialize(kind:, value: nil)
  resolved_kind = kind.to_sym
  raise ArgumentError, "Unknown label kind: #{kind.inspect}" unless KINDS.include?(resolved_kind)

  normalized_value = case resolved_kind
                     when :text
                       value.to_s
                     when :symbols
                       symbols = Array(value).map(&:to_s)
                       raise ArgumentError, 'Symbol label requires at least one symbol' if symbols.empty?

                       symbols
                     when :epsilon
                       value.nil? ? Graphomaton::DEFAULT_EPSILON_LABEL : value.to_s
                     when :uml
                       raise ArgumentError, 'UML label value must be a Hash' unless value.is_a?(Hash)

                       uml_value = {
                         event: value.key?(:event) ? value[:event] : value['event'],
                         guard: value.key?(:guard) ? value[:guard] : value['guard'],
                         action: value.key?(:action) ? value[:action] : value['action']
                       }.compact
                       raise ArgumentError, 'UML label requires an event' unless uml_value.key?(:event)

                       uml_value
                     end
  super(kind: resolved_kind, value: ModelValue.copy(normalized_value))
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind

Returns:

  • (Object)

    the current value of kind



39
40
41
# File 'lib/graphomaton/model.rb', line 39

def kind
  @kind
end

#valueObject (readonly)

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



39
40
41
# File 'lib/graphomaton/model.rb', line 39

def value
  @value
end

Class Method Details

.epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL) ⇒ Label

Parameters:

  • display (Object) (defaults to: Graphomaton::DEFAULT_EPSILON_LABEL)

Returns:



79
80
81
# File 'lib/graphomaton/model.rb', line 79

def self.epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL)
  new(kind: :epsilon, value: display)
end

.symbols(*values) ⇒ Label

Parameters:

  • values (Object)

Returns:



75
76
77
# File 'lib/graphomaton/model.rb', line 75

def self.symbols(*values)
  new(kind: :symbols, value: values.flatten)
end

.text(value) ⇒ Label

Parameters:

  • value (Object)

Returns:



71
72
73
# File 'lib/graphomaton/model.rb', line 71

def self.text(value)
  new(kind: :text, value: value)
end

.uml(event:, guard: nil, action: nil) ⇒ Label

Parameters:

  • event: (Object)
  • guard: (Object) (defaults to: nil)
  • action: (Object) (defaults to: nil)

Returns:



83
84
85
# File 'lib/graphomaton/model.rb', line 83

def self.uml(event:, guard: nil, action: nil)
  new(kind: :uml, value: { event: event, guard: guard, action: action }.compact)
end

Instance Method Details

#to_hHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


103
104
105
# File 'lib/graphomaton/model.rb', line 103

def to_h
  { type: kind, value: value }.compact
end

#to_sString

Returns:

  • (String)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/graphomaton/model.rb', line 87

def to_s
  case kind
  when :symbols
    value.join(', ')
  when :epsilon
    value
  when :uml
    event = value.fetch(:event).to_s
    guard = value[:guard] ? " [#{value[:guard]}]" : ''
    action = value[:action] ? " / #{value[:action]}" : ''
    "#{event}#{guard}#{action}"
  else
    value.to_s
  end
end