Sha256: 6c9424aa0cd9be028a7f0713cc651cc3385db5aef1f0bd671c4eec2e255abf20

Contents?: true

Size: 2 KB

Versions: 11

Compression:

Stored size: 2 KB

Contents

# encoding: UTF-8
# frozen_string_literal: true

# Requirements
# =======================================================================

# Stdlib
# -----------------------------------------------------------------------

# Deps
# -----------------------------------------------------------------------

# Project / Package
# -----------------------------------------------------------------------


# Refinements
# =======================================================================



# Definitions
# =======================================================================


# @todo document Locd::Config::Base class.
class Locd::Config::Base
  
  # Constants
  # ======================================================================
  
  
  # Class Methods
  # ======================================================================
  
  
  # Attributes
  # ======================================================================
  
  
  # TODO document `key_separator` attribute.
  # 
  # @return [attr_type]
  #     
  attr_reader :key_separator
  
  
  # Constructor
  # ======================================================================
  
  # Instantiate a new `Locd::Config::Base`.
  def initialize source:, parent: nil, key_separator: '.'
    @source = source
    @parent = parent
    @key_separator = key_separator
  end # #initialize
  
  
  # Instance Methods
  # ======================================================================
  
  def key_path_for *key
    key.flat_map { |k| k.to_s.split key_separator }
  end
  
  
  def dig *key_path
    if key_path.length == 1
      if source.key? key_path[0]
        return source[key_path[0]]
      end
    else
      just_before = source.dig key_path[0...-1]
      if  just_before.respond_to?( :key? ) &&
          just_before.key? key_path[-1]
        return just_before[key_path[-1]]
      end
    end
    
    if parent
      parent.dig key_path
    else
      nil
    end
  end
  
  
  def get *key
    dig key_path_for( *key )
  end
  
  
end # class Locd::Config::Base

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
locd-0.1.13 lib/locd/config/base.rb
locd-0.1.12 lib/locd/config/base.rb
locd-0.1.11 lib/locd/config/base.rb
locd-0.1.10 lib/locd/config/base.rb
locd-0.1.9 lib/locd/config/base.rb
locd-0.1.8 lib/locd/config/base.rb
locd-0.1.7 lib/locd/config/base.rb
locd-0.1.6 lib/locd/config/base.rb
locd-0.1.5 lib/locd/config/base.rb
locd-0.1.4 lib/locd/config/base.rb
locd-0.1.3 lib/locd/config/base.rb