Module: Faceter::Functions Private

Extended by:
Transproc::Registry
Defined in:
lib/faceter/functions.rb,
lib/faceter/functions/wrap.rb,
lib/faceter/functions/clean.rb,
lib/faceter/functions/group.rb,
lib/faceter/functions/split.rb,
lib/faceter/functions/unwrap.rb,
lib/faceter/functions/claster.rb,
lib/faceter/functions/exclude.rb,
lib/faceter/functions/ungroup.rb,
lib/faceter/functions/add_prefix.rb,
lib/faceter/functions/drop_prefix.rb,
lib/faceter/functions/keep_symbol.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Collection of the gem-specific transformations (pure functions)

Class Method Summary collapse

Class Method Details

.add_prefix(string, prefix, separator) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds the prefix with separator to the string

Examples:

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

Parameters:

  • string (#to_s)
  • prefix (#to_s)
  • separator (#to_s)

Returns:

  • (String)


19
20
21
# File 'lib/faceter/functions/add_prefix.rb', line 19

def self.add_prefix(string, prefix, separator)
  "#{prefix}#{separator}#{string}"
end

.claster(list, fn) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • list (Array)
  • fn (#call)

Returns:

  • (Array)


15
16
17
18
19
20
21
22
23
# 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) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes selected keys from hash if they values are empty

Examples:

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

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

Parameters:

  • hash (Hash)
  • selector (Selector::Condition)

Returns:

  • (Hash)


21
22
23
# 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) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes the prefix with separator from the string

Examples:

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

Parameters:

  • string (#to_s)
  • prefix (#to_s)
  • separator (#to_sl)

Returns:

  • (String)


19
20
21
22
23
24
25
# 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) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Excludes keys, that satisfies the selector, from the hash

Examples:

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

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

Parameters:

  • hash (Hash)
  • selector (Selector::Condition)

Returns:

  • (Array<Hash>)


21
22
23
# File 'lib/faceter/functions/exclude.rb', line 21

def self.exclude(hash, selector)
  hash.reject { |key| selector[key] }
end

.group(array, key, selector) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Groups array values using provided root key and selector

Examples:

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 }] }]

Parameters:

  • array (Array)
  • key (Object)
  • selector (Selector::Condition)

Returns:

  • (Array<Hash>)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ String, Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

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

Parameters:

  • source (Object)
  • fn (Transproc::Function)

Returns:

  • (String, Symbol)


20
21
22
23
# 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) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Splits the hash into two parts using a selector

Examples:

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

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

Parameters:

  • hash (Hash)
  • selector (Selector::Condition)

Returns:

  • (Array<Hash>)


21
22
23
24
# 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) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ungroups array values using provided root key and selector

Examples:

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 }]

Parameters:

  • array (Array)
  • key (Object)
  • selector (Selector::Condition)

Returns:

  • (Array<Hash>)


22
23
24
25
26
27
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Partially unwraps a subhash under the hash key following the selector

Examples:

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 }

Parameters:

  • hash (Hash)
  • key (Object)
  • selector (Selector::Condition)

Returns:

  • Hash



22
23
24
25
26
27
28
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Partially wraps a subhash into the new key following the selector

Examples:

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 }

Parameters:

  • hash (Hash)
  • key (Object)
  • selector (Selector::Condition)

Returns:

  • Hash



22
23
24
25
26
27
28
# 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