module Faceter::Functions

Collection of the gem-specific transformations (pure functions)

@api private

Public Class Methods

add_prefix(string, prefix, separator) click to toggle source

Adds the prefix with separator to the string

@example

fn = Functions[:add_prefix, "foo", "."]
fn["bar"] # => "foo.bar"

@param [#to_s] string @param [#to_s] prefix @param [#to_s] separator

@return [String]

# File lib/faceter/functions/add_prefix.rb, line 19
def self.add_prefix(string, prefix, separator)
  "#{prefix}#{separator}#{string}"
end
claster(list, fn) click to toggle source

Break the array to array of consecutive items that matches each other by given function

@param [Array] list @param [#call] fn

@return [Array]

# File lib/faceter/functions/claster.rb, line 15
def self.claster(list, fn)
  list.each_with_object([[]]) do |e, a|
    if a.last.eql?([]) || fn[e].equal?(fn[a.last.last])
      a.last << e
    else
      a << [e]
    end
  end
end
clean(hash, selector) click to toggle source

Removes selected keys from hash if they values are empty

@example

selector = Selector.new(only: /b/)
function = Functions[:clean, selector]

function[foo: {}, bar: {}, baz: :BAZ]
# => { foo: {}, baz: :BAZ }

@param [Hash] hash @param [Selector::Condition] selector

@return [Hash]

# File lib/faceter/functions/clean.rb, line 21
def self.clean(hash, selector)
  hash.reject { |key, value| selector[key] && value.empty? }
end
drop_prefix(string, prefix, separator) click to toggle source

Removes the prefix with separator from the string

@example

fn = Functions[:drop_prefix, "foo", "."]
fn["foo.bar"]  # => "bar"

@param [#to_s] string @param [#to_s] prefix @param [#to_sl] separator

@return [String]

# File lib/faceter/functions/drop_prefix.rb, line 19
def self.drop_prefix(string, prefix, separator)
  str   = string.to_s
  affix = "#{prefix}#{separator}"
  first = str.start_with?(affix) ? affix.length : 0

  str[first..-1]
end
exclude(hash, selector) click to toggle source

Excludes keys, that satisfies the selector, from the hash

@example

selector = Selector.new(only: /b/)
function = Functions[:exclude, selector]

function[{ foo: :FOO, bar: :BAR, baz: :BAZ }]
# => { foo: :FOO }

@param [Hash] hash @param [Selector::Condition] selector

@return [Array<Hash>]

# File lib/faceter/functions/exclude.rb, line 21
def self.exclude(hash, selector)
  hash.reject { |key| selector[key] }
end
group(array, key, selector) click to toggle source

Groups array values using provided root key and selector

@example

selector = Selector.new(only: [:bar])
function = Functions[:group, :qux, selector]

function[[{ foo: :FOO, bar: :BAR }, { foo: :FOO, bar: :BAZ }]]
# => [{ foo: :FOO, qux: [{ bar: :BAR }, { bar: :BAZ }] }]

@param [Array] array @param [Object] key @param [Selector::Condition] selector

@return [Array<Hash>]

# File lib/faceter/functions/group.rb, line 22
def self.group(array, key, selector)
  tuples = Hash.new { |h, k| h[k] = [] }

  array.each do |hash|
    to_group, to_keep = split(Hash[hash], selector)
    grouped = t(:to_tuples)[to_keep[key]]
    to_keep.delete(key)

    tuples[to_keep] << grouped.map { |item| item.merge(to_group) }
  end

  tuples.map do |root, children|
    list = children.flatten
    list.first.empty? ? root : root.merge(key => list)
  end
end
keep_symbol(source, fn) click to toggle source

Applies the function to the source and converts the result to symbol if the source data was a symbol.

@example

fn = Functions[:keep_symbol, -> v { v.to_s.reverse }]
fn["123"] # => "321"
fn[:123]  # => :321

@param [Object] source @param [Transproc::Function] fn

@return [String, Symbol]

# File lib/faceter/functions/keep_symbol.rb, line 20
def self.keep_symbol(source, fn)
  data = fn.call(source)
  source.instance_of?(Symbol) ? data.to_s.to_sym : data
end
split(hash, selector) click to toggle source

Splits the hash into two parts using a selector

@example

selector = Selector.new(except: /r/)
function = Functions[:split, selector]

function[{ foo: :FOO, bar: :BAR, baz: :BAZ }]
# => [{ foo: :FOO, baz: :BAZ }, { bar: :BAR }]

@param [Hash] hash @param [Selector::Condition] selector

@return [Array<Hash>]

# File lib/faceter/functions/split.rb, line 21
def self.split(hash, selector)
  fn = -> key, _ { selector[key] }
  [hash.select(&fn), hash.reject(&fn)]
end
ungroup(array, key, selector) click to toggle source

Ungroups array values using provided root key and selector

@example

selector = Selector.new(only: :bar)
function = Functions[:ungroup, :qux, selector]

function[[{ foo: :FOO, qux: [{ bar: :BAR }, { bar: :BAZ }] }]]
# => [{ foo: :FOO, bar: :BAR }, { foo: :FOO, bar: :BAZ }]

@param [Array] array @param [Object] key @param [Selector::Condition] selector

@return [Array<Hash>]

# File lib/faceter/functions/ungroup.rb, line 22
def self.ungroup(array, key, selector)
  array.flat_map do |hash|
    list = t(:to_tuples)[hash.delete(key)]
    group(list, key, !selector).map { |item| hash.merge(item) }
  end
end
unwrap(hash, key, selector) click to toggle source

Partially unwraps a subhash under the hash key following the selector

@example

selector = Selector.new(only: :bar)
function = Functions[:unwrap, :foo, selector]

function[foo: { bar: :BAR, baz: :BAZ }, qux: :QUX]
# => { qux: :QUX, foo: { baz: :BAZ }, bar: :BAR }

@param [Hash] hash @param [Object] key @param [Selector::Condition] selector

@return Hash

# File lib/faceter/functions/unwrap.rb, line 22
def self.unwrap(hash, key, selector)
  extracted, keep = split(hash.fetch(key, {}), selector)
  cleaner = Selector.new(only: key)

  clean_hash = clean(hash.merge(key => keep), cleaner)
  clean_hash.merge(extracted)
end
wrap(hash, key, selector) click to toggle source

Partially wraps a subhash into the new key following the selector

@example

selector = Selector.new(only: :bar)
function = Functions[:wrap, :foo, selector]

function[foo: { baz: :BAZ }, bar: :BAR, qux: :QUX]
# => { foo: { baz: :BAZ, bar: :BAR }, qux: :QUX }

@param [Hash] hash @param [Object] key @param [Selector::Condition] selector

@return Hash

# File lib/faceter/functions/wrap.rb, line 22
def self.wrap(hash, key, selector)
  to_wrap, to_keep = split(hash, selector)
  wrapped = to_keep[key]

  to_wrap = wrapped.merge(to_wrap) if wrapped.instance_of? Hash
  to_keep.merge(key => to_wrap)
end