Module: Accessory::Access::FluentHelpers
Overview
A set of convenient “fluent API” builder methods that get mixed into Lens and BoundLens.
These do the same thing as the Accessory::Access helper of the same name, but wrap the resulting accessor in a call to #then
, deriving a new Lens or BoundLens from the addition of the accessor.
Instance Method Summary collapse
-
#[] ⇒ Object
Alias for #subscript.
-
#after_last ⇒ Object
Alias for #betwixt(-1).
-
#all ⇒ Object
Traverses all elements of an
Enumerable
. -
#attr ⇒ Object
Traverses an abstract “attribute” of an arbitrary object, represented by a named getter/setter method pair.
-
#before_first ⇒ Object
Alias for #betwixt(0).
-
#between_each ⇒ Object
Traverses the positions “between” the elements of an
Enumerable
, including the positions at the “edges” (i.e. before the first, and after the last.). -
#betwixt ⇒ Object
Traverses into a specified cursor-position “between” two elements of an
Enumerable
, including the positions at the “edges” (i.e. before the first, or after the last.). -
#filter(&pred) ⇒ Object
Traverses the elements of an
Enumerable
that return a truthy value from a passed-in predicate block. -
#first ⇒ Object
Traverses into the “first” element within an
Enumerable
, using#first
. -
#ivar ⇒ Object
Traverses into a named instance-variable of an arbitrary object.
-
#last ⇒ Object
Traverses into the “last” element within an
Enumerable
, using#last
. -
#subscript ⇒ Object
Traverses into a specified
key
for an arbitrary container-object which supports the#[]
and#[]=
methods.
Instance Method Details
#[] ⇒ Object
Alias for #subscript
92 93 94 |
# File 'lib/accessory/access.rb', line 92 def [](...) self.then(Accessory::Accessors::SubscriptAccessor.new(...)) end |
#after_last ⇒ Object
Alias for #betwixt(-1). See #betwixt
117 118 119 |
# File 'lib/accessory/access.rb', line 117 def after_last self.betwixt(-1) end |
#all ⇒ Object
Traverses all elements of an Enumerable
.
Aliases
Equivalents in Elixir’s Access
module
Default constructor used by predecessor accessor
-
Array.new
127 128 129 |
# File 'lib/accessory/access.rb', line 127 def all self.then(Accessory::Accessors::AllAccessor.new) end |
#attr ⇒ Object
Traverses an abstract “attribute” of an arbitrary object, represented by a named getter/setter method pair.
For example, AttributeAccessor.new(:foo)
will traverse through the getter/setter pair .foo
and .foo=
.
The abstract “attribute” does not have to correspond to an actual attr_accessor
; the AttributeAccessor will work as long as the relevant named getter/setter methods exist on the receiver.
Aliases
Default constructor used by predecessor accessor
-
OpenStruct.new
97 98 99 |
# File 'lib/accessory/access.rb', line 97 def attr(...) self.then(Accessory::Accessors::AttributeAccessor.new(...)) end |
#before_first ⇒ Object
Alias for #betwixt(0). See #betwixt
112 113 114 |
# File 'lib/accessory/access.rb', line 112 def before_first self.betwixt(0) end |
#between_each ⇒ Object
Traverses the positions “between” the elements of an Enumerable
, including the positions at the “edges” (i.e. before the first, and after the last.)
BetweenEachAccessor can be used with Lens#put_in to insert new elements into an Enumerable between the existing ones.
Aliases
-
#between_each (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Array.new
122 123 124 |
# File 'lib/accessory/access.rb', line 122 def between_each self.then(Accessory::Accessors::BetweenEachAccessor.new) end |
#betwixt ⇒ Object
Traverses into a specified cursor-position “between” two elements of an Enumerable
, including the positions at the “edges” (i.e. before the first, or after the last.)
If the provided offset
is positive, this accessor will traverse the position between offset - 1
and offset
; if offset
is negative, this accessor will traverse the position after offset
.
The offset
in this accessor has equivalent semantics to the offset in Array#insert(offset, obj)
.
BetwixtAccessor can be used with Lens#put_in to insert new elements into an Enumerable between the existing ones. If you want to extend an Enumerable
as you would with #push
or #unshift
, this accessor will have better behavior than using SubscriptAccessor would.
Aliases
Default constructor used by predecessor accessor
-
Array.new
107 108 109 |
# File 'lib/accessory/access.rb', line 107 def betwixt(...) self.then(Accessory::Accessors::BetwixtAccessor.new(...)) end |
#filter(&pred) ⇒ Object
Traverses the elements of an Enumerable
that return a truthy value from a passed-in predicate block.
Aliases
Equivalents in Elixir’s Access
module
Default constructor used by predecessor accessor
-
Array.new
142 143 144 |
# File 'lib/accessory/access.rb', line 142 def filter(&pred) self.then(Accessory::Accessors::FilterAccessor.new(pred)) end |
#first ⇒ Object
Traverses into the “first” element within an Enumerable
, using #first
.
This accessor can be preferable to SubscriptAccessor for objects that are not subscriptable, e.g. Range.
Aliases
Default constructor used by predecessor accessor
-
Array.new
132 133 134 |
# File 'lib/accessory/access.rb', line 132 def first self.then(Accessory::Accessors::FirstAccessor.new) end |
#ivar ⇒ Object
Traverses into a named instance-variable of an arbitrary object.
For example, given InstanceVariableAccessor.new(:foo)
, the instance-variable @foo
of the input data will be traversed.
Aliases
Default constructor used by predecessor accessor
-
Object.new
102 103 104 |
# File 'lib/accessory/access.rb', line 102 def ivar(...) self.then(Accessory::Accessors::InstanceVariableAccessor.new(...)) end |
#last ⇒ Object
Traverses into the “last” element within an Enumerable
, using #last
.
This accessor can be preferable to SubscriptAccessor for objects that are not subscriptable, e.g. Range.
Aliases
Default constructor used by predecessor accessor
-
Array.new
137 138 139 |
# File 'lib/accessory/access.rb', line 137 def last self.then(Accessory::Accessors::LastAccessor.new) end |
#subscript ⇒ Object
Traverses into a specified key
for an arbitrary container-object which supports the #[]
and #[]=
methods.
Aliases
-
#subscript (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 Access
module
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 (FirstAccessor, BetwixtAccessor, etc.) may fit your expectations more closely for Array
traversal.
87 88 89 |
# File 'lib/accessory/access.rb', line 87 def subscript(...) self.then(Accessory::Accessors::SubscriptAccessor.new(...)) end |