Module: Xf::PublicApi

Included in:
Xf
Defined in:
lib/xf/public_api.rb

Instance Method Summary collapse

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.

Parameters:

  • start_range (Any)

    Start of a range

  • end_range (Any)

    End of a range

Returns:

  • (Proc[Any] -)

    Any] Proc that returns a value that’s been clamped to the given range



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.

See Also:



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.

Parameters:

  • targets (Array[Any])

    Targets to count

  • &fn (Proc)

    Function to define count key

Returns:

  • (Hash[Any, Integer])

    Counts



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

#logProc[Any] -

Solely meant as a tap addendum

Returns:

  • (Proc[Any] -)

    nil] nil from puts result



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

Note:

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.

Parameters:

  • *fns (#to_proc)

    List of objects coercible into Procs

Returns:

  • (Proc[Any] -)

    Any]



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

Note:

See the README for more instructions on usage. Will likely propogate more examples here and into the specs later.

Creates a Scope.

Parameters:

  • *paths (Array[Any])

    Hash#dig accessible segments

Returns:

See Also:



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.

Parameters:

  • trace_path (Any)

    Any hash key to dive searching for.

Returns:

See Also:



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.

Parameters:

  • trace_path (Any)

    Any hash value to dive searching for

Returns:

See Also:



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.

Parameters:

  • trace_path (Any)

    Any hash value to dive searching for

Returns:

See Also:



81
82
83
# File 'lib/xf/public_api.rb', line 81

def trace_value(trace_path)
  TraceValue.new(trace_path)
end