Module: JSI::BaseHash
- Includes:
- Hashlike
- Defined in:
- lib/jsi/base.rb
Overview
module extending a Base object when its schema instance is Hash-like (responds to #to_hash)
Constant Summary
Constants included from Hashlike
Hashlike::DESTRUCTIVE_METHODS, Hashlike::SAFE_KEY_ONLY_METHODS, Hashlike::SAFE_KEY_VALUE_METHODS, Hashlike::SAFE_METHODS
Instance Method Summary collapse
-
#[](property_name_) ⇒ JSI::Base, Object
The instance's subscript value at the given key property_name_.
-
#[]=(property_name, value) ⇒ Object
assigns the given property name of the instance to the given value.
-
#each {|Object, Object| ... } ⇒ self, Enumerator
yields each key and value of this JSI.
-
#to_hash ⇒ Hash
A hash in which each key is a key of the instance hash and each value is the result of selfkey.
Methods included from Hashlike
#inspect, #pretty_print, #to_s
Instance Method Details
#[](property_name_) ⇒ JSI::Base, Object
Returns the instance's subscript value at the given key property_name_. if there is a subschema defined for that property on this JSI's schema, returns the instance's subscript as a JSI instiation of that subschema.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/jsi/base.rb', line 369 def [](property_name_) memoize(:[], property_name_) do |property_name| begin property_schema = schema.subschema_for_property(property_name) property_schema = property_schema && property_schema.match_to_instance(instance[property_name]) if !instance.key?(property_name) && property_schema && property_schema.schema_object.key?('default') # use the default value default = property_schema.schema_object['default'] if default.respond_to?(:to_hash) || default.respond_to?(:to_ary) class_for_schema(property_schema).new(default, ancestor: @ancestor) else default end elsif property_schema && (instance[property_name].respond_to?(:to_hash) || instance[property_name].respond_to?(:to_ary)) class_for_schema(property_schema).new(instance[property_name], ancestor: @ancestor) else instance[property_name] end end end end |
#[]=(property_name, value) ⇒ Object
assigns the given property name of the instance to the given value. if the value is a JSI, its instance is assigned.
398 399 400 |
# File 'lib/jsi/base.rb', line 398 def []=(property_name, value) subscript_assign(property_name, value) end |
#each {|Object, Object| ... } ⇒ self, Enumerator
yields each key and value of this JSI. each yielded key is the same as a key of the instance, and each yielded value is the result of selfkey. returns an Enumerator if no block is given.
346 347 348 349 350 |
# File 'lib/jsi/base.rb', line 346 def each return to_enum(__method__) { instance.size } unless block_given? instance.each_key { |k| yield(k, self[k]) } self end |
#to_hash ⇒ Hash
Returns a hash in which each key is a key of the instance hash and each value is the result of selfkey.
354 355 356 |
# File 'lib/jsi/base.rb', line 354 def to_hash inject({}) { |h, (k, v)| h[k] = v; h } end |