Class: Ecu::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/ecu/labels/label.rb,
lib/ecu/interfaces/dcm/label.rb,
lib/ecu/interfaces/lab/label.rb,
lib/ecu/interfaces/dcm/property_parser.rb

Defined Under Namespace

Classes: InvalidValue

Constant Summary collapse

BYTESIZE =
{ number: 4,
string: 25 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



13
14
15
# File 'lib/ecu/labels/label.rb', line 13

def description
  @description
end

#functionObject (readonly)

Returns the value of attribute function.



13
14
15
# File 'lib/ecu/labels/label.rb', line 13

def function
  @function
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/ecu/labels/label.rb', line 13

def name
  @name
end

Class Method Details

.from_dcm(input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ecu/interfaces/dcm/label.rb', line 5

def self.from_dcm(input)
  lines = case input
          in Array  then input
          in String then input.lines
          end
  hsh = dcm_header.extract_captures(lines.first)

  hsh[:xdim] = hsh[:xdim].to_i if hsh.key?(:xdim)
  hsh[:ydim] = hsh[:ydim].to_i if hsh.key?(:ydim)

  fail "Malformed DCM" if hsh.empty?
  fail "Malformed DCM" unless lines.last.match?(/^END$/)

  lines[1..-2]
    .map { DcmPropertyParser.call(_1) }
    .each { hsh.merge!(_1) { |_, old, new| old + new } }

  new(**hsh)
end

.from_lab(line) ⇒ Object

This is a silly hack, since the *.lab file doesn’t contain any information about the label in question (just the name). Subsequently, just initialize a Festwert with no value and hope if does the job correctly



8
9
10
11
12
13
14
15
# File 'lib/ecu/interfaces/lab/label.rb', line 8

def self.from_lab(line)
  name, description = line.strip.split(";")
  if description.nil?
    Festwert.new(name: name, value: nil)
  else
    Festwert.new(name: name, value: nil, description: description)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



31
32
33
# File 'lib/ecu/labels/label.rb', line 31

def <=>(other)
  name <=> other.name if other.respond_to?(:name)
end

#==(other) ⇒ Object Also known as: eql?



48
49
50
51
52
53
54
55
56
57
# File 'lib/ecu/labels/label.rb', line 48

def ==(other)
  return false unless other.instance_of?(self.class)
  equality_properties.all? do |p|
    if %i(value xvalue yvalue).include?(p)
      ValueComparison.new(self.public_send(p), other.public_send(p)).eql?
    else
      self.public_send(p) == other.public_send(p)
    end
  end
end

#===(other) ⇒ Object



59
60
61
# File 'lib/ecu/labels/label.rb', line 59

def ===(other)
  name == other.name
end

#equality_propertiesObject



23
24
25
# File 'lib/ecu/labels/label.rb', line 23

def equality_properties
  fail NotImplementedError, "Must be implemented in child classes"
end

#hashObject



35
36
37
# File 'lib/ecu/labels/label.rb', line 35

def hash
  equality_properties.map { |p| self.public_send(p) }.hash
end

#init_error(message) ⇒ Object



15
16
17
# File 'lib/ecu/labels/label.rb', line 15

def init_error(message)
  fail ArgumentError, "Error creating #{name}: " + message
end

#match(selector) ⇒ Object



63
64
65
66
67
# File 'lib/ecu/labels/label.rb', line 63

def match(selector)
  %i(name function description).any? do |property|
    self.public_send(property) && self.public_send(property).match(selector)
  end
end

#propertiesObject



27
28
29
# File 'lib/ecu/labels/label.rb', line 27

def properties
  fail NotImplementedError, "Must be implemented in child classes"
end

#to_hObject



39
40
41
# File 'lib/ecu/labels/label.rb', line 39

def to_h
  properties.zip(properties.map { |p| instance_variable_get("@#{p}".to_sym) }).to_h
end

#to_labObject



17
18
19
# File 'lib/ecu/interfaces/lab/label.rb', line 17

def to_lab
  "#{name};#{description}"
end

#typeObject



19
20
21
# File 'lib/ecu/labels/label.rb', line 19

def type
  self.class.name.split('::').last
end

#valuestats(value, show_avg: false) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/ecu/labels/label.rb', line 69

def valuestats(value, show_avg: false)
  value = [value].flatten
  min, avg, max = value.min, value.avg.round(1), value.max
  if show_avg
    "[#{min};~#{avg};#{max}]"
  else
    "[#{min};#{max}]"
  end
end

#with(hash = {}) ⇒ Object



43
44
45
46
# File 'lib/ecu/labels/label.rb', line 43

def with(hash={})
  return self if hash.empty?
  self.class.new(**to_h.merge(hash))
end