Module: Flatten

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

Overview

Provides smash-key access to a Hash.

‘foo’=>{‘bar’=>‘bingo’}.smash #=> ‘foo‘foo.bar’=>‘bingo’ ‘foo‘foo.bar’=>‘bingo’.unsmash => ‘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 =
"0.1.1"

Instance Method Summary collapse

Methods included from GuardMethods

extended, included

Methods included from UtilityMethods

expand, expand!

Instance Method Details

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

Returns a smash version of self using the options provided.

Parameters:

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

Options Hash (options):

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

    truthy values will cause arrays to be smashd 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/flatten.rb', line 28

def smash(options = {})
  Flatten.smash(self, options)
end

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

Replaces self with smash version of itself.

Parameters:

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

Returns:

  • (Hash<String,Object>)


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

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

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

Used internally by both Flatten::Utility#smash and Flatten::Utility#unsmash

Overloads:

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

    This method returns an undefined value.

    Yields once per key in smash version of itself.

    Yield Parameters:

    • ((smash_key,value))
  • #smash_each(options = {}) ⇒ Enumerator<(smash_key,value)>

    Returns:

    • (Enumerator<(smash_key,value)>)


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

def smash_each(options = {}, &block)
  Flatten.smash_each(self, options, &block)
end

#smash_fetch(smash_key, options = {}) ⇒ Object #smash_fetch(smash_key, default, options = {}) ⇒ default #smash_fetch(smash_key, options = {}) { ... } ⇒ Object

Follows semantics of Hash#fetch

Overloads:

  • #smash_fetch(smash_key, options = {}) ⇒ Object

    Returns:

    • (Object)

    Raises:

    • (KeyError)

      if smash_key not found 

  • #smash_fetch(smash_key, default, options = {}) ⇒ default

    Parameters:

    • default (Object)

      the default object

    Returns:

    • (default)
  • #smash_fetch(smash_key, options = {}) { ... } ⇒ Object

    Returns that which was returned by the given block.

    Yields:

    • if smash_key not founs

    Returns:

    • (Object)

      that which was returned by the given block.



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

def smash_fetch(*args, &block)
  Flatten.smash_fetch(self, *args, &block)
end

#smash_get(smash_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/flatten.rb', line 78

def smash_get(*args)
  Flatten.smash_get(self, *args)
end

#unsmash(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/flatten.rb', line 86

def unsmash(options = {})
  Flatten.unsmash(self, options)
end

#unsmash!(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/flatten.rb', line 94

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