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

Class Method Details

.after_lastObject

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

.allObject

Traverses all elements of an Enumerable.

Aliases

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

.attrObject

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



27
28
29
# File 'lib/accessory/access.rb', line 27

def self.attr(...)
  Accessory::Accessors::AttributeAccessor.new(...)
end

.before_firstObject

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_eachObject

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

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

.betwixtObject

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



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

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

.firstObject

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



62
63
64
# File 'lib/accessory/access.rb', line 62

def self.first
  Accessory::Accessors::FirstAccessor.new
end

.ivarObject

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



32
33
34
# File 'lib/accessory/access.rb', line 32

def self.ivar(...)
  Accessory::Accessors::InstanceVariableAccessor.new(...)
end

.lastObject

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



67
68
69
# File 'lib/accessory/access.rb', line 67

def self.last
  Accessory::Accessors::LastAccessor.new
end

.subscriptObject

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

Aliases

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.

Parameters:

  • key (Object)

    the key to pass to the #[] and #[]= methods.



22
23
24
# File 'lib/accessory/access.rb', line 22

def self.subscript(...)
  Accessory::Accessors::SubscriptAccessor.new(...)
end