Class: Philiprehberger::ConfigKit::Store
- Inherits:
-
Object
- Object
- Philiprehberger::ConfigKit::Store
- Defined in:
- lib/philiprehberger/config_kit/store.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#dig(*keys) ⇒ Object?
Walks nested hash/array values using Ruby’s standard
digsemantics. - #get(key) ⇒ Object (also: #[])
-
#initialize(yaml: nil, env: ENV, &block) ⇒ Store
constructor
A new instance of Store.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(yaml: nil, env: ENV, &block) ⇒ Store
Returns a new instance of Store.
8 9 10 11 12 13 14 15 16 |
# File 'lib/philiprehberger/config_kit/store.rb', line 8 def initialize(yaml: nil, env: ENV, &block) @schema = Schema.new @schema.instance_eval(&block) if block loader = Loader.new(@schema, yaml_path: yaml, env: env) @values = loader.load @values.freeze freeze end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
6 7 8 |
# File 'lib/philiprehberger/config_kit/store.rb', line 6 def schema @schema end |
Instance Method Details
#dig(*keys) ⇒ Object?
Walks nested hash/array values using Ruby’s standard dig semantics.
Fetches the top-level value via #get and, if additional keys are provided and the value responds to dig, delegates to value.dig(*rest). Returns nil when any intermediate key is missing.
33 34 35 36 37 38 39 40 41 |
# File 'lib/philiprehberger/config_kit/store.rb', line 33 def dig(*keys) raise ArgumentError, 'wrong number of arguments (given 0, expected 1+)' if keys.empty? first, *rest = keys value = get(first) return value if rest.empty? || value.nil? value.respond_to?(:dig) ? value.dig(*rest) : nil end |
#get(key) ⇒ Object Also known as: []
18 19 20 |
# File 'lib/philiprehberger/config_kit/store.rb', line 18 def get(key) @values[key.to_s.include?('.') ? key.to_s : key] end |
#key?(key) ⇒ Boolean
51 52 53 |
# File 'lib/philiprehberger/config_kit/store.rb', line 51 def key?(key) @values.key?(key.to_s.include?('.') ? key.to_s : key) end |
#keys ⇒ Object
47 48 49 |
# File 'lib/philiprehberger/config_kit/store.rb', line 47 def keys @values.keys end |
#to_h ⇒ Object
43 44 45 |
# File 'lib/philiprehberger/config_kit/store.rb', line 43 def to_h build_nested_hash end |