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

Commit

Permalink
feat(conf): rename __conf__ to OpenHAB.conf_root
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng committed Apr 4, 2022
1 parent 33676e3 commit ea0a657
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.
13 changes: 7 additions & 6 deletions docs/usage/triggers/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,44 @@ When an event is triggered to a rule, the event object has the following fields

## Examples

Watch `items` directory inside of the openhab configuration path and log any changes. `__conf__` is available and is the path to the OpenHAB configuration directory as a Ruby pathname object.
Watch `items` directory inside of the openhab configuration path and log any changes. `OpenHAB.conf_root` is available and is the path
to the OpenHAB configuration directory as a Ruby pathname object.

```ruby
rule 'watch directory' do
watch __conf__/'items'
watch OpenHAB.conf_root/'items'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
```

Watch `items` directory for files that end in `*.erb` inside of the openhab configuration path and log any changes
```ruby
rule 'watch directory' do
watch __conf__/'items', glob: '*.erb'
watch OpenHAB.conf_root/'items', glob: '*.erb'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
```

Watch `items/foo.items` inside of the openhab configuration path and log any changes
```ruby
rule 'watch directory' do
watch __conf__/'items/foo.items'
watch OpenHAB.conf_root/'items/foo.items'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
```

Watch `items/*.items` inside of the openhab configuration path and log any changes
```ruby
rule 'watch directory' do
watch __conf__/'items/*.items'
watch OpenHAB.conf_root/'items/*.items'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
```

Watch `items/*.items` inside of the openhab configuration path for when items files are deleted or created (ignore changes)
```ruby
rule 'watch directory' do
watch __conf__/'items/*.items', for: [:deleted, :created]
watch OpenHAB.conf_root/'items/*.items', for: [:deleted, :created]
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
```
Expand Down
4 changes: 2 additions & 2 deletions features/attachments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature: attachments
end
"""
When <action>
Then It should log 'attachment - <attachment>' within 5 seconds
Then It should log 'attachment - <attachment>' within 15 seconds
Examples: Checks multiple attachments
| trigger | attachment | action |
| changed Switch1 | foo | item "Switch1" state is changed to "ON" |
Expand All @@ -32,7 +32,7 @@ Feature: attachments
| on_start true | qaz | I wait 2 seconds |
| every :second | qux | I wait 2 seconds |
| cron five_seconds_from_now | qab | I wait 5 seconds |
| watch __conf__+'foo' | qac | I create a file in subdirectory 'foo' of conf named 'bar' |
| watch OpenHAB.conf_root/'foo' | qac | I create a file in subdirectory 'foo' of conf named 'bar' |


Scenario Outline: Guards have access to attachments
Expand Down
6 changes: 3 additions & 3 deletions features/rule_language.feature
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ Feature: rule_language
Scenario: OpenHAB config directory is available
Given code in a rules file:
"""
logger.info("Conf #{__conf__}")
logger.info("Conf directory is #{__conf__.each_filename.to_a.last(2).join('/')}")
logger.info("Conf #{OpenHAB.conf_root}")
logger.info("Conf directory is #{OpenHAB.conf_root.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
Expand All @@ -234,7 +234,7 @@ Feature: rule_language
"""
When I deploy the rules file
Then It should log /Rule UID: '.+'/ within 5 seconds

Scenario: DSL methods don't leak into other objects
Given a raw rule:
"""
Expand Down
76 changes: 38 additions & 38 deletions features/watch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,79 @@ Feature: watch
Background:
Given Clean OpenHAB with latest Ruby Libraries

Scenario Outline: Watch supports directories
Scenario Outline: Watch supports directories
Given a file in subdirectory 'foo' of conf named 'bar'
And a file in subdirectory 'foo' of conf named 'baz'
And a deployed rule:
"""
rule 'watch directory' do
watch __conf__ + 'foo'
watch OpenHAB.conf_root + 'foo'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It should log '<log>' within 5 seconds
Examples:
| action | file | log |
| create | qux | qux - created |
| delete | bar | bar - deleted |
| modify | baz | baz - modified |
| action | file | log |
| create | qux | qux - created |
| delete | bar | bar - deleted |
| modify | baz | baz - modified |

Scenario Outline: Watch supports globs
Given a file in subdirectory 'foo' of conf named 'bar.erb'
And a file in subdirectory 'foo' of conf named 'bar'
And a deployed rule:
"""
rule 'watch directory' do
watch __conf__/'foo', glob: '*.erb'
watch OpenHAB.conf_root/'foo', glob: '*.erb'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It <should> log '<log>' within 5 seconds
Examples:
| action | file | should | log |
| create | qux.erb | should | qux.erb - created |
| delete | bar.erb | should | bar.erb - deleted |
| modify | baz.erb | should | baz.erb - modified |
| create | qux | should not| qux - created |
| delete | bar | should not| qux - deleted |
| modify | baz | should not| baz - modified |
| action | file | should | log |
| create | qux.erb | should | qux.erb - created |
| delete | bar.erb | should | bar.erb - deleted |
| modify | baz.erb | should | baz.erb - modified |
| create | qux | should not | qux - created |
| delete | bar | should not | qux - deleted |
| modify | baz | should not | baz - modified |

Scenario Outline: Watch supports a single file
Given a file in subdirectory 'foo' of conf named 'bar'
And a file in subdirectory 'foo' of conf named 'baz'
And a deployed rule:
"""
rule 'watch file' do
watch __conf__ + 'foo/bar'
watch OpenHAB.conf_root + 'foo/bar'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It <should> log '<log>' within 5 seconds
Examples:
| action | file | should | log |
| modify | bar | should | bar - modified |
| delete | bar | should | bar - deleted |
| modify | baz | should not| baz - modified |
| delete | baz | should not| baz - deleted |
| action | file | should | log |
| modify | bar | should | bar - modified |
| delete | bar | should | bar - deleted |
| modify | baz | should not | baz - modified |
| delete | baz | should not | baz - deleted |

Scenario Outline: Watch supports creation for single file
Given a subdirectory 'foo' of conf
And a deployed rule:
"""
rule 'watch file' do
watch __conf__ + 'foo/bar'
watch OpenHAB.conf_root + 'foo/bar'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It <should> log '<log>' within 5 seconds
Examples:
| action | file | should | log |
| create | bar | should | bar - created |
| create | baz | should not| baz - created |
| action | file | should | log |
| create | bar | should | bar - created |
| create | baz | should not | baz - created |


Scenario Outline: Watch supports globs in path
Expand All @@ -86,35 +86,35 @@ Feature: watch
And a deployed rule:
"""
rule 'watch directory' do
watch __conf__ + 'foo/*.erb'
watch OpenHAB.conf_root + 'foo/*.erb'
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It <should> log '<log>' within 5 seconds
Examples:
| action | file | should | log |
| create | qux.erb | should | qux.erb - created |
| delete | bar.erb | should | bar.erb - deleted |
| modify | baz.erb | should | baz.erb - modified |
| create | qux | should not| qux - created |
| delete | bar | should not| qux - deleted |
| modify | baz | should not| baz - modifie |
| action | file | should | log |
| create | qux.erb | should | qux.erb - created |
| delete | bar.erb | should | bar.erb - deleted |
| modify | baz.erb | should | baz.erb - modified |
| create | qux | should not | qux - created |
| delete | bar | should not | qux - deleted |
| modify | baz | should not | baz - modifie |

Scenario Outline: Watch can limit event triggers
Given a file in subdirectory 'foo' of conf named 'bar'
And a deployed rule:
"""
rule 'watch directory' do
watch __conf__ + 'foo', for: <event_type>
watch OpenHAB.conf_root + 'foo', for: <event_type>
run { |event| logger.info("#{event.path.basename} - #{event.type}") }
end
"""
When I <action> a file in subdirectory 'foo' of conf named '<file>'
Then It <should> log '<log>' within 5 seconds
Examples:
| action | file | event_type | should | log |
| delete | bar | :deleted | should | bar - deleted |
| delete | bar | [:deleted, :modified] | should | bar - deleted |
| delete | bar | :modified | should not| bar - deleted |
| delete | bar | [:modified,:created] | should not| bar - deleted |
| action | file | event_type | should | log |
| delete | bar | :deleted | should | bar - deleted |
| delete | bar | [:deleted, :modified] | should | bar - deleted |
| delete | bar | :modified | should not | bar - deleted |
| delete | bar | [:modified,:created] | should not | bar - deleted |
17 changes: 11 additions & 6 deletions lib/openhab/dsl/openhab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
require 'pathname'

module OpenHAB
#
# Return the OpenHAB conf directory as a ruby pathname
#
# @return [Pathname] OpenHAB conf path
#
def self.conf_root
Pathname.new(ENV['OPENHAB_CONF'])
end

module DSL
#
# Provides access to OpenHAB attributes
Expand All @@ -12,13 +21,9 @@ module Core

module_function

#
# Return the OpenHAB conf directory as a ruby pathname
#
# @return [Pathname] OpenHAB conf path
#
# @deprecated Please use {OpenHAB.conf_root} instead
def __conf__
Pathname.new(ENV['OPENHAB_CONF'])
OpenHAB.conf_root
end
end
end
Expand Down

0 comments on commit ea0a657

Please sign in to comment.