Class: OpenHAB::Core::Items::Metadata::Hash
- Inherits:
-
Object
- Object
- OpenHAB::Core::Items::Metadata::Hash
- Includes:
- Enumerable
- 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.
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.ManagedProvider
-
#replace(new_config) ⇒ self
Replace the configuration with a new Hash.
-
#to_hash ⇒ ::Hash
Implicit conversion to Hash.
Methods included from Enumerable
#all_groups, #all_members, #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, #up
Instance Attribute Details
#item ⇒ Object (readonly)
115 116 117 118 119 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 115 def item return nil unless attached? DSL.items[uid.item_name] end |
#namespace ⇒ String (readonly)
21 22 23 24 25 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 21 class Hash java_import org.openhab.core.items.Metadata private_constant :Metadata include Enumerable extend Forwardable def_delegators :@metadata, :configuration, :hash, :to_s, :uid, :value private :configuration # @!method to_hash # Implicit conversion to {::Hash}. # @return [::Hash] # Make it act like a Hash; some methods can be handled by # java.util.Map, others we have to convert to a Ruby Hash first, and # still others (mutators) must be manually implemented below. def_delegators :configuration, :any?, :compact, :compare_by_identity?, :deconstruct_keys, :default, :default_proc, :each, :each_key, :each_pair, :each_value, :empty?, :filter, :flatten, :has_value?, :invert, :key, :keys, :length, :rassoc, :reject, :select, :shift, :size, :to_a, :to_h, :to_hash, :transform_keys, :transform_values, :values, :value? def_delegator :uid, :namespace 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 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 [](key) configuration[key.to_s] end # @!visibility private def []=(key, value) key = key.to_s new_config = to_h new_config[key] = value replace(new_config) value # rubocop:disable Lint/Void end alias_method :store, :[]= # @!visibility private def assoc(key) configuration.assoc(key.to_s) end # @!visibility private def clear replace({}) end # @!visibility private def compact! replace(compact) end # @!visibility private def compare_by_identity raise NotImplementedError end # @!visibility private def default=(*) raise NotImplementedError end # @!visibility private def default_proc=(*) raise NotImplementedError 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 # @!visibility private def delete_if(&block) raise NotImplementedError unless block replace(to_h.delete_if(block)) end # @!visibility private def dig(key, *keys) configuration.dig(key.to_s, *keys) end # @!visibility private def except(*keys) to_h.except(*keys.map(&:to_s)) end # @!visibility private def fetch(key, &block) configuration.fetch(key.to_s, &block) end # @!visibility private def fetch_values(*keys, &block) configuration.fetch_values(*keys.map(&:to_s), &block) end # @!visibility private def keep_if(&block) select!(&block) self end # @!visibility private def key?(key) configuration.key?(key.to_s) end alias_method :include?, :key? alias_method :has_key?, :key? alias_method :member?, :key? # @!visibility private def merge!(*others, &block) return self if others.empty? new_config = to_h others.each do |h| new_config.merge!(h.transform_keys(&:to_s), &block) end replace(new_config) end alias_method :update, :merge! # @!visibility private def reject!(&block) raise NotImplementedError unless block r = to_h.reject!(&block) replace(r) if r 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 select!(&block) raise NotImplementedError unless block? r = to_h.select!(&block) replace(r) if r end alias_method :filter!, :select! # @!visibility private def slice(*keys) configuration.slice(*keys.map(&:to_s)) end # @!visibility private def to_proc ->(k) { self[k] } end # @!visibility private def transform_keys!(*args, &block) replace(transform_keys(*args, &block)) end # @!visibility private def transform_values!(&block) raise NotImplementedError unless block replace(transform_values(&block)) end # @!visibility private def values_at(*keys) configuration.values_at(*keys.map(&:to_s)) end # @!visibility private def inspect return to_h.inspect if value.empty? return value.inspect if configuration.empty? [value, to_h].inspect end remove_method :to_s alias_method :to_s, :inspect # # @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 = Provider.registry.provider_for(uid) return preferred_provider unless provider unless provider.is_a?(org.openhab.core.common.registry.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.
21 22 23 24 25 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 21 class Hash java_import org.openhab.core.items.Metadata private_constant :Metadata include Enumerable extend Forwardable def_delegators :@metadata, :configuration, :hash, :to_s, :uid, :value private :configuration # @!method to_hash # Implicit conversion to {::Hash}. # @return [::Hash] # Make it act like a Hash; some methods can be handled by # java.util.Map, others we have to convert to a Ruby Hash first, and # still others (mutators) must be manually implemented below. def_delegators :configuration, :any?, :compact, :compare_by_identity?, :deconstruct_keys, :default, :default_proc, :each, :each_key, :each_pair, :each_value, :empty?, :filter, :flatten, :has_value?, :invert, :key, :keys, :length, :rassoc, :reject, :select, :shift, :size, :to_a, :to_h, :to_hash, :transform_keys, :transform_values, :values, :value? def_delegator :uid, :namespace 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 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 [](key) configuration[key.to_s] end # @!visibility private def []=(key, value) key = key.to_s new_config = to_h new_config[key] = value replace(new_config) value # rubocop:disable Lint/Void end alias_method :store, :[]= # @!visibility private def assoc(key) configuration.assoc(key.to_s) end # @!visibility private def clear replace({}) end # @!visibility private def compact! replace(compact) end # @!visibility private def compare_by_identity raise NotImplementedError end # @!visibility private def default=(*) raise NotImplementedError end # @!visibility private def default_proc=(*) raise NotImplementedError 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 # @!visibility private def delete_if(&block) raise NotImplementedError unless block replace(to_h.delete_if(block)) end # @!visibility private def dig(key, *keys) configuration.dig(key.to_s, *keys) end # @!visibility private def except(*keys) to_h.except(*keys.map(&:to_s)) end # @!visibility private def fetch(key, &block) configuration.fetch(key.to_s, &block) end # @!visibility private def fetch_values(*keys, &block) configuration.fetch_values(*keys.map(&:to_s), &block) end # @!visibility private def keep_if(&block) select!(&block) self end # @!visibility private def key?(key) configuration.key?(key.to_s) end alias_method :include?, :key? alias_method :has_key?, :key? alias_method :member?, :key? # @!visibility private def merge!(*others, &block) return self if others.empty? new_config = to_h others.each do |h| new_config.merge!(h.transform_keys(&:to_s), &block) end replace(new_config) end alias_method :update, :merge! # @!visibility private def reject!(&block) raise NotImplementedError unless block r = to_h.reject!(&block) replace(r) if r 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 select!(&block) raise NotImplementedError unless block? r = to_h.select!(&block) replace(r) if r end alias_method :filter!, :select! # @!visibility private def slice(*keys) configuration.slice(*keys.map(&:to_s)) end # @!visibility private def to_proc ->(k) { self[k] } end # @!visibility private def transform_keys!(*args, &block) replace(transform_keys(*args, &block)) end # @!visibility private def transform_values!(&block) raise NotImplementedError unless block replace(transform_values(&block)) end # @!visibility private def values_at(*keys) configuration.values_at(*keys.map(&:to_s)) end # @!visibility private def inspect return to_h.inspect if value.empty? return value.inspect if configuration.empty? [value, to_h].inspect end remove_method :to_s alias_method :to_s, :inspect # # @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 = Provider.registry.provider_for(uid) return preferred_provider unless provider unless provider.is_a?(org.openhab.core.common.registry.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?
109 110 111 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 109 def attached? uid.item_name != "-" end |
#provider ⇒ org.openhab.core.common.registry.ManagedProvider
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 391 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 = Provider.registry.provider_for(uid) return preferred_provider unless provider unless provider.is_a?(org.openhab.core.common.registry.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.
334 335 336 337 338 |
# File 'lib/openhab/core/items/metadata/hash.rb', line 334 def replace(new_config) @metadata = org.openhab.core.items.Metadata.new(uid, value, new_config.transform_keys(&:to_s)) commit self end |
#to_hash ⇒ ::Hash
Implicit conversion to Hash.
|
# File 'lib/openhab/core/items/metadata/hash.rb', line 31
|