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.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all_groups, #all_members, #command, #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, #toggle, #up, #update!

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)


105
106
107
# File 'lib/openhab/core/provider.rb', line 105

def registry
  raise NotImplementedError
end

.typeSymbol (readonly)

Returns:



142
143
144
# File 'lib/openhab/core/provider.rb', line 142

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:



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

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)


174
175
176
# File 'lib/openhab/core/provider.rb', line 174

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

#allArray<Object> Also known as: getAll

Get all elements in this provider

Returns:



184
185
186
# File 'lib/openhab/core/provider.rb', line 184

def all
  @elements.values
end

#inspectString

Returns:

  • (String)


153
154
155
# File 'lib/openhab/core/provider.rb', line 153

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