class Accessory::Accessors::FirstAccessor
Traverses into the “first” element within an Enumerable
, using #first
.
This accessor can be preferable to {Accessors::SubscriptAccessor} for objects that are not subscriptable, e.g. Range
.
Aliases
-
{Access.first}
-
{Access::FluentHelpers#first} (included in {Lens} and {BoundLens})
Default constructor used by predecessor accessor
-
Array.new
Public Instance Methods
@!visibility private
# File lib/accessory/accessors/first_accessor.rb, line 20 def ensure_valid(traversal_result) if traversal_result.kind_of?(Enumerable) traversal_result else [] end end
Feeds data.first
down the accessor chain, returning the result. @param data [Object] the object to traverse @return [Object] the value derived from the rest of the accessor chain
# File lib/accessory/accessors/first_accessor.rb, line 39 def get(data) value = traverse_or_default(data) if block_given? yield(value) else value end end
Finds data.first
, feeds it down the accessor chain, and overwrites the stored value with the returned result.
If :pop
is returned from the accessor chain, the stored value will be removed using data.delete_at(0)
.
@param data [Object] the object to traverse @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/first_accessor.rb, line 57 def get_and_update(data) old_value = traverse_or_default(data) case yield(old_value) in [:clean, result, _] [:clean, result, data] in [:dirty, result, new_value] if data.respond_to?(:"first=") data.first = new_value else data[0] = new_value end [:dirty, result, data] in :pop data.delete_at(0) [:dirty, old_value, data] end end
@!visibility private
# File lib/accessory/accessors/first_accessor.rb, line 29 def inspect_args; nil; end
@!visibility private
# File lib/accessory/accessors/first_accessor.rb, line 32 def traverse(data) data.first end