Module: Collapsium::PathedAccess

Included in:
UberHash
Defined in:
lib/collapsium/pathed_access.rb

Overview

The PathedAccess module can be used to extend Hash with pathed access on top of regular access, i.e. instead of ‘h[“second”]` you can write `h`.

The main benefit is much simpler code for accessing nested structured. For any given path, PathedAccess will return nil from [] if any of the path components do not exist.

Similarly, intermediate nodes will be created when you write a value for a path.

Constant Summary collapse

READ_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Methods redefined to support pathed read access.

[
  :[], :default, :delete, :fetch, :has_key?, :include?, :key?, :member?,
].freeze
WRITE_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Methods redefined to support pathed write access.

[
  :[]=, :store,
].freeze
DEFAULT_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default path separator

'.'.freeze

Instance Method Summary collapse

Instance Method Details

#separatorString

Returns the separator is the character or pattern splitting paths.

Returns:

  • (String)

    the separator is the character or pattern splitting paths.



26
27
28
29
# File 'lib/collapsium/pathed_access.rb', line 26

def separator
  @separator ||= DEFAULT_SEPARATOR
  return @separator
end

#split_patternRegExp

Returns the pattern to split paths at; based on separator.

Returns:

  • (RegExp)

    the pattern to split paths at; based on separator



49
50
51
# File 'lib/collapsium/pathed_access.rb', line 49

def split_pattern
  /(?<!\\)#{Regexp.escape(separator)}/
end