class Accessory::Accessors::SubscriptAccessor

Traverses into a specified key for an arbitrary container-object which supports the #[] and #[]= methods.

@param key [Object] the key to pass to the #[] and #[]= methods.

Aliases

Equivalents in Elixir's {hexdocs.pm/elixir/Access.html Access} module

Default constructor used by predecessor accessor

Usage Notes:

Subscripting into an Array will work, but may not have the results you expect:

# extends the Array
[].lens[3].put_in(1) # => [nil, nil, nil, 1]

# default-constructs a Hash, not an Array
[].lens[0][0].put_in(1) # => [{0=>1}]

Other accessors ({Accessors::FirstAccessor}, {Accessors::BetwixtAccessor}, etc.) may fit your expectations more closely for Array traversal.

Public Class Methods

new(key, **kwargs) click to toggle source

@!visibility private

Calls superclass method Accessory::Accessor::new
# File lib/accessory/accessors/subscript_accessor.rb, line 36
def initialize(key, **kwargs)
  super(**kwargs)
  @key = key
end

Public Instance Methods

ensure_valid(traversal_result) click to toggle source

@!visibility private

# File lib/accessory/accessors/subscript_accessor.rb, line 57
def ensure_valid(traversal_result)
  if traversal_result.respond_to?(:[])
    traversal_result
  else
    {}
  end
end
get(data) { |value| ... } click to toggle source

Finds data[@key], feeds it down the accessor chain, and returns the result. @param data [Enumerable] the Enumerable to index into @return [Object] the value derived from the rest of the accessor chain

# File lib/accessory/accessors/subscript_accessor.rb, line 74
def get(data)
  value = traverse_or_default(data)

  if block_given?
    yield(value)
  else
    value
  end
end
get_and_update(data) { |value| ... } click to toggle source

Finds data[@key], feeds it down the accessor chain, and overwrites data[@key] with the returned result.

If :pop is returned from the accessor chain, the key is instead deleted from the {data} with data.delete(@key). @param data [Enumerable] the Enumerable to index into @return [Array] a two-element array containing 1. the original value found; and 2. the result value from the accessor chain

# File lib/accessory/accessors/subscript_accessor.rb, line 91
def get_and_update(data)
  value = traverse_or_default(data)

  case yield(value)
  in [:clean, result, _]
    [:clean, result, data]
  in [:dirty, result, new_value]
    data[@key] = new_value
    [:dirty, result, data]
  in :pop
    data.delete(@key)
    [:dirty, value, data]
  end
end
inspect(format: :long) click to toggle source

@!visibility private

Calls superclass method Accessory::Accessor#inspect
# File lib/accessory/accessors/subscript_accessor.rb, line 42
def inspect(format: :long)
  case format
  when :long
    super()
  when :short
    @key.inspect
  end
end
inspect_args() click to toggle source

@!visibility private

# File lib/accessory/accessors/subscript_accessor.rb, line 52
def inspect_args
  @key.inspect
end
traverse(data) click to toggle source

@!visibility private

# File lib/accessory/accessors/subscript_accessor.rb, line 66
def traverse(data)
  data[@key]
end