class Each

Constants

Filter

Attributes

filter[R]
inner_focus[R]
outer_focus[R]

Public Class Methods

new(outer_focus = nil, inner_focus = nil, filter = nil) click to toggle source
# File lib/ruby-optics/each.rb, line 14
def initialize(outer_focus = nil, inner_focus = nil, filter = nil)
  @outer_focus = outer_focus || Lens.identity
  @inner_focus = inner_focus || Lens.identity
  @filter = filter
end
with_filter(focusing_lens, &blk) click to toggle source
# File lib/ruby-optics/each.rb, line 56
def self.with_filter(focusing_lens, &blk)
  Each.new.with_filter(focusing_lens, &blk)
end

Public Instance Methods

compose_lens(lens) click to toggle source
# File lib/ruby-optics/each.rb, line 48
def compose_lens(lens)
  Each.new(
    outer_focus = self.outer_focus,
    inner_focus = self.inner_focus.compose_lens(lens),
    filter      = self.filter
  )
end
get_all(object) click to toggle source
# File lib/ruby-optics/each.rb, line 36
def get_all(object)
  filtered(outer_focus.get(object)).map { |a| inner_focus.get(a) }
end
modify_all(object, &blk) click to toggle source
# File lib/ruby-optics/each.rb, line 24
def modify_all(object, &blk)
  outer_focus.modify(object) { |enumerable|
    enumerable.map { |a|
      if !filter.nil?
        filter.call(a) ? inner_focus.modify(a, &blk) : a
      else
        inner_focus.modify(a, &blk)
      end
    }
  }
end
set(new_value, object) click to toggle source
# File lib/ruby-optics/each.rb, line 20
def set(new_value, object)
  modify_all(object) { |_| new_value }
end
with_filter(focusing_lens, &blk) click to toggle source
# File lib/ruby-optics/each.rb, line 40
def with_filter(focusing_lens, &blk)
  Each.new(
    outer_focus = self.outer_focus,
    inner_focus = self.inner_focus,
    filter      = Filter.new(focusing_lens, blk)
  )
end

Private Instance Methods

filtered(enumerable) click to toggle source
# File lib/ruby-optics/each.rb, line 62
def filtered(enumerable)
  return enumerable if filter.nil? 

  enumerable.select { |object| filter.call(object) }
end