Module: Sparsify

Extended by:
GuardMethods, UtilityMethods
Defined in:
lib/sparsify.rb,
lib/sparsify/version.rb,
lib/sparsify/separator.rb,
lib/sparsify/deprecations.rb,
lib/sparsify/guard_methods.rb,
lib/sparsify/utility_methods.rb

Overview

Provides sparse-key access to a Hash.

‘foo’=>{‘bar’=>‘bingo’}.sparse #=> ‘foo‘foo.bar’=>‘bingo’ ‘foo‘foo.bar’=>‘bingo’.unsparse => ‘foo’=>{‘bar’=>‘bingo’}

Defined Under Namespace

Modules: Deprecations, GuardMethods, UtilityMethods Classes: Separator

Constant Summary collapse

DEFAULT_SEPARATOR =

The default separator, used if not specified in command’s options hash.

'.'.freeze
VERSION =
'1.1.0'

Instance Method Summary collapse

Methods included from GuardMethods

extended, included

Methods included from UtilityMethods

expand, expand!

Instance Method Details

#sparse(options = {}) ⇒ Hash<String,Object>

Returns a sparse version of self using the options provided.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

Options Hash (options):

  • :separator (String)
  • :prefix (String)
  • :sparse_array (Boolean, :zero_pad) — default: false

    truthy values will cause arrays to be sparsed by index and decended into. :zero_pad causes indexes to be zero-padded to make them sort lexically.

Returns:

  • (Hash<String,Object>)


28
29
30
# File 'lib/sparsify.rb', line 28

def sparse(options = {})
  Sparsify.sparse(self, options)
end

#sparse!(options = {}) ⇒ Hash<String,Object>

Replaces self with sparse version of itself.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

Returns:

  • (Hash<String,Object>)


36
37
38
# File 'lib/sparsify.rb', line 36

def sparse!(options = {})
  self.replace(sparse, options)
end

#sparse_each(options = {}) {|| ... } ⇒ void #sparse_each(options = {}) ⇒ Enumerator<(sparse_key,value)>

Used internally by both Sparsify::Utility#sparse and Sparsify::Utility#unsparse

Overloads:

  • #sparse_each(options = {}) {|| ... } ⇒ void

    This method returns an undefined value.

    Yields once per key in sparse version of itself.

    Yield Parameters:

    • ((sparse_key,value))
  • #sparse_each(options = {}) ⇒ Enumerator<(sparse_key,value)>

    Returns:

    • (Enumerator<(sparse_key,value)>)


51
52
53
# File 'lib/sparsify.rb', line 51

def sparse_each(options = {}, &block)
  Sparsify.sparse_each(self, options, &block)
end

#sparse_fetch(sparse_key, options = {}) ⇒ Object #sparse_fetch(sparse_key, default, options = {}) ⇒ default #sparse_fetch(sparse_key, options = {}) { ... } ⇒ Object

Follows semantics of Hash#fetch

Overloads:

  • #sparse_fetch(sparse_key, options = {}) ⇒ Object

    Returns:

    • (Object)

    Raises:

    • (KeyError)

      if sparse_key not found 

  • #sparse_fetch(sparse_key, default, options = {}) ⇒ default

    Parameters:

    • default (Object)

      the default object

    Returns:

    • (default)
  • #sparse_fetch(sparse_key, options = {}) { ... } ⇒ Object

    Returns that which was returned by the given block.

    Yields:

    • if sparse_key not founs

    Returns:

    • (Object)

      that which was returned by the given block.



69
70
71
# File 'lib/sparsify.rb', line 69

def sparse_fetch(*args, &block)
  Sparsify.sparse_fetch(self, *args, &block)
end

#sparse_get(sparse_key, options = {}) ⇒ Object

Follows semantics of Hash#[] without support for Hash#default_proc

Returns at that address or nil if none found.

Returns:

  • (Object)

    at that address or nil if none found



78
79
80
# File 'lib/sparsify.rb', line 78

def sparse_get(*args)
  Sparsify.sparse_get(self, *args)
end

#unsparse(options = {}) ⇒ Hash<String,Object>

Returns a deeply-nested hash version of self.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

Returns:

  • (Hash<String,Object>)


86
87
88
# File 'lib/sparsify.rb', line 86

def unsparse(options = {})
  Sparsify.unsparse(self, options)
end

#unsparse!(options = {}) ⇒ Hash<String,Object>

Replaces self with deeply-nested version of self.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

Returns:

  • (Hash<String,Object>)


94
95
96
# File 'lib/sparsify.rb', line 94

def unsparse!(options = {})
  self.replace(unsparse, options)
end