Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terse Rule: fix inferred id not generating unique suffix #264

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/openhab/dsl/rules/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def initialize(provider)
# run { logger.info "Happy new day!" }
# end
#
def rule(name = nil, id: nil, replace: false, script: nil, binding: nil, &block)
def rule(name = nil, id: nil, replace: nil, script: nil, binding: nil, &block)
raise ArgumentError, "Block is required" unless block

inferred_id = nil
Expand All @@ -89,7 +89,7 @@ def rule(name = nil, id: nil, replace: false, script: nil, binding: nil, &block)

if replace
logger.debug { "Removing existing rule '#{builder.uid}'." } if DSL.rules.remove(builder.uid)
else
elsif replace.nil?
id_not_inferred = inferred_id.nil? || inferred_id != builder.uid
if id_not_inferred && (existing_rule = $rules.get(builder.uid))
logger.warn "Rule '#{builder.uid}' is not created because " \
Expand Down
37 changes: 21 additions & 16 deletions lib/openhab/dsl/rules/terse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,27 @@ class << self
# @see BuilderDSL#$1
def def_terse_rule(trigger)
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{trigger}(*args, id: nil, name: nil, description: nil, # def changed(*args, id: nil, name: nil, description: nil,
tag: nil, tags: nil, on_load: false, **kwargs, &block) # tag: nil, tags: nil, on_load: false, **kwargs, &block)
raise ArgumentError, "Block is required" unless block # raise ArgumentError, "Block is required" unless block
#
id ||= NameInference.infer_rule_id_from_block(block) # id ||= NameInference.infer_rule_id_from_block(block)
script = block.source rescue nil # script = block.source rescue nil
caller_binding = block.binding # caller_binding = block.binding
rule name, id: id, script: script, binding: caller_binding do # rule name, id: id, script: script, binding: caller_binding do
self.on_load if on_load # self.on_load if on_load
self.description(description) if description # self.description(description) if description
self.tags(*Array.wrap(tag), *Array.wrap(tags)) # self.tags(*Array.wrap(tag), *Array.wrap(tags))
#{trigger}(*args, **kwargs) # changed(*args, **kwargs)
run(&block) # run(&block)
end # end
end # end
module_function #{trigger.inspect} # module_function :changed
def #{trigger}(*args, id: nil, name: nil, description: nil, # def changed(*args, id: nil, name: nil, description: nil,
tag: nil, tags: nil, on_load: false, **kwargs, &block) # tag: nil, tags: nil, on_load: false, **kwargs, &block)
raise ArgumentError, "Block is required" unless block # raise ArgumentError, "Block is required" unless block
#
replace = nil # replace = nil
unless id # unless id
replace = false # replace = false
id = NameInference.infer_rule_id_from_block(block) # id = NameInference.infer_rule_id_from_block(block)
end # end
script = block.source rescue nil # script = block.source rescue nil
caller_binding = block.binding # caller_binding = block.binding
rule name, id: id, replace: replace, script: script, # rule name, id: id, replace: replace, script: script
binding: caller_binding do # binding: caller_binding do
self.on_load if on_load # self.on_load if on_load
self.description(description) if description # self.description(description) if description
self.tags(*Array.wrap(tag), *Array.wrap(tags)) # self.tags(*Array.wrap(tag), *Array.wrap(tags))
#{trigger}(*args, **kwargs) # changed(*args, **kwargs)
run(&block) # run(&block)
end # end
end # end
module_function #{trigger.inspect} # module_function :changed
RUBY
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/openhab/dsl/rules/terse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
expect(ran).to be true
end

it "infers a unique id for each rule" do
rules = []
2.times do
rules << changed(TestSwitch) { nil }
end
expect(rules.map(&:uid).uniq.length).to be > 1
end

it "doesn't create a new rule with an existing rule id" do
changed(TestSwitch, id: "Test") { nil }
rule = changed(TestSwitch, id: "Test") { nil }
expect(rule).to be_nil
end

it "returns the rule object" do
rule = changed(TestSwitch) { nil }
expect(rule).to be_a OpenHAB::Core::Rules::Rule
Expand Down