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)


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

def registry
  raise NotImplementedError
end

.typeSymbol (readonly)

Returns:



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

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:



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

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)


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

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

#allArray<Object> Also known as: getAll

Get all elements in this provider

Returns:



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

def all
  @elements.values
end

#inspectString

Returns:

  • (String)


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

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