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
-
.add_prefix(string, prefix, separator) ⇒ String
private
Adds the prefix with separator to the string.
-
.claster(list, fn) ⇒ Array
private
Break the array to array of consecutive items that matches each other by given function.
-
.clean(hash, selector) ⇒ Hash
private
Removes selected keys from hash if they values are empty.
-
.drop_prefix(string, prefix, separator) ⇒ String
private
Removes the prefix with separator from the string.
-
.exclude(hash, selector) ⇒ Array<Hash>
private
Excludes keys, that satisfies the selector, from the hash.
-
.group(array, key, selector) ⇒ Array<Hash>
private
Groups array values using provided root key and selector.
-
.keep_symbol(source, fn) ⇒ String, Symbol
private
Applies the function to the source and converts the result to symbol if the source data was a symbol.
-
.split(hash, selector) ⇒ Array<Hash>
private
Splits the hash into two parts using a selector.
-
.ungroup(array, key, selector) ⇒ Array<Hash>
private
Ungroups array values using provided root key and selector.
-
.unwrap(hash, key, selector) ⇒ Object
private
Partially unwraps a subhash under the hash key following the selector.
-
.wrap(hash, key, selector) ⇒ Object
private
Partially wraps a subhash into the new key following the selector.
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
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
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
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
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
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
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.
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
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
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
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
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 |