module Xf::PublicApi
Public Instance Methods
Clamps a value to be within a range. Works with numbers, dates, and other items. More of a utility function.
@param start_range [Any]
Start of a range
@param end_range [Any]
End of a range
@return [Proc -> Any]
Proc that returns a value that's been clamped to the given range
# File lib/xf/public_api.rb, line 112 def clamp(start_range, end_range) Proc.new { |target| next start_range if target < start_range next end_range if target > end_range target } end
The more traditional functional Compose, where things go in the opposite order. Pipe is probably more intuitive to Rubyists, which is why this is an implementation of pipe.
@see pipe
# File lib/xf/public_api.rb, line 35 def compose(*fns) pipe(*fns.reverse) end
Counts by a function. This is entirely because I hackney this everywhere in pry anyways, so I want a function to do it for me already.
@param targets [Array] Targets to count @param &fn [Proc] Function to define count key
@return [Hash[Any, Integer]] Counts
# File lib/xf/public_api.rb, line 128 def count_by(targets, &fn) fn ||= -> v { v } targets.each_with_object(Hash.new(0)) { |target, counts| counts[fn[target]] += 1 } end
Solely meant as a tap addendum
@return [Proc -> nil]
nil from puts result
# File lib/xf/public_api.rb, line 144 def log Proc.new { |target| puts target } end
Combines a list of functions, or items that respond to `to_proc`, into a function chain that can be called against a target.
@note
This _could_ be done with reduce, but we're trying to keep performance as close to vanilla as possible.
@param *fns [#to_proc] List of objects coercible into Procs
@return [Proc -> Any]
# File lib/xf/public_api.rb, line 20 def pipe(*fns) Proc.new { |target| new_value = target fns.each { |fn| new_value = fn.to_proc.call(new_value) } new_value } end
Creates a Scope
.
@see Xf::Scope
@note
See the README for more instructions on usage. Will likely propogate more examples here and into the specs later.
@param *paths [Array] Hash#dig accessible segments
@return [Xf::Scope]
# File lib/xf/public_api.rb, line 53 def scope(*paths) Scope.new(paths.flatten) end
# File lib/xf/public_api.rb, line 136 def slice(*keys) Proc.new { |hash| hash.slice(*keys) } end
Creates a Trace
.
@see Xf::Trace
@param trace_path [Any]
Any hash key to dive searching for.
@return [Xf::Trace]
# File lib/xf/public_api.rb, line 67 def trace(trace_path) Trace.new(trace_path) end
Creates a TraceKeyValue
, which matches against a value rather than a key.
@see Xf::TraceKeyValue
@param trace_path [Any]
Any hash value to dive searching for
@return [Xf::TraceValue]
# File lib/xf/public_api.rb, line 95 def trace_key_value(trace_key, trace_value) TraceKeyValue.new(trace_key, trace_value) end
Creates a TraceValue
, which matches against a value rather than a key.
@see Xf::TraceValue
@param trace_path [Any]
Any hash value to dive searching for
@return [Xf::TraceValue]
# File lib/xf/public_api.rb, line 81 def trace_value(trace_path) TraceValue.new(trace_path) end