Sha256: d1b9a4667e55f27876f1716a614dbc72e322ef611993d119e60671b3db60a43d

Contents?: true

Size: 2 KB

Versions: 6

Compression:

Stored size: 2 KB

Contents

module Cmds
  # hash of common default values used in method options.
  # 
  # don't use them directly -- use {Cmds.defaults}.
  # 
  # the values themselves are frozen so we don't have to worry about cloning
  # them before providing them for use.
  # 
  # the constant Hash itself is **not** frozen -- you can mutate this to
  # change the default options for **ALL** Cmds method calls...
  # just be aware of what you're doing. not recommended
  # outside of quick hacks and small scripts since other pieces and parts
  # you don't even know about may depend on said behavior.
  # 
  DEFAULTS = {
    # positional arguments for a command
    args: [],
    
    # keyword arguments for a command
    kwds: {},
    
    # how to format a command string for execution
    format: :squish,
    
    # what to do with array option values
    array_mode: :join,
    
    # what to join array option values with when using `array_mode = :join`
    array_join_string: ',',
    
    # what to do with false array values
    false_mode: :omit,
  }.map {|k, v| [k, v.freeze]}.to_h
  
  # merge an method call options hash with common defaults for the module.
  # 
  # this makes it easy to use the same defaults in many different methods
  # without repeating the declarations everywhere.
  # 
  # @param [Hash] opts
  #   hash of overrides provided by method caller.
  # 
  # @param [Array<Symbol>, '*'] keys
  #   keys for the defaults you want to use.
  # 
  # @param [Hash<Symbol, Object>] extras
  #   extra keys and values to add to the returned defaults.
  # 
  # @return [Hash<Symbol, Object>]
  #   defaults to use in the method call.
  #   
  def self.defaults opts, keys = '*', extras = {}
    if keys == '*'
      DEFAULTS
    else      
      keys.
        map {|key|
          [key, DEFAULTS.fetch(key)]
        }.
        to_h
    end.
      merge!(extras).
      merge!(opts)
  end
  
  # proxy through to class method {Cmds.defaults}.
  # 
  def defaults opts, keys = '*', extras = {}
    self.class.defaults opts, keys, extras
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cmds-0.1.5 lib/cmds/util/defaults.rb
cmds-0.1.4 lib/cmds/util/defaults.rb
cmds-0.1.3 lib/cmds/util/defaults.rb
cmds-0.1.2 lib/cmds/util/defaults.rb
cmds-0.1.1 lib/cmds/util/defaults.rb
cmds-0.1.0 lib/cmds/util/defaults.rb