openhab-scripting 4.1.2 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/dsl.rb +8 -12
  4. data/lib/openhab/dsl/group.rb +1 -5
  5. data/lib/openhab/dsl/items/comparable_item.rb +49 -0
  6. data/lib/openhab/dsl/items/contact_item.rb +41 -0
  7. data/lib/openhab/dsl/items/date_time_item.rb +64 -0
  8. data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
  9. data/lib/openhab/dsl/items/generic_item.rb +174 -0
  10. data/lib/openhab/dsl/items/group_item.rb +56 -92
  11. data/lib/openhab/dsl/items/image_item.rb +5 -41
  12. data/lib/openhab/dsl/items/item_equality.rb +36 -0
  13. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  14. data/lib/openhab/dsl/items/items.rb +71 -35
  15. data/lib/openhab/dsl/items/metadata.rb +325 -0
  16. data/lib/openhab/dsl/items/number_item.rb +6 -312
  17. data/lib/openhab/dsl/items/numeric_item.rb +68 -0
  18. data/lib/openhab/dsl/items/persistence.rb +122 -0
  19. data/lib/openhab/dsl/items/player_item.rb +49 -40
  20. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  21. data/lib/openhab/dsl/items/string_item.rb +16 -58
  22. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  23. data/lib/openhab/dsl/lazy_array.rb +8 -6
  24. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  25. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  26. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  27. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  28. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  29. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  30. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  31. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  32. data/lib/openhab/dsl/rules/terse.rb +24 -0
  33. data/lib/openhab/dsl/states.rb +1 -1
  34. data/lib/openhab/dsl/time_of_day.rb +3 -5
  35. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  36. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  37. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  38. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  39. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  40. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  41. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  42. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  43. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  44. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  45. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  46. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  47. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  48. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  49. data/lib/openhab/dsl/types/string_type.rb +88 -0
  50. data/lib/openhab/dsl/types/type.rb +72 -0
  51. data/lib/openhab/dsl/types/types.rb +77 -0
  52. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  53. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  54. data/lib/openhab/dsl/units.rb +11 -6
  55. data/lib/openhab/version.rb +1 -1
  56. data/lib/openhab.rb +0 -1
  57. metadata +33 -28
  58. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  59. data/lib/openhab/dsl/items/item_command.rb +0 -90
  60. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  61. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  62. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  63. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  64. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  65. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  66. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  67. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  68. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  69. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  70. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  71. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  72. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  73. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  74. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  75. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  76. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  77. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  79. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  80. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  81. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  82. data/lib/openhab/dsl/types/datetime.rb +0 -338
  83. data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Types
6
+ java_import org.openhab.core.types.UnDefType
7
+
8
+ # Adds methods to core OpenHAB UnDefType to make it more natural in Ruby
9
+ class UnDefType # rubocop:disable Lint/EmptyClass
10
+ # @!parse include Type
11
+
12
+ # @!method null?
13
+ # Check if == +NULL+
14
+ # @return [Boolean]
15
+
16
+ # @!method undef?
17
+ # Check if == +UNDEF+
18
+ # @return [Boolean]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Types
6
+ java_import org.openhab.core.library.types.UpDownType
7
+
8
+ # Adds methods to core OpenHAB UpDownType to make it more natural in Ruby
9
+ class UpDownType
10
+ # @!parse include Type
11
+
12
+ # @!method up?
13
+ # Check if == +UP+
14
+ # @return [Boolean]
15
+
16
+ # @!method down?
17
+ # Check if == +DOWN+
18
+ # @return [Boolean]
19
+
20
+ #
21
+ # Invert the type
22
+ #
23
+ # @return [UpDownType] +UP+ if +DOWN+, +DOWN+ if +UP+
24
+ #
25
+ def !
26
+ return UP if down?
27
+ return DOWN if up?
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -10,7 +10,6 @@ end
10
10
 
11
11
  Object.send(:remove_const, :QuantityType)
12
12
  java_import org.openhab.core.library.types.QuantityType
13
- java_import org.openhab.core.types.util.UnitUtils
14
13
 
15
14
  module OpenHAB
16
15
  module DSL
@@ -18,6 +17,13 @@ module OpenHAB
18
17
  # Provides support for interacting with OpenHAB Units of Measurement
19
18
  #
20
19
  module Units
20
+ # @return The default unit for the current thread
21
+ def unit(unit = nil, &block)
22
+ return with_unit(unit, &block) if unit || block # back-compat
23
+
24
+ Thread.current[:unit]
25
+ end
26
+
21
27
  #
22
28
  # Sets a thread local variable to the supplied unit such that classes operating inside the block
23
29
  # can perform automatic conversions to the supplied unit for NumberItems
@@ -25,13 +31,12 @@ module OpenHAB
25
31
  # @param [Object] unit OpenHAB Unit or String representing unit
26
32
  # @yield [] Block executed in context of the supplied unit
27
33
  #
28
- #
29
- def unit(unit)
30
- unit = UnitUtils.parse_unit(unit) if unit.is_a? String
31
- Thread.current.thread_variable_set(:unit, unit)
34
+ def with_unit(unit)
35
+ unit = org.openhab.core.types.util.UnitUtils.parse_unit(unit) if unit.is_a? String
36
+ Thread.current[:unit] = unit
32
37
  yield
33
38
  ensure
34
- Thread.current.thread_variable_set(:unit, nil)
39
+ Thread.current[:unit] = nil
35
40
  end
36
41
  end
37
42
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.1.2'
8
+ VERSION = '4.3.0'
9
9
  end
data/lib/openhab.rb CHANGED
@@ -4,7 +4,6 @@ require 'openhab/core/load_path'
4
4
  require 'openhab/core/openhab_setup'
5
5
  require 'openhab/log/logger'
6
6
  require 'openhab/dsl/dsl'
7
-
8
7
  require 'openhab/version'
9
8
 
10
9
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.2
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-08 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,16 +54,24 @@ files:
54
54
  - lib/openhab/dsl/dsl.rb
55
55
  - lib/openhab/dsl/gems.rb
56
56
  - lib/openhab/dsl/group.rb
57
- - lib/openhab/dsl/items/datetime_item.rb
57
+ - lib/openhab/dsl/items/comparable_item.rb
58
+ - lib/openhab/dsl/items/contact_item.rb
59
+ - lib/openhab/dsl/items/date_time_item.rb
60
+ - lib/openhab/dsl/items/dimmer_item.rb
61
+ - lib/openhab/dsl/items/generic_item.rb
58
62
  - lib/openhab/dsl/items/group_item.rb
59
63
  - lib/openhab/dsl/items/image_item.rb
60
- - lib/openhab/dsl/items/item_command.rb
61
- - lib/openhab/dsl/items/item_delegate.rb
64
+ - lib/openhab/dsl/items/item_equality.rb
65
+ - lib/openhab/dsl/items/item_registry.rb
62
66
  - lib/openhab/dsl/items/items.rb
67
+ - lib/openhab/dsl/items/metadata.rb
63
68
  - lib/openhab/dsl/items/number_item.rb
69
+ - lib/openhab/dsl/items/numeric_item.rb
70
+ - lib/openhab/dsl/items/persistence.rb
64
71
  - lib/openhab/dsl/items/player_item.rb
65
72
  - lib/openhab/dsl/items/rollershutter_item.rb
66
73
  - lib/openhab/dsl/items/string_item.rb
74
+ - lib/openhab/dsl/items/switch_item.rb
67
75
  - lib/openhab/dsl/lazy_array.rb
68
76
  - lib/openhab/dsl/monkey_patch/actions/actions.rb
69
77
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
@@ -73,36 +81,16 @@ files:
73
81
  - lib/openhab/dsl/monkey_patch/events/item_state.rb
74
82
  - lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
75
83
  - lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
76
- - lib/openhab/dsl/monkey_patch/items/contact_item.rb
77
- - lib/openhab/dsl/monkey_patch/items/dimmer_item.rb
78
- - lib/openhab/dsl/monkey_patch/items/items.rb
79
- - lib/openhab/dsl/monkey_patch/items/metadata.rb
80
- - lib/openhab/dsl/monkey_patch/items/persistence.rb
81
- - lib/openhab/dsl/monkey_patch/items/switch_item.rb
82
84
  - lib/openhab/dsl/monkey_patch/ruby/number.rb
83
- - lib/openhab/dsl/monkey_patch/ruby/range.rb
84
85
  - lib/openhab/dsl/monkey_patch/ruby/ruby.rb
85
86
  - lib/openhab/dsl/monkey_patch/ruby/string.rb
86
- - lib/openhab/dsl/monkey_patch/ruby/time.rb
87
- - lib/openhab/dsl/monkey_patch/types/decimal_type.rb
88
- - lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb
89
- - lib/openhab/dsl/monkey_patch/types/next_previous_type.rb
90
- - lib/openhab/dsl/monkey_patch/types/on_off_type.rb
91
- - lib/openhab/dsl/monkey_patch/types/open_closed_type.rb
92
- - lib/openhab/dsl/monkey_patch/types/percent_type.rb
93
- - lib/openhab/dsl/monkey_patch/types/play_pause_type.rb
94
- - lib/openhab/dsl/monkey_patch/types/quantity_type.rb
95
- - lib/openhab/dsl/monkey_patch/types/refresh_type.rb
96
- - lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb
97
- - lib/openhab/dsl/monkey_patch/types/stop_move_type.rb
98
- - lib/openhab/dsl/monkey_patch/types/types.rb
99
- - lib/openhab/dsl/monkey_patch/types/up_down_type.rb
100
87
  - lib/openhab/dsl/persistence.rb
101
88
  - lib/openhab/dsl/rules/automation_rule.rb
102
89
  - lib/openhab/dsl/rules/guard.rb
103
90
  - lib/openhab/dsl/rules/property.rb
104
91
  - lib/openhab/dsl/rules/rule.rb
105
92
  - lib/openhab/dsl/rules/rule_config.rb
93
+ - lib/openhab/dsl/rules/terse.rb
106
94
  - lib/openhab/dsl/rules/triggers/changed.rb
107
95
  - lib/openhab/dsl/rules/triggers/channel.rb
108
96
  - lib/openhab/dsl/rules/triggers/command.rb
@@ -113,8 +101,25 @@ files:
113
101
  - lib/openhab/dsl/things.rb
114
102
  - lib/openhab/dsl/time_of_day.rb
115
103
  - lib/openhab/dsl/timers.rb
116
- - lib/openhab/dsl/types/datetime.rb
117
- - lib/openhab/dsl/types/quantity.rb
104
+ - lib/openhab/dsl/types/comparable_type.rb
105
+ - lib/openhab/dsl/types/date_time_type.rb
106
+ - lib/openhab/dsl/types/decimal_type.rb
107
+ - lib/openhab/dsl/types/increase_decrease_type.rb
108
+ - lib/openhab/dsl/types/next_previous_type.rb
109
+ - lib/openhab/dsl/types/numeric_type.rb
110
+ - lib/openhab/dsl/types/on_off_type.rb
111
+ - lib/openhab/dsl/types/open_closed_type.rb
112
+ - lib/openhab/dsl/types/percent_type.rb
113
+ - lib/openhab/dsl/types/play_pause_type.rb
114
+ - lib/openhab/dsl/types/quantity_type.rb
115
+ - lib/openhab/dsl/types/refresh_type.rb
116
+ - lib/openhab/dsl/types/rewind_fastforward_type.rb
117
+ - lib/openhab/dsl/types/stop_move_type.rb
118
+ - lib/openhab/dsl/types/string_type.rb
119
+ - lib/openhab/dsl/types/type.rb
120
+ - lib/openhab/dsl/types/types.rb
121
+ - lib/openhab/dsl/types/un_def_type.rb
122
+ - lib/openhab/dsl/types/up_down_type.rb
118
123
  - lib/openhab/dsl/units.rb
119
124
  - lib/openhab/log/configuration.rb
120
125
  - lib/openhab/log/logger.rb
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
- require 'java'
5
- require 'time'
6
- require 'openhab/dsl/types/datetime'
7
- require 'openhab/dsl/items/item_delegate'
8
- require 'openhab/dsl/items/item_command'
9
-
10
- module OpenHAB
11
- module DSL
12
- module Items
13
- #
14
- # Delegation to OpenHAB DateTime Item
15
- #
16
- # @author Anders Alfredsson
17
- #
18
- class DateTimeItem
19
- extend Forwardable
20
- extend OpenHAB::DSL::Items::ItemDelegate
21
- extend OpenHAB::DSL::Items::ItemCommand
22
- include Comparable
23
-
24
- def_item_delegator :@datetime_item
25
-
26
- item_type Java::OrgOpenhabCoreLibraryItems::DateTimeItem
27
-
28
- #
29
- # Create a new DateTimeItem
30
- #
31
- # @param [Java::org::openhab::core::libarary::items::DateTimeItem] datetime_item Openhab DateTimeItem to
32
- # delegate to
33
- #
34
- def initialize(datetime_item)
35
- @datetime_item = datetime_item
36
- item_missing_delegate { @datetime_item }
37
- item_missing_delegate { to_dt }
38
- end
39
-
40
- #
41
- # Return an instance of DateTime that wraps the DateTimeItem's state
42
- #
43
- # @return [OpenHAB::DSL::Types::DateTime] Wrapper for the Item's state, or nil if it has no state
44
- #
45
- def to_dt
46
- OpenHAB::DSL::Types::DateTime.new(@datetime_item.state) if state?
47
- end
48
-
49
- #
50
- # Compare the Item's state to another Item or object that can be compared
51
- #
52
- # @param [Object] other Other objet to compare against
53
- #
54
- # @return [Integer] -1, 0 or 1 depending on the result of the comparison
55
- #
56
- def <=>(other)
57
- return unless state?
58
-
59
- logger.trace("Comparing self (#{self.class}) to #{other} (#{other.class})")
60
- other = other.to_dt if other.is_a? DateTimeItem
61
- to_dt <=> other
62
- end
63
-
64
- #
65
- # Get the time zone of the Item
66
- #
67
- # @return [String] The timezone in `[+-]hh:mm(:ss)` format or nil if the Item has no state
68
- #
69
- def zone
70
- to_dt.zone if state?
71
- end
72
- end
73
- end
74
- end
75
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/log/logger'
5
- require 'openhab/dsl/monkey_patch/events/item_command'
6
-
7
- module OpenHAB
8
- module DSL
9
- module Items
10
- #
11
- # Holds methods to automatically generate commands and
12
- # accessors for items
13
- module ItemCommand
14
- include OpenHAB::Log
15
-
16
- #
17
- # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
18
- # string representation of the enum value For example, an enum with values of STOP and START
19
- # would create methods stop() and start() that send the corresponding STOP and START commands to the item
20
- #
21
- # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
22
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
23
- # will be used as the method name instead, for example `:play? => :playing?`
24
- #
25
- #
26
- def item_command(command_enum, methods = {})
27
- # rubocop:disable Style/HashEachMethods
28
- # Disable rule because Java enum does not support each_value
29
- command_enum.values.each do |command|
30
- command_method = command.to_s.downcase
31
- command_method = methods.transform_keys(&:to_sym).fetch(command_method.to_sym, command_method)
32
- logger.trace("Creating command method (#{command_method}) for #{self}")
33
- define_method(command_method) do
34
- self.command(command)
35
- end
36
- end
37
- # rubocop:enable Style/HashEachMethods
38
- end
39
-
40
- #
41
- # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
42
- # string representation appended with a question mark '?' of the enum value For example,
43
- # an enum with values of UP and DOWN would create methods up? and down? that check
44
- # if the current state matches the value of the enum
45
- #
46
- # @param [Java::JavaLang::Enum] command_enum Enumeration to create methods for each value
47
- # to check if current state matches that enum
48
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
49
- # will be used as the method name instead, for example `:play? => :playing?`
50
- #
51
- #
52
- def item_state(command_enum, methods = {})
53
- # rubocop:disable Style/HashEachMethods
54
- # Disable rule because Java enum does not support each_value
55
- command_enum.values.each do |command|
56
- status_method = "#{command.to_s.downcase}?"
57
- status_method = methods.transform_keys(&:to_sym).fetch(status_method.to_sym, status_method)
58
- logger.trace("Creating status method (#{status_method}) for #{self}")
59
- define_method(status_method) do
60
- state? && state.as(command_enum) == command
61
- end
62
- end
63
- # rubocop:enable Style/HashEachMethods
64
- end
65
-
66
- #
67
- # Extract the accepted state and command types from the specified OpenHAB
68
- # Item class and pass them to item_state/item_command
69
- #
70
- # @param [Java::JavaLang::Class] item_class a Class that implements Java::OrgOpenhabCoreItems::Item
71
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
72
- # will be used as the method name instead, for example `:play? => :playing?`
73
- #
74
- def item_type(item_class, methods = {}) # rubocop:disable Metrics/MethodLength
75
- item_class.field_reader(:ACCEPTED_DATA_TYPES)
76
- item_class.field_reader(:ACCEPTED_COMMAND_TYPES)
77
- item_class.ACCEPTED_DATA_TYPES.select(&:is_enum)
78
- .grep_v(UnDefType)
79
- .each { |type| item_state(type.ruby_class, methods) }
80
- item_class.ACCEPTED_COMMAND_TYPES.select(&:is_enum)
81
- .grep_v(UnDefType)
82
- .each do |type|
83
- item_command(type.ruby_class, methods)
84
- MonkeyPatch::Events::ItemCommandEvent.def_enum_predicates(type.ruby_class)
85
- end
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,125 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/log/logger'
5
- require 'forwardable'
6
-
7
- module OpenHAB
8
- module DSL
9
- module Items
10
- #
11
- # Holds methods to delegate to items
12
- #
13
- module ItemDelegate
14
- include OpenHAB::Log
15
-
16
- # @return [Array<Symbol>] Array of methods that by default will be forwarded to an OpenHAB Item.
17
- DEFAULT_DELEGATION_METHODS = %i[to_s groups].freeze
18
- private_constant :DEFAULT_DELEGATION_METHODS
19
-
20
- #
21
- # Extends calling object with DSL and helper methods
22
- #
23
- # @param [Object] base Object to decorate with DSL and helper methods
24
- #
25
- #
26
- def self.extended(base)
27
- base.extend Forwardable
28
- base.include OpenHAB::DSL::Items::ItemDelegate
29
- end
30
-
31
- #
32
- # Delegate a set of methods to the supplied delegator
33
- #
34
- # @param [Java::OrgOpenhabCoreItems::GenericItem] item to delegate methods to
35
- # @param [Symbol] methods to delegate, if not supplied default methods are used
36
- #
37
- #
38
- def def_item_delegator(item, *methods)
39
- methods = DEFAULT_DELEGATION_METHODS if methods.size.zero?
40
- methods.each do |method|
41
- logger.trace("Creating delegate for (#{method}) to #{item}) for #{self.class}")
42
- def_delegator item, method
43
- end
44
- define_method(:oh_item) { instance_variable_get(item) }
45
- define_method(:hash) { oh_item.hash_code }
46
- define_method(:eql?) { |other| hash == other.hash }
47
- end
48
-
49
- #
50
- # Delegates methods to the object returned from the supplied block if
51
- # they don't exist in the object this is included in. If the supplied block returns nil, no delegation occurs
52
- # If this item is called more than once delegation occurs in the order of invocation, i.e. the object returned
53
- # by the first block is delegated to if it responds to the missing method,
54
- # then the second block is processed, etc.
55
- #
56
- # @param [Proc] &delegate delgegate block
57
- #
58
- #
59
- def item_missing_delegate(&delegate)
60
- @delegates ||= []
61
- @delegates << delegate
62
- end
63
-
64
- #
65
- # Delegate missing method calls to delegates supplied to item_delgate method
66
- # if no delegates exist or respond to missing method, super is invoked which will
67
- # throw the appropriate method missing error
68
- #
69
- # @param [String] meth misisng method
70
- # @param [Array] *args Arguments to the missing method
71
- # @param [Proc] &block supplied to the missing method
72
- #
73
- # @return [Object] Result of missing method invocation
74
- #
75
- def method_missing(meth, *args, &block)
76
- logger.trace("Method (#{meth}) missing for item #{self.class}")
77
- delegate = delegate_for(meth)
78
- if delegate
79
- logger.trace("Delegating #{meth} to #{delegate.class}")
80
- delegate.__send__(meth, *args, &block)
81
- else
82
- super
83
- end
84
- end
85
-
86
- #
87
- # Checks if any of the supplied delgates respond to a specific method
88
- #
89
- # @param [String] meth method to check for
90
- # @param [Boolean] _include_private if private methods should be checked
91
- #
92
- # @return [Boolean] True if any delegates respond to method, false otherwise
93
- #
94
- def respond_to_missing?(meth, _include_private = false)
95
- logger.trace("Checking if (#{self.class}) responds to (#{meth})")
96
- responds = !delegate_for(meth).nil?
97
- logger.trace("(#{self.class}) responds to (#{meth}) (#{responds})")
98
- responds
99
- end
100
-
101
- private
102
-
103
- #
104
- # Find a delegate for the supplied method
105
- #
106
- # @param [String] meth method to find delegate for
107
- #
108
- # @return [Boolean] True if any method responds to the supplied delegate, false otherwise
109
- #
110
- def delegate_for(meth)
111
- (@delegates || []).each do |delegate_block|
112
- delegate = delegate_block.call(meth)
113
- logger.trace("Checking if delegate (#{delegate.class}) responds to (#{meth})")
114
- if delegate.respond_to? meth
115
- logger.trace("Delegate (#{delegate.class}) found for method (#{meth})")
116
- return delegate
117
- end
118
- end
119
- logger.trace("No delegate found for method (#{meth})")
120
- nil
121
- end
122
- end
123
- end
124
- end
125
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/dsl/items/item_command'
5
-
6
- module OpenHAB
7
- module DSL
8
- module MonkeyPatch
9
- #
10
- # Patches OpenHAB items
11
- #
12
- module Items
13
- java_import Java::OrgOpenhabCoreLibraryItems::ContactItem
14
-
15
- #
16
- # Alias class for ContactItem
17
- #
18
- ::Contact = ContactItem
19
-
20
- #
21
- # Monkey patch Contact Item with Ruby methods
22
- #
23
- class ContactItem
24
- extend OpenHAB::DSL::Items::ItemCommand
25
-
26
- java_import Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
27
-
28
- item_type Java::OrgOpenhabCoreLibraryItems::ContactItem
29
-
30
- #
31
- # Compares contacts to OpenClosedTypes
32
- #
33
- # @param [Object] other object to compare to
34
- #
35
- # @return [Boolean] True if contact has a state and state equals other, nil if no state,
36
- # result from super if not supplied an OpenClosedType
37
- #
38
- def ==(other)
39
- other = other.get_state_as(OpenClosedType) if other.respond_to?(:get_state_as)
40
-
41
- if other.is_a? OpenClosedType
42
- state? && state == other
43
- else
44
- super
45
- end
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end