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].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
- .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 |
# 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
#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
.epsilon(display = Graphomaton::DEFAULT_EPSILON_LABEL) ⇒ Label
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
75 76 77 |
# File 'lib/graphomaton/model.rb', line 75 def self.symbols(*values) new(kind: :symbols, value: values.flatten) end |
.text(value) ⇒ Label
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
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_h ⇒ Hash[Symbol, untyped]
103 104 105 |
# File 'lib/graphomaton/model.rb', line 103 def to_h { type: kind, value: value }.compact end |
#to_s ⇒ 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 |