Module: Xf::PublicApi
- Included in:
- Xf
- Defined in:
- lib/xf/public_api.rb
Instance Method Summary collapse
-
#clamp(start_range, end_range) ⇒ Proc[Any] -
Clamps a value to be within a range.
-
#compose(*fns) ⇒ Object
(also: #c)
The more traditional functional Compose, where things go in the opposite order.
-
#count_by(targets, &fn) ⇒ Hash[Any, Integer]
Counts by a function.
-
#log ⇒ Proc[Any] -
Solely meant as a tap addendum.
-
#pipe(*fns) ⇒ Proc[Any] -
(also: #p)
Combines a list of functions, or items that respond to ‘to_proc`, into a function chain that can be called against a target.
-
#scope(*paths) ⇒ Xf::Scope
(also: #s)
Creates a Scope.
- #slice(*keys) ⇒ Object
-
#trace(trace_path) ⇒ Xf::Trace
(also: #t)
Creates a Trace.
-
#trace_key_value(trace_key, trace_value) ⇒ Xf::TraceValue
(also: #tkv)
Creates a TraceKeyValue, which matches against a value rather than a key.
-
#trace_value(trace_path) ⇒ Xf::TraceValue
(also: #tv)
Creates a TraceValue, which matches against a value rather than a key.
Instance Method Details
#clamp(start_range, end_range) ⇒ Proc[Any] -
Clamps a value to be within a range. Works with numbers, dates, and other items. More of a utility function.
112 113 114 115 116 117 118 119 |
# 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 |
#compose(*fns) ⇒ Object Also known as: c
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.
35 36 37 |
# File 'lib/xf/public_api.rb', line 35 def compose(*fns) pipe(*fns.reverse) end |
#count_by(targets, &fn) ⇒ Hash[Any, Integer]
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.
128 129 130 131 132 133 134 |
# 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 |
#log ⇒ Proc[Any] -
Solely meant as a tap addendum
144 145 146 |
# File 'lib/xf/public_api.rb', line 144 def log Proc.new { |target| puts target } end |
#pipe(*fns) ⇒ Proc[Any] - Also known as: p
This could be done with reduce, but we’re trying to keep performance as close to vanilla as possible.
Combines a list of functions, or items that respond to ‘to_proc`, into a function chain that can be called against a target.
20 21 22 23 24 25 26 |
# 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 |
#scope(*paths) ⇒ Xf::Scope Also known as: s
See the README for more instructions on usage. Will likely propogate more examples here and into the specs later.
Creates a Scope.
53 54 55 |
# File 'lib/xf/public_api.rb', line 53 def scope(*paths) Scope.new(paths.flatten) end |
#slice(*keys) ⇒ Object
136 137 138 |
# File 'lib/xf/public_api.rb', line 136 def slice(*keys) Proc.new { |hash| hash.slice(*keys) } end |
#trace(trace_path) ⇒ Xf::Trace Also known as: t
Creates a Trace.
67 68 69 |
# File 'lib/xf/public_api.rb', line 67 def trace(trace_path) Trace.new(trace_path) end |
#trace_key_value(trace_key, trace_value) ⇒ Xf::TraceValue Also known as: tkv
Creates a TraceKeyValue, which matches against a value rather than a key.
95 96 97 |
# File 'lib/xf/public_api.rb', line 95 def trace_key_value(trace_key, trace_value) TraceKeyValue.new(trace_key, trace_value) end |
#trace_value(trace_path) ⇒ Xf::TraceValue Also known as: tv
Creates a TraceValue, which matches against a value rather than a key.
81 82 83 |
# File 'lib/xf/public_api.rb', line 81 def trace_value(trace_path) TraceValue.new(trace_path) end |