Class: TypedParams::NamespacedSet
- Inherits:
-
Object
- Object
- TypedParams::NamespacedSet
- Defined in:
- lib/typed_params/namespaced_set.rb
Overview
NamespacedSet is a set of key-values that are namespaced by a class. What makes this special is that access supports class inheritance. For example, given a Parent class and a Child class that inherits from Parent, the Child namespace can access the Parent namespace as long as a Child namespace doesn’t also exist, in which case it will take precedence.
For example, the above, codified:
class Parent; end
class Child < Parent; end
s = NamespacedSet.new
s[Parent, :foo] = :bar
s[Parent, :foo] => :bar
s[Child, :foo] => :bar
s[Child, :baz] = :qux
s[Parent, :baz] => nil
s[Child, :baz] => :qux
Direct Known Subclasses
Instance Method Summary collapse
- #[](namespace, key) ⇒ Object
- #[]=(namespace, key, value) ⇒ Object
- #exists?(namespace, key) ⇒ Boolean
-
#initialize ⇒ NamespacedSet
constructor
A new instance of NamespacedSet.
Constructor Details
#initialize ⇒ NamespacedSet
Returns a new instance of NamespacedSet.
29 |
# File 'lib/typed_params/namespaced_set.rb', line 29 def initialize = @store = {} |
Instance Method Details
#[](namespace, key) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/typed_params/namespaced_set.rb', line 35 def [](namespace, key) _, data = store.find { |k, _| k == namespace } || store.find { |k, _| namespace <= k } return nil if data.nil? data[key] end |
#[]=(namespace, key, value) ⇒ Object
31 32 33 |
# File 'lib/typed_params/namespaced_set.rb', line 31 def []=(namespace, key, value) store.deep_merge!(namespace => { key => value }) end |
#exists?(namespace, key) ⇒ Boolean
45 46 47 48 49 50 51 52 53 |
# File 'lib/typed_params/namespaced_set.rb', line 45 def exists?(namespace, key) _, data = store.find { |k, _| k == namespace } || store.find { |k, _| namespace <= k } return false if data.nil? data.key?(key) end |