Class: Philiprehberger::ConfigKit::Store
- Inherits:
-
Object
- Object
- Philiprehberger::ConfigKit::Store
- Includes:
- Enumerable
- 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. -
#each {|key, value| ... } ⇒ self, Enumerator
Yield each ‘[key, value]` pair in declaration order.
-
#fetch(key, *args, &block) ⇒ Object
Hash-like fetch with default fallback, block fallback, or KeyError.
- #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.
10 11 12 13 14 15 16 17 18 |
# File 'lib/philiprehberger/config_kit/store.rb', line 10 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.
8 9 10 |
# File 'lib/philiprehberger/config_kit/store.rb', line 8 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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/philiprehberger/config_kit/store.rb', line 69 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 |
#each {|key, value| ... } ⇒ self, Enumerator
Yield each ‘[key, value]` pair in declaration order.
Returns an ‘Enumerator` if no block is given, enabling `map`, `select`, `to_a`, and other `Enumerable` methods to flow through.
53 54 55 56 57 58 |
# File 'lib/philiprehberger/config_kit/store.rb', line 53 def each(&block) return @values.each unless block @values.each(&block) self end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key, &block) ⇒ Object
Hash-like fetch with default fallback, block fallback, or KeyError.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/philiprehberger/config_kit/store.rb', line 35 def fetch(key, *args, &block) raise ArgumentError, "wrong number of arguments (given #{args.length + 1}, expected 1..2)" if args.length > 1 normalized = key.to_s.include?('.') ? key.to_s : key return @values[normalized] if @values.key?(normalized) return args.first if args.length == 1 return block.call(key) if block raise KeyError, "key not found: #{key.inspect}" end |
#get(key) ⇒ Object Also known as: []
20 21 22 |
# File 'lib/philiprehberger/config_kit/store.rb', line 20 def get(key) @values[key.to_s.include?('.') ? key.to_s : key] end |
#key?(key) ⇒ Boolean
87 88 89 |
# File 'lib/philiprehberger/config_kit/store.rb', line 87 def key?(key) @values.key?(key.to_s.include?('.') ? key.to_s : key) end |
#keys ⇒ Object
83 84 85 |
# File 'lib/philiprehberger/config_kit/store.rb', line 83 def keys @values.keys end |
#to_h ⇒ Object
79 80 81 |
# File 'lib/philiprehberger/config_kit/store.rb', line 79 def to_h build_nested_hash end |