Class: Isomorphic::Node::Internal::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorphic/node.rb

Overview

Implements a tree of hashes, where retrieval is from the bottom-up.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, inflector = nil) ⇒ Scope

Default constructor.

Parameters:



391
392
393
394
395
396
397
# File 'lib/isomorphic/node.rb', line 391

def initialize(parent, inflector = nil)
  super()

  @parent, @inflector = parent, inflector

  @value_by_namespace_and_key = ActiveSupport::HashWithIndifferentAccess.new
end

Instance Attribute Details

#inflectorIsomorphic::Inflector::AbstractInflector (readonly)

Returns the inflector.

Returns:



401
402
403
404
405
406
407
408
409
# File 'lib/isomorphic/node.rb', line 401

%i(inflector).each do |method_name|
  define_method(method_name) do
    if parent.is_a?(Isomorphic::Node::Internal::Scope)
      parent.send(method_name)
    else
      instance_variable_get(:"@#{method_name}")
    end
  end
end

#parentObject (readonly)

Returns the value of attribute parent.



385
386
387
# File 'lib/isomorphic/node.rb', line 385

def parent
  @parent
end

Instance Method Details

#child(namespace) ⇒ Isomorphic::Node::Internal::Scope

Build a new child scope for the given namespace.

Parameters:

  • namespace (#to_s)

    the namespace name

Returns:



415
416
417
418
419
420
421
# File 'lib/isomorphic/node.rb', line 415

def child(namespace)
  scope = self.class.new(self)

  scope.instance_variable_get(:@value_by_namespace_and_key).send(:[]=, namespace, inflector.convert_hash({}))

  scope
end

#get(namespace, key) ⇒ Object?

Get the scoped value of the given key in the context of the given namespace.

Parameters:

  • namespace (#to_s)

    the namespace name

  • key (#to_s)

    the key

Returns:

  • (Object, nil)

    the value or nil if the namespace or key are not defined



428
429
430
431
432
433
434
435
436
# File 'lib/isomorphic/node.rb', line 428

def get(namespace, key)
  if @value_by_namespace_and_key.key?(namespace) && @value_by_namespace_and_key[namespace].key?(key)
    @value_by_namespace_and_key[namespace][key]
  elsif @parent.is_a?(self.class)
    @parent.get(namespace, key)
  else
    nil
  end
end

#keys(namespace) ⇒ Array<String>

The keys for the given namespace.

Parameters:

  • namespace (#to_s)

    the namespace name

Returns:

  • (Array<String>)

    the keys or nil if the namespace is not defined



442
443
444
445
446
447
448
# File 'lib/isomorphic/node.rb', line 442

def keys(namespace)
  if @value_by_namespace_and_key.key?(namespace)
    @value_by_namespace_and_key[namespace].keys
  else
    nil
  end
end

#namespacesArray<String>

The namespaces.

Returns:

  • (Array<String>)

    the namespace names



453
454
455
# File 'lib/isomorphic/node.rb', line 453

def namespaces
  @value_by_namespace_and_key.keys
end

#set(namespace, key, value) ⇒ Object?

Set the scoped value of the given key in the context of the given namespace.

Parameters:

  • namespace (#to_s)

    the namespace name

  • key (#to_s)

    the key

  • value (Object)

    the value

Returns:

  • (Object, nil)

    the value or nil if the namespace is not defined



463
464
465
466
467
468
469
470
471
# File 'lib/isomorphic/node.rb', line 463

def set(namespace, key, value)
  if @value_by_namespace_and_key.key?(namespace)
    @value_by_namespace_and_key[namespace][key] = value
  elsif @parent.is_a?(self.class)
    @parent.set(namespace, key, value)
  else
    nil
  end
end

#values(namespace) ⇒ Array<Object>?

The values for the given namespace.

Parameters:

  • namespace (#to_s)

    the namespace name

Returns:

  • (Array<Object>, nil)

    the values or nil if the namespace is not defined



477
478
479
480
481
482
# File 'lib/isomorphic/node.rb', line 477

def values(namespace)
  keys(namespace).try(:inject, inflector.convert_hash({})) { |acc, key|
    acc[key] = get(namespace, key)
    acc
  }
end