Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
fix(rule): rule method returns the created rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng committed Feb 28, 2022
1 parent e13218a commit afe7f69
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 13 additions & 1 deletion features/rule_language.feature
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,16 @@ Feature: rule_language
logger.info("Conf directory is #{__conf__.each_filename.to_a.last(2).join('/')}")
"""
When I deploy the rules file
Then It should log 'Conf directory is openhab/conf' within 5 seconds
Then It should log 'Conf directory is openhab/conf' within 5 seconds

Scenario: Rule method returns the rule object with Rule UID
Given code in a rules file:
"""
rule = rule 'test' do
on_start
run { logger.info('inside rule') }
end
logger.info "Rule UID: '#{rule.uid}'"
"""
When I deploy the rules file
Then It should log /Rule UID: '.+'/ within 5 seconds
7 changes: 2 additions & 5 deletions lib/openhab/dsl/rules/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class << self
# @yield [] Block executed in context of a RuleConfig
#
#
# rubocop: disable Metrics/MethodLength
def rule(rule_name, &block)
thread_local(RULE_NAME: rule_name) do
@rule_name = rule_name
Expand All @@ -43,12 +42,10 @@ def rule(rule_name, &block)
config.guard = Guard::Guard.new(run_context: config.caller, only_if: config.only_if, not_if: config.not_if)
logger.trace { config.inspect }
process_rule_config(config)
nil # Must return something other than the rule object. See https://github.com/boc-tothefuture/openhab-jruby/issues/438
end
rescue StandardError => e
logger.log_exception(e, @rule_name)
end
# rubocop: enable Metrics/MethodLength

#
# Cleanup rules in this script file
Expand All @@ -71,10 +68,10 @@ def process_rule_config(config)

rule = AutomationRule.new(config: config)
Rules.script_rules << rule
add_rule(rule)
added_rule = add_rule(rule)

rule.execute(nil, { 'event' => Struct.new(:attachment).new(config.start_attachment) }) if config.on_start?
rule
added_rule
end

#
Expand Down

0 comments on commit afe7f69

Please sign in to comment.