Class: Isomorphic::Node::Internal::Scope
- Inherits:
-
Object
- Object
- Isomorphic::Node::Internal::Scope
- Defined in:
- lib/isomorphic/node.rb
Overview
Implements a tree of hashes, where retrieval is from the bottom-up.
Instance Attribute Summary collapse
-
#inflector ⇒ Isomorphic::Inflector::AbstractInflector
readonly
The inflector.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#child(namespace) ⇒ Isomorphic::Node::Internal::Scope
Build a new child scope for the given namespace.
-
#get(namespace, key) ⇒ Object?
Get the scoped value of the given key in the context of the given namespace.
-
#initialize(parent, inflector = nil) ⇒ Scope
constructor
Default constructor.
-
#keys(namespace) ⇒ Array<String>
The keys for the given namespace.
-
#namespaces ⇒ Array<String>
The namespaces.
-
#set(namespace, key, value) ⇒ Object?
Set the scoped value of the given key in the context of the given namespace.
-
#values(namespace) ⇒ Array<Object>?
The values for the given namespace.
Constructor Details
#initialize(parent, inflector = nil) ⇒ Scope
Default constructor.
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
#inflector ⇒ Isomorphic::Inflector::AbstractInflector (readonly)
Returns the inflector.
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 |
#parent ⇒ Object (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.
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.
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.
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 |
#namespaces ⇒ Array<String>
The namespaces.
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.
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.
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 |