Class: OpenHAB::Core::Rules::Registry

Inherits:
Object
  • Object
show all
Includes:
LazyArray
Defined in:
lib/openhab/core/rules/registry.rb

Overview

Provides access to all openHAB rules, and acts like an array.

Instance Method Summary collapse

Methods included from LazyArray

#each, #method_missing, #to_ary

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, #update!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::LazyArray

Instance Method Details

#[](uid) ⇒ Rule? Also known as: include?, key?, has_key?

Gets a specific Rule

Parameters:

  • uid (String)

    Rule UID

Returns:



21
22
23
# File 'lib/openhab/core/rules/registry.rb', line 21

def [](uid)
  $rules.get(uid)
end

#build(preferred_provider = nil) { ... } ⇒ Object

Enter the Rule Builder DSL.

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.

Yields:

Returns:

  • (Object)

    The result of the block.



44
45
46
# File 'lib/openhab/core/rules/registry.rb', line 44

def build(preferred_provider = nil, &block)
  DSL::Rules::Builder.new(preferred_provider).instance_eval_with_dummy_items(&block)
end

#remove(rule_uid) ⇒ Rule?

Remove a Rule.

The rule must be a managed thing (typically created by Ruby or in the UI).

Examples:

my_rule = rule do
  every :day
  run { nil }
end

rules.remove(my_rule)

Parameters:

  • rule_uid (String, Rule)

Returns:

  • (Rule, nil)

    The removed rule, if found.



64
65
66
67
68
69
70
71
72
73
# File 'lib/openhab/core/rules/registry.rb', line 64

def remove(rule_uid)
  rule_uid = rule_uid.uid if rule_uid.is_a?(Rule)
  return nil unless (provider = Provider.registry.provider_for(rule_uid))

  unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
    raise "Cannot remove rule #{rule_uid} from non-managed provider #{provider.inspect}"
  end

  provider.remove(rule_uid)
end

#scenesTaggedArray

Returns all Scenes (rules tagged with "Scene")

Returns:



80
81
82
# File 'lib/openhab/core/rules/registry.rb', line 80

def scenes
  @scenes ||= TaggedArray.new("Scene")
end

#scriptsTaggedArray

Returns all Scripts (rules tagged with "Script")

Returns:



89
90
91
# File 'lib/openhab/core/rules/registry.rb', line 89

def scripts
  @scripts ||= TaggedArray.new("Script")
end

#to_aArray<Rule>

Explicit conversion to array

Returns:



34
35
36
# File 'lib/openhab/core/rules/registry.rb', line 34

def to_a
  $rules.all.to_a
end