Class: OpenHAB::Core::Items::Metadata::Hash
- Inherits:
-
Object
- Object
- OpenHAB::Core::Items::Metadata::Hash
- Includes:
- EmulateHash
- Defined in:
- lib/openhab/core/items/metadata/hash.rb
Overview
Hash represents the configuration for a single metadata namespace.
It implements the entire interface of Hash.
All keys are converted to strings.
As a special case, a #== comparison can be done against a [value, config] array.
Instance Attribute Summary collapse
- #item ⇒ Object readonly
- #namespace ⇒ String readonly
-
#value ⇒ String
The main value for the metadata namespace.
Instance Method Summary collapse
-
#attached? ⇒ true, false
Is this object attached to an actual Item?.
- #provider ⇒ org.openhab.core.common.registry.Provider?
- #provider! ⇒ org.openhab.core.common.registry.ManagedProvider
-
#replace(new_config) ⇒ self
Replace the configuration with a new Hash.
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!
Instance Attribute Details
#item ⇒ Object (readonly)
85 86 87 88 89 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 85 def item return nil unless attached? DSL.items[uid.item_name] end |
#namespace ⇒ String (readonly)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 26 class Hash java_import org.openhab.core.items.Metadata private_constant :Metadata # Mutators are manually implemented below. include EmulateHash extend Forwardable def_delegators :@metadata, :configuration, :hash, :uid, :value private :configuration def_delegator :uid, :namespace alias_method :to_map, :configuration private :to_map class << self # @!visibility private def from_item(item_name, namespace, value) namespace = namespace.to_s value = case value when Hash return value if value.uid.item_name == item_name && value.uid.namespace == namespace [value.value, value.send(:configuration)] when Array raise ArgumentError, "Array must contain 2 elements: value, config" if value.length != 2 [value.first, (value.last || {}).transform_keys(&:to_s)] when ::Hash then ["", value.transform_keys(&:to_s)] else [value.to_s, {}] end new(Metadata.new(org.openhab.core.items.MetadataKey.new(namespace.to_s, item_name), *value)) end # @!visibility private def from_value(namespace, value) from_item("-", namespace, value) end end # @!visibility private def initialize(metadata = nil) @metadata = metadata end # @!visibility private def dup new(Metadata.new(org.openhab.core.items.MetadataKey.new(uid.namespace, "-"), value, configuration)) end # Is this object attached to an actual Item? # @return [true,false] def attached? uid.item_name != "-" end # @!attribute [r] item # @return [Item, nil] The item this namespace is attached to. def item return nil unless attached? DSL.items[uid.item_name] end # @!visibility private def commit return unless attached? javaify provider!.update(@metadata) end # @!visibility private def create_or_update return unless attached? javaify (p = provider!).get(uid) ? p.update(@metadata) : p.add(@metadata) end # @!visibility private def remove provider!.remove(uid) end # @!visibility private def eql?(other) return true if equal?(other) return false unless other.is_a?(Hash) return false unless value == other.value configuration == other.configuration end # # Set the metadata value # def value=(value) @metadata = org.openhab.core.items.Metadata.new(uid, value.to_s, configuration) commit end # @!visibility private def <(other) if other.is_a?(Hash) return false if attached? && uid == other.uid return false unless value == other.value end configuration < other end # @!visibility private def <=(other) if other.is_a?(Hash) return true if attached? && uid == other.uid return false unless value == other.value end configuration <= other end # @!visibility private def ==(other) if other.is_a?(Hash) return false unless value == other.value return configuration == other.configuration elsif value.empty? && other.respond_to?(:to_hash) return configuration == other.to_hash elsif other.is_a?(Array) return other == [value, configuration] end false end # @!visibility private def >(other) if other.is_a?(Hash) return false if attached? && uid == other.uid return false unless value == other.value end configuration > other end # @!visibility private def >=(other) if other.is_a?(Hash) return true if attached? && uid == other.uid return false unless value == other.value end configuration >= other end # @!visibility private def store(key, value) key = key.to_s new_config = to_h new_config[key] = value replace(new_config) value end # @!visibility private def delete(key) key = key.to_s new_config = to_h return yield(key) if block_given? && !new_config.key?(key) old_value = new_config.delete(key) replace(new_config) old_value end # # Replace the configuration with a new {::Hash}. # # @param [::Hash] new_config # @return [self] # def replace(new_config) @metadata = org.openhab.core.items.Metadata.new(uid, value, new_config.transform_keys(&:to_s)) commit self end # @!visibility private def inspect return to_h.inspect if value.empty? return value.inspect if configuration.empty? [value, to_h].inspect end # @return [org.openhab.core.common.registry.Provider, nil] def provider Provider.registry.provider_for(uid) end # # @raise [FrozenError] if the provider is not a # {org.openhab.core.common.registry.ManagedProvider ManagedProvider} that can be updated. # @return [org.openhab.core.common.registry.ManagedProvider] # def provider! preferred_provider = Provider.current( Thread.current[:openhab_providers]&.dig(:metadata_items, uid.item_name) || Thread.current[:openhab_providers]&.dig(:metadata_namespaces, uid.namespace), self ) if attached? provider = self.provider return preferred_provider unless provider unless provider.is_a?(ManagedProvider) raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}." end if preferred_provider != provider logger.warn("Provider #{preferred_provider.inspect} cannot be used with #{uid}; " \ "reverting to provider #{provider.inspect}. " \ "This may cause unexpected issues, like metadata persisting that you did not expect to.") preferred_provider = provider end end preferred_provider end private # # @see https://github.com/openhab/openhab-core/issues/3169 # # in the meantime, force the serialization round-trip right now # def javaify mapper = Provider.registry.managed_provider.get.storage.entityMapper @metadata = mapper.from_json(mapper.to_json_tree(@metadata), Metadata.java_class) end end |
#value ⇒ String
Returns The main value for the metadata namespace.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 26 class Hash java_import org.openhab.core.items.Metadata private_constant :Metadata # Mutators are manually implemented below. include EmulateHash extend Forwardable def_delegators :@metadata, :configuration, :hash, :uid, :value private :configuration def_delegator :uid, :namespace alias_method :to_map, :configuration private :to_map class << self # @!visibility private def from_item(item_name, namespace, value) namespace = namespace.to_s value = case value when Hash return value if value.uid.item_name == item_name && value.uid.namespace == namespace [value.value, value.send(:configuration)] when Array raise ArgumentError, "Array must contain 2 elements: value, config" if value.length != 2 [value.first, (value.last || {}).transform_keys(&:to_s)] when ::Hash then ["", value.transform_keys(&:to_s)] else [value.to_s, {}] end new(Metadata.new(org.openhab.core.items.MetadataKey.new(namespace.to_s, item_name), *value)) end # @!visibility private def from_value(namespace, value) from_item("-", namespace, value) end end # @!visibility private def initialize(metadata = nil) @metadata = metadata end # @!visibility private def dup new(Metadata.new(org.openhab.core.items.MetadataKey.new(uid.namespace, "-"), value, configuration)) end # Is this object attached to an actual Item? # @return [true,false] def attached? uid.item_name != "-" end # @!attribute [r] item # @return [Item, nil] The item this namespace is attached to. def item return nil unless attached? DSL.items[uid.item_name] end # @!visibility private def commit return unless attached? javaify provider!.update(@metadata) end # @!visibility private def create_or_update return unless attached? javaify (p = provider!).get(uid) ? p.update(@metadata) : p.add(@metadata) end # @!visibility private def remove provider!.remove(uid) end # @!visibility private def eql?(other) return true if equal?(other) return false unless other.is_a?(Hash) return false unless value == other.value configuration == other.configuration end # # Set the metadata value # def value=(value) @metadata = org.openhab.core.items.Metadata.new(uid, value.to_s, configuration) commit end # @!visibility private def <(other) if other.is_a?(Hash) return false if attached? && uid == other.uid return false unless value == other.value end configuration < other end # @!visibility private def <=(other) if other.is_a?(Hash) return true if attached? && uid == other.uid return false unless value == other.value end configuration <= other end # @!visibility private def ==(other) if other.is_a?(Hash) return false unless value == other.value return configuration == other.configuration elsif value.empty? && other.respond_to?(:to_hash) return configuration == other.to_hash elsif other.is_a?(Array) return other == [value, configuration] end false end # @!visibility private def >(other) if other.is_a?(Hash) return false if attached? && uid == other.uid return false unless value == other.value end configuration > other end # @!visibility private def >=(other) if other.is_a?(Hash) return true if attached? && uid == other.uid return false unless value == other.value end configuration >= other end # @!visibility private def store(key, value) key = key.to_s new_config = to_h new_config[key] = value replace(new_config) value end # @!visibility private def delete(key) key = key.to_s new_config = to_h return yield(key) if block_given? && !new_config.key?(key) old_value = new_config.delete(key) replace(new_config) old_value end # # Replace the configuration with a new {::Hash}. # # @param [::Hash] new_config # @return [self] # def replace(new_config) @metadata = org.openhab.core.items.Metadata.new(uid, value, new_config.transform_keys(&:to_s)) commit self end # @!visibility private def inspect return to_h.inspect if value.empty? return value.inspect if configuration.empty? [value, to_h].inspect end # @return [org.openhab.core.common.registry.Provider, nil] def provider Provider.registry.provider_for(uid) end # # @raise [FrozenError] if the provider is not a # {org.openhab.core.common.registry.ManagedProvider ManagedProvider} that can be updated. # @return [org.openhab.core.common.registry.ManagedProvider] # def provider! preferred_provider = Provider.current( Thread.current[:openhab_providers]&.dig(:metadata_items, uid.item_name) || Thread.current[:openhab_providers]&.dig(:metadata_namespaces, uid.namespace), self ) if attached? provider = self.provider return preferred_provider unless provider unless provider.is_a?(ManagedProvider) raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}." end if preferred_provider != provider logger.warn("Provider #{preferred_provider.inspect} cannot be used with #{uid}; " \ "reverting to provider #{provider.inspect}. " \ "This may cause unexpected issues, like metadata persisting that you did not expect to.") preferred_provider = provider end end preferred_provider end private # # @see https://github.com/openhab/openhab-core/issues/3169 # # in the meantime, force the serialization round-trip right now # def javaify mapper = Provider.registry.managed_provider.get.storage.entityMapper @metadata = mapper.from_json(mapper.to_json_tree(@metadata), Metadata.java_class) end end |
Instance Method Details
#attached? ⇒ true, false
Is this object attached to an actual Item?
79 80 81 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 79 def attached? uid.item_name != "-" end |
#provider ⇒ org.openhab.core.common.registry.Provider?
224 225 226 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 224 def provider Provider.registry.provider_for(uid) end |
#provider! ⇒ org.openhab.core.common.registry.ManagedProvider
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 233 def provider! preferred_provider = Provider.current( Thread.current[:openhab_providers]&.dig(:metadata_items, uid.item_name) || Thread.current[:openhab_providers]&.dig(:metadata_namespaces, uid.namespace), self ) if attached? provider = self.provider return preferred_provider unless provider unless provider.is_a?(ManagedProvider) raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}." end if preferred_provider != provider logger.warn("Provider #{preferred_provider.inspect} cannot be used with #{uid}; " \ "reverting to provider #{provider.inspect}. " \ "This may cause unexpected issues, like metadata persisting that you did not expect to.") preferred_provider = provider end end preferred_provider end |
#replace(new_config) ⇒ self
Replace the configuration with a new Hash.
209 210 211 212 213 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 209 def replace(new_config) @metadata = org.openhab.core.items.Metadata.new(uid, value, new_config.transform_keys(&:to_s)) commit self end |