Module: Accessory::Access
- Defined in:
- lib/accessory/access.rb
Overview
A set of convenient module-function helpers to use with Lens[...].
These functions aren’t very convenient unless you
include Accessory
Defined Under Namespace
Modules: FluentHelpers
Class Method Summary collapse
-
.after_last ⇒ Object
Alias for Accessory::Accessors::Access.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 Accessory::Accessors::Access.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
Enumerablethat 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
keyfor an arbitrary container-object which supports the#[]and#[]=methods.
Class Method Details
.after_last ⇒ Object
Alias for Accessory::Accessors::Access.betwixt(-1). See betwixt
47 48 49 |
# File 'lib/accessory/access.rb', line 47 def self.after_last self.betwixt(-1) end |
.all ⇒ Object
Traverses all elements of an Enumerable.
Aliases
-
Accessory::Access::FluentHelpers#all (included in Lens and BoundLens)
Equivalents in Elixir’s Access module
Default constructor used by predecessor accessor
-
Array.new
57 58 59 |
# File 'lib/accessory/access.rb', line 57 def self.all 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
-
Accessory::Access::FluentHelpers#attr (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
OpenStruct.new
27 28 29 |
# File 'lib/accessory/access.rb', line 27 def self.attr(...) Accessory::Accessors::AttributeAccessor.new(...) end |
.before_first ⇒ Object
Alias for Accessory::Accessors::Access.betwixt(0). See betwixt
42 43 44 |
# File 'lib/accessory/access.rb', line 42 def self.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
-
Accessory::Access::FluentHelpers#between_each (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Array.new
52 53 54 |
# File 'lib/accessory/access.rb', line 52 def self.between_each 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
-
Accessory::Access::FluentHelpers#betwixt (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Array.new
37 38 39 |
# File 'lib/accessory/access.rb', line 37 def self.betwixt(...) 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
-
Accessory::Access::FluentHelpers#filter (included in Lens and BoundLens)
Equivalents in Elixir’s Access module
Default constructor used by predecessor accessor
-
Array.new
72 73 74 |
# File 'lib/accessory/access.rb', line 72 def self.filter(&pred) 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
-
Accessory::Access::FluentHelpers#first (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Array.new
62 63 64 |
# File 'lib/accessory/access.rb', line 62 def self.first 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
-
Accessory::Access::FluentHelpers#ivar (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Object.new
32 33 34 |
# File 'lib/accessory/access.rb', line 32 def self.ivar(...) 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
-
Accessory::Access::FluentHelpers#last (included in Lens and BoundLens)
Default constructor used by predecessor accessor
-
Array.new
67 68 69 |
# File 'lib/accessory/access.rb', line 67 def self.last Accessory::Accessors::LastAccessor.new end |
.subscript ⇒ Object
Traverses into a specified key for an arbitrary container-object which supports the #[] and #[]= methods.
Aliases
-
Accessory::Access::FluentHelpers#subscript (included in Lens and BoundLens)
-
Accessory::Access::FluentHelpers#[] (included in Lens and BoundLens)
-
just passing a
keywill 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.
22 23 24 |
# File 'lib/accessory/access.rb', line 22 def self.subscript(...) Accessory::Accessors::SubscriptAccessor.new(...) end |