class Sorta::Lens::Untyped

Extracts data from a given object

Public Class Methods

new(*args) click to toggle source
# File lib/lens/untyped.rb, line 11
def initialize(*args)
  @args = args
  validate_arguments
end
on(...) click to toggle source
# File lib/lens/untyped.rb, line 7
def self.on(...)
  new(...)
end

Public Instance Methods

call(object) click to toggle source
# File lib/lens/untyped.rb, line 16
def call(object)
  @getable = object.respond_to? :[]
  result = @args.each_with_object({}) do |sym, acc|
    acc[sym] = extract(sym, object)
  end
  @getable = nil
  result
end
extract(sym, object) click to toggle source
# File lib/lens/untyped.rb, line 25
def extract(sym, object)
  if @getable
    object[sym]
  elsif object.respond_to? sym
    object.send(sym)
  else
    raise ArgumentError, "Object #{object} does not support extracting this symbol #{sym}"
  end
end
validate_arguments() click to toggle source
# File lib/lens/untyped.rb, line 35
def validate_arguments
  @args.each do |sym|
    unless sym.is_a?(Symbol)
      raise ArgumentError,
            "Unexpected argument #{sym.class}, must be Symbol"
    end
  end
end