Skip to main content Link Search Menu Expand Document (external link)

ContactItem

MethodDescriptionExample
open?Returns true if item state == OPENputs "#{item} is closed." if item.open?
closed?Returns true if item state == CLOSEDputs "#{item} is off." if item.closed
Examples

open?/closed? checks state of contact

# Log open contacts
Contacts.select(&:open?).each { |contact| logger.info("Contact #{contact.id} is open")}

# Log closed contacts
Contacts.select(&:closed?).each { |contact| logger.info("Contact #{contact.id} is closed")}

Contacts can be selected in an enumerable with grep.

# Get all Contacts
items.grep(ContactItem)
     .each { |contact| logger.info("#{contact.id} is a Contact") }

Contacts states work in grep.

# Log all open contacts in a group
Contacts.grep(OPEN)
        .each { |contact| logger.info("#{contact.id} is in #{contact}") }

# Log all closed contacts in a group
Contacts.grep(CLOSED)
        .each { |contact| logger.info("#{contact.id} is in #{contact}") }

Contact states work in case statements.

#Log if contact is open or closed
case TestContact
when (OPEN)
  logger.info("#{TestContact.id} is open")
when (CLOSED)
  logger.info("#{TestContact.id} is closed")
end

Other examples

rule 'Log state of all doors on system startup' do
  on_start
  run do
    Doors.each do |door|
      case door
      when OPEN then logger.info("#{door.id} is Open")
      when CLOSED then logger.info("#{door.id} is Open")
      else logger.info("#{door.id} is not initialized")
      end
    end
  end
end