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
-
{Access.subscript}
-
{Access::FluentHelpers#subscript} (included in {Lens} and {BoundLens})
-
{Access::FluentHelpers#[]} (included in {Lens} and {BoundLens})
-
just passing a
key
will also work, when +not(key.kind_of?(Accessor))+ (this is a special case in {Lens#initialize})
Equivalents in Elixir's {hexdocs.pm/elixir/Access.html Access
} module
-
{hexdocs.pm/elixir/Access.html#at/1
Access.at/1
} -
{hexdocs.pm/elixir/Access.html#key/2
Access.key/2
}
Default constructor used by predecessor accessor
-
Hash.new
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
@!visibility private
Accessory::Accessor::new
# File lib/accessory/accessors/subscript_accessor.rb, line 36 def initialize(key, **kwargs) super(**kwargs) @key = key end
Public Instance Methods
@!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
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
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
@!visibility private
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
@!visibility private
# File lib/accessory/accessors/subscript_accessor.rb, line 52 def inspect_args @key.inspect end
@!visibility private
# File lib/accessory/accessors/subscript_accessor.rb, line 66 def traverse(data) data[@key] end