Module: OpenHAB::Core::Types::Type

Included in:
Command, State
Defined in:
lib/openhab/core/types/type.rb

Overview

This is a parent interface for all States and Commands. It was introduced as many states can be commands at the same time and vice versa. E.g. a light can have the state ON or OFF and one can also send ON and OFF as commands to the device. This duality is captured by this marker interface and allows implementing classes to be both state and command at the same time.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ true, false

Check equality, including type conversion

Returns:

  • (true, false)

    if the same value is represented, including type conversions



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/openhab/core/types/type.rb', line 45

def ==(other)
  logger.trace { "(#{self.class}) #{self} == #{other} (#{other.class})" }
  return true if equal?(other)

  # i.e. ON == OFF, REFRESH == ON, ON == REFRESH
  # (RefreshType isn't really coercible)
  return equals(other) if other.instance_of?(self.class) || is_a?(RefreshType) || other.is_a?(RefreshType)

  if other.respond_to?(:coerce)
    begin
      return false unless (lhs, rhs = other.coerce(self))
    rescue TypeError
      # this one is a bit odd. 50 (Integer) == ON is internally
      # flipping the argument and calling this method. but it responds
      # to coerce, and then raises a TypeError (from Float???) that
      # it couldn't convert to OnOffType. it probably should have
      # returned nil. catch it and return false instead
      return false
    end

    return lhs == rhs
  end

  super
end

#eql?(other) ⇒ true, false

Check equality without type conversion

Returns:

  • (true, false)

    if the same value is represented, without type conversion



33
34
35
36
37
# File 'lib/openhab/core/types/type.rb', line 33

def eql?(other)
  return false unless other.instance_of?(self.class)

  equals(other)
end