Class: OpenHAB::Core::Provider Abstract

Inherits:
org.openhab.core.common.registry.AbstractProvider show all
Includes:
Enumerable
Defined in:
lib/openhab/core/provider.rb

Overview

This class is abstract.

Constant Summary collapse

KNOWN_TYPES =

Known supported provider types

Returns:

%i[items metadata things links].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all_groups, #all_members, #command, #decrease, #down, #equipments, #fast_forward, #groups, #increase, #locations, #member_of, #members, #move, #next, #not_member_of, #not_tagged, #off, #on, #pause, #play, #points, #previous, #refresh, #rewind, #stop, #tagged, #up

Class Attribute Details

.registryorg.openhab.core.common.registry.Registry (readonly)

This method is abstract.

The registry that this provider provides elements for.

Returns:

Raises:

  • (NotImplementedError)


103
104
105
# File 'lib/openhab/core/provider.rb', line 103

def registry
  raise NotImplementedError
end

.typeSymbol (readonly)

Returns:



140
141
142
# File 'lib/openhab/core/provider.rb', line 140

def type
  name.split("::")[-2].downcase.to_sym
end

Class Method Details

.current(preferred_provider = nil, element = nil) ⇒ org.openhab.core.common.registry.Provider

Determines the current provider that should be used to create elements belonging to this registry.

Parameters:

  • preferred_provider (org.openhab.core.common.registry.Provider, Proc, :persistent, :transient, nil) (defaults to: nil)

    An optional preferred provider to use. Can be one of several types:

    • An explicit instance of ManagedProvider
    • A Proc, which can calculate the preferred provider based on whatever conditions it wants, and then is further processed as this parameter.
    • :persistent, meaning the default ManagedProvider for this registry. Managed providers persist their objects to JSON, and will survive after the Ruby script is unloaded. This is where objects you configure with MainUI are stored. You should use this provider when you're creating something in response to a one-time event.
    • :transient, meaning a ManagedProvider that will remove all of its contents when the Ruby script is unloaded. You should use this if you're generating objects dynamically, either based on some sort of other configuration, or simply hard coded and you're using Ruby as a more expressive way to define things than a .items or .things file. If you don't use this provider for something such as metadata, then you may have issues such as metadata still showing up even though you're no longer creating items with it anymore.
    • nil, meaning to fall back to the current thread setting. See DSL.provider. If there is no thread setting (or the thread setting was Proc that returned nil), it defaults to :transient.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/openhab/core/provider.rb', line 74

def current(preferred_provider = nil, element = nil)
  preferred_provider ||= Thread.current[:openhab_providers]&.[](type)
  if preferred_provider.is_a?(Proc)
    preferred_provider = if preferred_provider.arity.zero? || element.nil?
                           preferred_provider.call
                         else
                           preferred_provider.call(element)
                         end
  end

  case preferred_provider
  when nil, :transient
    instance
  when :persistent
    registry.managed_provider.get
  when org.openhab.core.common.registry.ManagedProvider
    preferred_provider
  else
    raise ArgumentError, "#{preferred_provider.inspect} is not a ManagedProvider"
  end
end

Instance Method Details

#[](key) ⇒ Object Also known as: get

Get an element from this provider

Parameters:

  • key (Object)

    The proper key type for the elements in this provider.

Returns:

  • (Object)


172
173
174
# File 'lib/openhab/core/provider.rb', line 172

def [](key)
  @elements[key]
end

#allArray<Object> Also known as: getAll

Get all elements in this provider

Returns:



182
183
184
# File 'lib/openhab/core/provider.rb', line 182

def all
  @elements.values
end

#inspectString

Returns:

  • (String)


151
152
153
# File 'lib/openhab/core/provider.rb', line 151

def inspect
  "#<#{self.class.name}:#{object_id}>"
end