Class: Graphomaton::Label
- Inherits:
-
Data
- Object
- Data
- Graphomaton::Label
- Defined in:
- lib/graphomaton/model.rb,
sig/graphomaton.rbs
Constant Summary collapse
- KINDS =
%i[text symbols epsilon uml alternatives].freeze
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .alternatives(*values) ⇒ Label
- .epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL) ⇒ Label
- .symbols(*values) ⇒ Label
- .text(value) ⇒ Label
- .uml(event:, guard: nil, action: nil) ⇒ Label
Instance Method Summary collapse
-
#initialize(kind:, value: nil) ⇒ Label
constructor
A new instance of Label.
- #to_h ⇒ Hash[Symbol, untyped]
- #to_s ⇒ String
Constructor Details
#initialize(kind:, value: nil) ⇒ Label
Returns a new instance of Label.
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 70 71 72 73 74 |
# 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 when :alternatives alternatives = Array(value) raise ArgumentError, 'Alternative label requires at least one label' if alternatives.empty? alternatives end super(kind: resolved_kind, value: ModelValue.copy(normalized_value)) end |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind
39 40 41 |
# File 'lib/graphomaton/model.rb', line 39 def kind @kind end |
#value ⇒ Object (readonly)
Returns the value of attribute value
39 40 41 |
# File 'lib/graphomaton/model.rb', line 39 def value @value end |
Class Method Details
.alternatives(*values) ⇒ Label
92 93 94 |
# File 'lib/graphomaton/model.rb', line 92 def self.alternatives(*values) new(kind: :alternatives, value: values.flatten) end |
.epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL) ⇒ Label
84 85 86 |
# File 'lib/graphomaton/model.rb', line 84 def self.epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL) new(kind: :epsilon, value: display) end |
.symbols(*values) ⇒ Label
80 81 82 |
# File 'lib/graphomaton/model.rb', line 80 def self.symbols(*values) new(kind: :symbols, value: values.flatten) end |
.text(value) ⇒ Label
76 77 78 |
# File 'lib/graphomaton/model.rb', line 76 def self.text(value) new(kind: :text, value: value) end |
.uml(event:, guard: nil, action: nil) ⇒ Label
88 89 90 |
# File 'lib/graphomaton/model.rb', line 88 def self.uml(event:, guard: nil, action: nil) new(kind: :uml, value: { event: event, guard: guard, action: action }.compact) end |
Instance Method Details
#to_h ⇒ Hash[Symbol, untyped]
114 115 116 117 |
# File 'lib/graphomaton/model.rb', line 114 def to_h serialized = kind == :alternatives ? value.map { |label| label.is_a?(Label) ? label.to_h : label } : value { type: kind, value: serialized }.compact end |
#to_s ⇒ String
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/graphomaton/model.rb', line 96 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}" when :alternatives value.join(', ') else value.to_s end end |