Module: JSI::BaseHash

Includes:
PathedHashNode
Defined in:
lib/jsi/base.rb

Overview

module extending a Base object when its 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

Methods included from PathedHashNode

#each, #node_content_hash_pubsend, #to_hash

Methods included from Hashlike

#inspect, #merge, #pretty_print, #to_s, #update

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.

Parameters:

  • property_name (String, Object)

    the property name to subscript

Returns:

  • (JSI::Base, Object)

    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.



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
# File 'lib/jsi/base.rb', line 370

def [](property_name)
  instance_property_key_ = jsi_instance_hash_pubsend(:key?, property_name)
  if !instance_property_key_
    deref do |deref_jsi|
      return deref_jsi[property_name]
    end
  end
  instance_property_value_ = jsi_instance_sub(property_name)
  memoize(:[], property_name, instance_property_value_, instance_property_key_) do |property_name_, instance_property_value, instance_property_key|
    begin
      property_schema = schema.subschema_for_property(property_name_)
      property_schema = property_schema && property_schema.match_to_instance(instance_property_value)

      if !instance_property_key && 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)
          # we are using #dup so that we get a modified copy of self, in which we set dup[property_name_]=default.
          # this avoids duplication of code with #modified_copy and below in #[] to handle pathing and such.
          dup.tap { |o| o[property_name_] = default }[property_name_]
        else
          default
        end
      elsif property_schema && (instance_property_value.respond_to?(:to_hash) || instance_property_value.respond_to?(:to_ary))
        class_for_schema(property_schema).new(Base::NOINSTANCE, jsi_document: @jsi_document, jsi_ptr: @jsi_ptr[property_name_], ancestor_jsi: @ancestor_jsi || self)
      else
        instance_property_value
      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.

Parameters:

  • property_name (Object)

    this should generally be a String, but JSI does not enforce any constraint on it.

  • value (Object)

    the value to be assigned to the given subscript property_name



408
409
410
# File 'lib/jsi/base.rb', line 408

def []=(property_name, value)
  subscript_assign(property_name, value)
end